Skip to content

Update "Pipelines and transactions" doc example to include pipeline.close() #4134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 7, 2025
Merged
Changes from 1 commit
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
118 changes: 63 additions & 55 deletions src/test/java/io/redis/examples/PipeTransExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,42 +27,49 @@ public void run() {
// REMOVE_END

// STEP_START basic_pipe
AbstractPipeline pipe = jedis.pipelined();
// Pipeline is a way to send multiple commands to Redis in a single request.
// It can be used to improve performance by reducing the number of round trips to the server.
// Make sure to close the pipeline after use to release resources and return the connection to the pool.
try (AbstractPipeline pipe = jedis.pipelined()) {

for (int i = 0; i < 5; i++) {
pipe.set(String.format("seat:%d", i), String.format("#%d", i));
for (int i = 0; i < 5; i++) {
pipe.set(String.format("seat:%d", i), String.format("#%d", i));
}

pipe.sync();
}

pipe.sync();
try (AbstractPipeline pipe = jedis.pipelined()) {

pipe = jedis.pipelined();
Response<String> resp0 = pipe.get("seat:0");
Response<String> resp3 = pipe.get("seat:3");
Response<String> resp4 = pipe.get("seat:4");

Response<String> resp0 = pipe.get("seat:0");
Response<String> resp3 = pipe.get("seat:3");
Response<String> resp4 = pipe.get("seat:4");
pipe.sync();

pipe.sync();
// Responses are available after the pipeline has executed.
System.out.println(resp0.get()); // >>> #0
System.out.println(resp3.get()); // >>> #3
System.out.println(resp4.get()); // >>> #4

// Responses are available after the pipeline has executed.
System.out.println(resp0.get()); // >>> #0
System.out.println(resp3.get()); // >>> #3
System.out.println(resp4.get()); // >>> #4

// REMOVE_START
Assert.assertEquals("#0", resp0.get());
Assert.assertEquals("#3", resp3.get());
Assert.assertEquals("#4", resp4.get());
// REMOVE_END
}
// STEP_END
// REMOVE_START
Assert.assertEquals("#0", resp0.get());
Assert.assertEquals("#3", resp3.get());
Assert.assertEquals("#4", resp4.get());
// REMOVE_END

// STEP_START basic_trans
AbstractTransaction trans = jedis.multi();

trans.incrBy("counter:1", 1);
trans.incrBy("counter:2", 2);
trans.incrBy("counter:3", 3);
try ( AbstractTransaction trans = jedis.multi()) {

trans.exec();
trans.incrBy("counter:1", 1);
trans.incrBy("counter:2", 2);
trans.incrBy("counter:3", 3);

trans.exec();
}
System.out.println(jedis.get("counter:1")); // >>> 1
System.out.println(jedis.get("counter:2")); // >>> 2
System.out.println(jedis.get("counter:3")); // >>> 3
Expand All @@ -78,40 +85,41 @@ public void run() {
jedis.set("shellpath", "/usr/syscmds/");

// Start the transaction and watch the key we are about to update.
trans = jedis.transaction(false); // create a Transaction object without sending MULTI command
trans.watch("shellpath"); // send WATCH command(s)
trans.multi(); // send MULTI command

String currentPath = jedis.get("shellpath");
String newPath = currentPath + ":/usr/mycmds/";

// Commands added to the `trans` object
// will be buffered until `trans.exec()` is called.
Response<String> setResult = trans.set("shellpath", newPath);
List<Object> transResults = trans.exec();

// The `exec()` call returns null if the transaction failed.
if (transResults != null) {
// Responses are available if the transaction succeeded.
System.out.println(setResult.get()); // >>> OK

// You can also get the results from the list returned by
// `trans.exec()`.
for (Object item: transResults) {
System.out.println(item);
try (AbstractTransaction trans = jedis.transaction(false)) { // create a Transaction object without sending MULTI command
trans.watch("shellpath"); // send WATCH command(s)
trans.multi(); // send MULTI command

String currentPath = jedis.get("shellpath");
String newPath = currentPath + ":/usr/mycmds/";

// Commands added to the `trans` object
// will be buffered until `trans.exec()` is called.
Response<String> setResult = trans.set("shellpath", newPath);
List<Object> transResults = trans.exec();

// The `exec()` call returns null if the transaction failed.
if (transResults != null) {
// Responses are available if the transaction succeeded.
System.out.println(setResult.get()); // >>> OK

// You can also get the results from the list returned by
// `trans.exec()`.
for (Object item: transResults) {
System.out.println(item);
}
// >>> OK

System.out.println(jedis.get("shellpath"));
// >>> /usr/syscmds/:/usr/mycmds/
}
// >>> OK

System.out.println(jedis.get("shellpath"));
// >>> /usr/syscmds/:/usr/mycmds/
// REMOVE_START
Assert.assertEquals("/usr/syscmds/:/usr/mycmds/", jedis.get("shellpath"));
Assert.assertEquals("OK", setResult.get());
Assert.assertEquals(1, transResults.size());
Assert.assertEquals("OK", transResults.get(0).toString());
// REMOVE_END
}
// STEP_END
// REMOVE_START
Assert.assertEquals("/usr/syscmds/:/usr/mycmds/", jedis.get("shellpath"));
Assert.assertEquals("OK", setResult.get());
Assert.assertEquals(1, transResults.size());
Assert.assertEquals("OK", transResults.get(0).toString());
// REMOVE_END

// HIDE_START
jedis.close();
Expand Down
Loading