Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 16 additions & 23 deletions content/develop/clients/jedis/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,38 +58,31 @@ To include `Jedis` as a dependency in your application, edit the dependency file

## Connect and test

The following code opens a basic connection to a local Redis server
and closes it after use.
Add the following imports to your source file:

```java
package org.example;
import redis.clients.jedis.UnifiedJedis;
{{< clients-example set="landing" step="import" lang_filter="Java-Sync" >}}
{{< /clients-example >}}

public class Main {
public static void main(String[] args) {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
Connect to localhost on port 6379:

// Code that interacts with Redis...

jedis.close();
}
}
```
{{< clients-example set="landing" step="connect" lang_filter="Java-Sync" >}}
{{< /clients-example >}}

After you have connected, you can check the connection by storing and
retrieving a simple string value:
retrieving a simple [string]({{< relref "/develop/data-types/strings" >}}) value:

{{< clients-example set="landing" step="set_get_string" lang_filter="Java-Sync" >}}
{{< /clients-example >}}

```java
...
Store and retrieve a [hash]({{< relref "/develop/data-types/hashes" >}}):

String res1 = jedis.set("bike:1", "Deimos");
System.out.println(res1); // OK
{{< clients-example set="landing" step="set_get_hash" lang_filter="Java-Sync" >}}
{{< /clients-example >}}

String res2 = jedis.get("bike:1");
System.out.println(res2); // Deimos
Close the connection when you're done:

...
```
{{< clients-example set="landing" step="close" lang_filter="Java-Sync" >}}
{{< /clients-example >}}

## More information

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// EXAMPLE: landing
// BINDER_ID netasync-landing
// STEP_START import
using NRedisStack;
using NRedisStack.RedisStackCommands;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// EXAMPLE: landing
// BINDER_ID netsync-landing
// STEP_START import
using NRedisStack;
using NRedisStack.RedisStackCommands;
Expand Down
44 changes: 44 additions & 0 deletions local_examples/client-specific/jedis/LandingExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// EXAMPLE: landing
// BINDER_ID jedis-landing
// STEP_START import
import redis.clients.jedis.UnifiedJedis;
import java.util.HashMap;
import java.util.Map;
// STEP_END

public class LandingExample {

@Test
public void run() {
// STEP_START connect
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
// STEP_END

// STEP_START set_get_string
String res1 = jedis.set("bike:1", "Deimos");
System.out.println(res1); // >>> OK

String res2 = jedis.get("bike:1");
System.out.println(res2); // >>> Deimos
// STEP_END

// STEP_START set_get_hash
Map<String, String> hash = new HashMap<>();
hash.put("name", "John");
hash.put("surname", "Smith");
hash.put("company", "Redis");
hash.put("age", "29");

Long res3 = jedis.hset("user-session:123", hash);
System.out.println(res3); // >>> 4

Map<String, String> res4 = jedis.hgetAll("user-session:123");
System.out.println(res4);
// >>> {name=John, surname=Smith, company=Redis, age=29}
// STEP_END

// STEP_START close
jedis.close();
// STEP_END
}
}