Skip to content

Instantly share code, notes, and snippets.

@guyroyse
Last active March 11, 2020 23:08
Show Gist options
  • Save guyroyse/1c1f51bffecd540d8e682cc7446660b2 to your computer and use it in GitHub Desktop.
Save guyroyse/1c1f51bffecd540d8e682cc7446660b2 to your computer and use it in GitHub Desktop.
package com.guyroyse.blogs.lettucevsjedis;
import redis.clients.jedis.*;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class JedisMultithreaded {
private static final String YOUR_CONNECTION_STRING = "redis://:foobared@yourserver:6379/0";
public static void main(String[] args) {
JedisPool pool = new JedisPool(YOUR_CONNECTION_STRING);
List<String> allResults = IntStream.rangeClosed(1, 5)
.parallel()
.mapToObj(n -> {
Jedis jedis = pool.getResource();
jedis.set("foo" + n, "bar" + n);
String result = jedis.get("foo" + n);
jedis.close();
return result;
})
.collect(Collectors.toList());
pool.close();
System.out.println(allResults); // "bar1, bar2, bar3, bar4, bar5"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment