Created
December 24, 2020 14:17
-
-
Save Honatas/097a5dff24b99b688d40e433ebfae2bb to your computer and use it in GitHub Desktop.
A basic wrapper around Jedis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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