Skip to content

Instantly share code, notes, and snippets.

@Honatas
Created December 24, 2020 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Honatas/097a5dff24b99b688d40e433ebfae2bb to your computer and use it in GitHub Desktop.
Save Honatas/097a5dff24b99b688d40e433ebfae2bb to your computer and use it in GitHub Desktop.
A basic wrapper around Jedis
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
public class RedisClient {
private static JedisPool pool;
private RedisClient() {
}
public static void init() {
if (pool == null) {
pool = new JedisPool();
}
}
public static Jedis getConnection() {
if (pool == null) {
RedisClient.init();
}
return pool.getResource();
}
public static void pushOnQueue(String queueName, String data) {
try (Jedis jedis = RedisClient.getConnection()) {
jedis.lpush(queueName, data);
}
}
public static String get(String key) {
try (Jedis jedis = RedisClient.getConnection()) {
return jedis.get(key);
}
}
public static void set(String key, String value) {
try (Jedis jedis = RedisClient.getConnection()) {
jedis.set(key, value);
}
}
public static void disconnect() {
pool.close();
pool = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment