Skip to content

Commit 7e98ac6

Browse files
DOC-6019 Jedis notebook for strings data type page
1 parent 1953236 commit 7e98ac6

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// EXAMPLE: set_tutorial
2+
// BINDER_ID jedis-dt-string
3+
package io.redis.examples;
4+
5+
//REMOVE_START
6+
import static org.junit.jupiter.api.Assertions.*;
7+
8+
import org.junit.jupiter.api.Test;
9+
//REMOVE_END
10+
11+
import redis.clients.jedis.UnifiedJedis;
12+
import redis.clients.jedis.params.SetParams;
13+
14+
import java.util.ArrayList;
15+
import java.util.Arrays;
16+
import java.util.List;
17+
18+
public class StringExample {
19+
20+
@Test
21+
public void run() {
22+
try (UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379")) {
23+
24+
// STEP_START set_get
25+
String res1 = jedis.set("bike:1", "Deimos");
26+
System.out.println(res1); // OK
27+
String res2 = jedis.get("bike:1");
28+
System.out.println(res2); // Deimos
29+
// STEP_END
30+
31+
// REMOVE_START
32+
assertEquals("OK", res1);
33+
assertEquals("Deimos", res2);
34+
// REMOVE_END
35+
36+
// STEP_START setnx_xx
37+
Long res3 = jedis.setnx("bike:1", "bike");
38+
System.out.println(res3); // 0 (because key already exists)
39+
System.out.println(jedis.get("bike:1")); // Deimos (value is unchanged)
40+
String res4 = jedis.set("bike:1", "bike", SetParams.setParams().xx()); // set the value to "bike" if it
41+
// already
42+
// exists
43+
System.out.println(res4); // OK
44+
// STEP_END
45+
46+
// REMOVE_START
47+
assertEquals(0L, res3.longValue());
48+
assertEquals("OK", res4);
49+
// REMOVE_END
50+
51+
// STEP_START mset
52+
String res5 = jedis.mset("bike:1", "Deimos", "bike:2", "Ares", "bike:3", "Vanth");
53+
System.out.println(res5); // OK
54+
List<String> res6 = jedis.mget("bike:1", "bike:2", "bike:3");
55+
System.out.println(res6); // [Deimos, Ares, Vanth]
56+
// STEP_END
57+
58+
// REMOVE_START
59+
assertEquals("OK", res5);
60+
List<String> expected = new ArrayList<>(Arrays.asList("Deimos", "Ares", "Vanth"));
61+
assertEquals(expected, res6);
62+
// REMOVE_END
63+
64+
// STEP_START incr
65+
jedis.set("total_crashes", "0");
66+
Long res7 = jedis.incr("total_crashes");
67+
System.out.println(res7); // 1
68+
Long res8 = jedis.incrBy("total_crashes", 10);
69+
System.out.println(res8); // 11
70+
// STEP_END
71+
72+
// REMOVE_START
73+
assertEquals(1L, res7.longValue());
74+
assertEquals(11L, res8.longValue());
75+
// REMOVE_END
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)