|
| 1 | +package com.baeldung.redis_scan.client; |
| 2 | + |
| 3 | +import com.baeldung.redis_scan.iterator.RedisIterator; |
| 4 | +import com.baeldung.redis_scan.strategy.ScanStrategy; |
| 5 | +import org.slf4j.Logger; |
| 6 | +import org.slf4j.LoggerFactory; |
| 7 | +import redis.clients.jedis.Jedis; |
| 8 | +import redis.clients.jedis.JedisPool; |
| 9 | + |
| 10 | +import java.net.URI; |
| 11 | +import java.net.URISyntaxException; |
| 12 | +import java.util.*; |
| 13 | + |
| 14 | +public class RedisClient { |
| 15 | + private static Logger log = LoggerFactory.getLogger(RedisClient.class); |
| 16 | + |
| 17 | + private static volatile RedisClient instance = null; |
| 18 | + |
| 19 | + private static JedisPool jedisPool; |
| 20 | + |
| 21 | + public static RedisClient getInstance(String ip, final int port) { |
| 22 | + if (instance == null) { |
| 23 | + synchronized (RedisClient.class) { |
| 24 | + if (instance == null) { |
| 25 | + instance = new RedisClient(ip, port); |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + return instance; |
| 30 | + } |
| 31 | + |
| 32 | + private RedisClient(String ip, int port) { |
| 33 | + try { |
| 34 | + if (jedisPool == null) { |
| 35 | + jedisPool = new JedisPool(new URI("http://" + ip + ":" + port)); |
| 36 | + } |
| 37 | + } catch (URISyntaxException e) { |
| 38 | + log.error("Malformed server address", e); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + public Long lpush(final String key, final String[] strings) { |
| 43 | + try (Jedis jedis = jedisPool.getResource()) { |
| 44 | + return jedis.lpush(key, strings); |
| 45 | + } catch (Exception ex) { |
| 46 | + log.error("Exception caught in lpush", ex); |
| 47 | + } |
| 48 | + return null; |
| 49 | + } |
| 50 | + |
| 51 | + public List<String> lrange(final String key, final long start, final long stop) { |
| 52 | + try (Jedis jedis = jedisPool.getResource()) { |
| 53 | + return jedis.lrange(key, start, stop); |
| 54 | + } catch (Exception ex) { |
| 55 | + log.error("Exception caught in lrange", ex); |
| 56 | + } |
| 57 | + return new LinkedList<String>(); |
| 58 | + } |
| 59 | + |
| 60 | + public String hmset(final String key, final Map<String, String> hash) { |
| 61 | + try (Jedis jedis = jedisPool.getResource()) { |
| 62 | + return jedis.hmset(key, hash); |
| 63 | + } catch (Exception ex) { |
| 64 | + log.error("Exception caught in hmset", ex); |
| 65 | + } |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + public Map<String, String> hgetAll(final String key) { |
| 70 | + try (Jedis jedis = jedisPool.getResource()) { |
| 71 | + return jedis.hgetAll(key); |
| 72 | + } catch (Exception ex) { |
| 73 | + log.error("Exception caught in hgetAll", ex); |
| 74 | + } |
| 75 | + return new HashMap<String, String>(); |
| 76 | + } |
| 77 | + |
| 78 | + public Long sadd(final String key, final String... members) { |
| 79 | + try (Jedis jedis = jedisPool.getResource()) { |
| 80 | + return jedis.sadd(key, members); |
| 81 | + } catch (Exception ex) { |
| 82 | + log.error("Exception caught in sadd", ex); |
| 83 | + } |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + public Set<String> smembers(final String key) { |
| 88 | + try (Jedis jedis = jedisPool.getResource()) { |
| 89 | + return jedis.smembers(key); |
| 90 | + } catch (Exception ex) { |
| 91 | + log.error("Exception caught in smembers", ex); |
| 92 | + } |
| 93 | + return new HashSet<String>(); |
| 94 | + } |
| 95 | + |
| 96 | + public Long zadd(final String key, final Map<String, Double> scoreMembers) { |
| 97 | + try (Jedis jedis = jedisPool.getResource()) { |
| 98 | + return jedis.zadd(key, scoreMembers); |
| 99 | + } catch (Exception ex) { |
| 100 | + log.error("Exception caught in zadd", ex); |
| 101 | + } |
| 102 | + return 0L; |
| 103 | + } |
| 104 | + |
| 105 | + public Set<String> zrange(final String key, final long start, final long stop) { |
| 106 | + try (Jedis jedis = jedisPool.getResource()) { |
| 107 | + return jedis.zrange(key, start, stop); |
| 108 | + } catch (Exception ex) { |
| 109 | + log.error("Exception caught in zrange", ex); |
| 110 | + } |
| 111 | + return new HashSet<String>(); |
| 112 | + } |
| 113 | + |
| 114 | + public String mset(final HashMap<String, String> keysValues) { |
| 115 | + try (Jedis jedis = jedisPool.getResource()) { |
| 116 | + ArrayList<String> keysValuesArrayList = new ArrayList<String>(); |
| 117 | + keysValues.forEach((key, value) -> { |
| 118 | + keysValuesArrayList.add(key); |
| 119 | + keysValuesArrayList.add(value); |
| 120 | + }); |
| 121 | + return jedis.mset((keysValuesArrayList.toArray(new String[keysValues.size()]))); |
| 122 | + } catch (Exception ex) { |
| 123 | + log.error("Exception caught in mset", ex); |
| 124 | + } |
| 125 | + return null; |
| 126 | + } |
| 127 | + |
| 128 | + public Set<String> keys(final String pattern) { |
| 129 | + try (Jedis jedis = jedisPool.getResource()) { |
| 130 | + return jedis.keys(pattern); |
| 131 | + } catch (Exception ex) { |
| 132 | + log.error("Exception caught in keys", ex); |
| 133 | + } |
| 134 | + return new HashSet<String>(); |
| 135 | + } |
| 136 | + |
| 137 | + public RedisIterator iterator(int initialScanCount, String pattern, ScanStrategy strategy) { |
| 138 | + return new RedisIterator(jedisPool, initialScanCount, pattern, strategy); |
| 139 | + } |
| 140 | + |
| 141 | + public void flushAll() { |
| 142 | + try (Jedis jedis = jedisPool.getResource()) { |
| 143 | + jedis.flushAll(); |
| 144 | + } catch (Exception ex) { |
| 145 | + log.error("Exception caught in flushAll", ex); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | +} |
0 commit comments