Skip to content

Commit 03ce340

Browse files
DOC-4560 pipe/transaction examples for docs (#4038)
* DOC-4560 pipe/transaction examples for docs * Update src/test/java/io/redis/examples/PipeTransExample.java Co-authored-by: M Sazzadul Hoque <[email protected]> --------- Co-authored-by: M Sazzadul Hoque <[email protected]>
1 parent ad593ba commit 03ce340

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// EXAMPLE: pipe_trans_tutorial
2+
// REMOVE_START
3+
package io.redis.examples;
4+
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
// REMOVE_END
8+
import java.util.List;
9+
10+
import redis.clients.jedis.UnifiedJedis;
11+
import redis.clients.jedis.AbstractPipeline;
12+
import redis.clients.jedis.AbstractTransaction;
13+
import redis.clients.jedis.Response;
14+
15+
public class PipeTransExample {
16+
@Test
17+
public void run() {
18+
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
19+
// REMOVE_START
20+
for (int i = 0; i < 5; i++) {
21+
jedis.del(String.format("seat:%d", i));
22+
}
23+
24+
jedis.del("counter:1", "counter:2", "counter:3", "shellpath");
25+
// REMOVE_END
26+
27+
// STEP_START basic_pipe
28+
AbstractPipeline pipe = jedis.pipelined();
29+
30+
for (int i = 0; i < 5; i++) {
31+
pipe.set(String.format("seat:%d", i), String.format("#%d", i));
32+
}
33+
34+
pipe.sync();
35+
36+
pipe = jedis.pipelined();
37+
38+
Response<String> resp0 = pipe.get("seat:0");
39+
Response<String> resp3 = pipe.get("seat:3");
40+
Response<String> resp4 = pipe.get("seat:4");
41+
42+
pipe.sync();
43+
44+
// Responses are available after the pipeline has executed.
45+
System.out.println(resp0.get()); // >>> #0
46+
System.out.println(resp3.get()); // >>> #3
47+
System.out.println(resp4.get()); // >>> #4
48+
// STEP_END
49+
// REMOVE_START
50+
Assert.assertEquals("#0", resp0.get());
51+
Assert.assertEquals("#3", resp3.get());
52+
Assert.assertEquals("#4", resp4.get());
53+
// REMOVE_END
54+
55+
// STEP_START basic_trans
56+
AbstractTransaction trans = jedis.multi();
57+
58+
trans.incrBy("counter:1", 1);
59+
trans.incrBy("counter:2", 2);
60+
trans.incrBy("counter:3", 3);
61+
62+
trans.exec();
63+
64+
System.out.println(jedis.get("counter:1")); // >>> 1
65+
System.out.println(jedis.get("counter:2")); // >>> 2
66+
System.out.println(jedis.get("counter:3")); // >>> 3
67+
// STEP_END
68+
// REMOVE_START
69+
Assert.assertEquals("1", jedis.get("counter:1"));
70+
Assert.assertEquals("2", jedis.get("counter:2"));
71+
Assert.assertEquals("3", jedis.get("counter:3"));
72+
// REMOVE_END
73+
74+
// STEP_START trans_watch
75+
// Set initial value of `shellpath`.
76+
jedis.set("shellpath", "/usr/syscmds/");
77+
78+
// Start the transaction and watch the key we are about to update.
79+
trans = jedis.transaction(false); // create a Transaction object without sending MULTI command
80+
trans.watch("shellpath"); // send WATCH command(s)
81+
trans.multi(); // send MULTI command
82+
83+
String currentPath = jedis.get("shellpath");
84+
String newPath = currentPath + ":/usr/mycmds/";
85+
86+
// Commands added to the `trans` object
87+
// will be buffered until `trans.exec()` is called.
88+
Response<String> setResult = trans.set("shellpath", newPath);
89+
List<Object> transResults = trans.exec();
90+
91+
// The `exec()` call returns null if the transaction failed.
92+
if (transResults != null) {
93+
// Responses are available if the transaction succeeded.
94+
System.out.println(setResult.get()); // >>> OK
95+
96+
// You can also get the results from the list returned by
97+
// `trans.exec()`.
98+
for (Object item: transResults) {
99+
System.out.println(item);
100+
}
101+
// >>> OK
102+
103+
System.out.println(jedis.get("shellpath"));
104+
// >>> /usr/syscmds/:/usr/mycmds/
105+
}
106+
// STEP_END
107+
// REMOVE_START
108+
Assert.assertEquals("/usr/syscmds/:/usr/mycmds/", jedis.get("shellpath"));
109+
Assert.assertEquals("OK", setResult.get());
110+
Assert.assertEquals(1, transResults.size());
111+
Assert.assertEquals("OK", transResults.get(0).toString());
112+
// REMOVE_END
113+
114+
// HIDE_START
115+
jedis.close();
116+
}
117+
}
118+
// HIDE_END

0 commit comments

Comments
 (0)