Skip to content

Commit bd6985c

Browse files
committed
Add Strings Example
1 parent 3b44006 commit bd6985c

File tree

1 file changed

+75
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)