Skip to content

Commit 51b92a3

Browse files
tapankavasthisjmillington
authored andcommitted
BAEL-3613: Reading All Available Redis Keys (eugenp#8490)
1 parent 6ae0be5 commit 51b92a3

File tree

10 files changed

+537
-1
lines changed

10 files changed

+537
-1
lines changed

persistence-modules/redis/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
</dependencies>
3939

4040
<properties>
41-
<jedis.version>2.9.0</jedis.version>
41+
<jedis.version>3.2.0</jedis.version>
4242
<embedded-redis.version>0.6</embedded-redis.version>
4343
<redisson.version>3.3.0</redisson.version>
4444
<lettuce-core.version>5.0.1.RELEASE</lettuce-core.version>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.redis_scan.iterator;
2+
3+
import com.baeldung.redis_scan.strategy.ScanStrategy;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import redis.clients.jedis.Jedis;
7+
import redis.clients.jedis.JedisPool;
8+
import redis.clients.jedis.ScanParams;
9+
import redis.clients.jedis.ScanResult;
10+
11+
import java.util.Iterator;
12+
import java.util.LinkedList;
13+
import java.util.List;
14+
15+
public class RedisIterator<T> implements Iterator<List<T>> {
16+
17+
private static Logger log = LoggerFactory.getLogger(RedisIterator.class);
18+
private static final int DEFAULT_SCAN_COUNT = 10;
19+
20+
private final JedisPool jedisPool;
21+
private ScanParams scanParams;
22+
private String cursor;
23+
private ScanStrategy<T> strategy;
24+
25+
public RedisIterator(JedisPool jedisPool, int initialScanCount, String pattern, ScanStrategy<T> strategy) {
26+
super();
27+
this.jedisPool = jedisPool;
28+
this.scanParams = new ScanParams().match(pattern).count(initialScanCount);
29+
this.strategy = strategy;
30+
}
31+
32+
public RedisIterator(JedisPool jedisPool, String pattern, ScanStrategy<T> strategy) {
33+
super();
34+
this.jedisPool = jedisPool;
35+
this.scanParams = new ScanParams().match(pattern).count(DEFAULT_SCAN_COUNT);
36+
this.strategy = strategy;
37+
}
38+
39+
@Override
40+
public boolean hasNext() {
41+
return !"0".equals(cursor);
42+
}
43+
44+
@Override
45+
public List<T> next() {
46+
if (cursor == null) {
47+
cursor = "0";
48+
}
49+
try (Jedis jedis = jedisPool.getResource()) {
50+
ScanResult<T> scanResult = strategy.scan(jedis, cursor, scanParams);
51+
cursor = scanResult.getCursor();
52+
return scanResult.getResult();
53+
54+
} catch (Exception ex) {
55+
log.error("Exception caught in next()", ex);
56+
}
57+
return new LinkedList<T>();
58+
}
59+
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.redis_scan.strategy;
2+
3+
import redis.clients.jedis.Jedis;
4+
import redis.clients.jedis.ScanParams;
5+
import redis.clients.jedis.ScanResult;
6+
7+
public interface ScanStrategy<T> {
8+
ScanResult<T> scan(Jedis jedis, String cursor, ScanParams scanParams);
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.redis_scan.strategy.impl;
2+
3+
import com.baeldung.redis_scan.strategy.ScanStrategy;
4+
import redis.clients.jedis.Jedis;
5+
import redis.clients.jedis.ScanParams;
6+
import redis.clients.jedis.ScanResult;
7+
8+
import java.util.Map;
9+
import java.util.Map.Entry;
10+
11+
public class Hscan implements ScanStrategy<Map.Entry<String, String>> {
12+
13+
private String key;
14+
15+
public Hscan(String key) {
16+
super();
17+
this.key = key;
18+
}
19+
20+
@Override
21+
public ScanResult<Entry<String, String>> scan(Jedis jedis, String cursor, ScanParams scanParams) {
22+
return jedis.hscan(key, cursor, scanParams);
23+
}
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.redis_scan.strategy.impl;
2+
3+
import com.baeldung.redis_scan.strategy.ScanStrategy;
4+
import redis.clients.jedis.Jedis;
5+
import redis.clients.jedis.ScanParams;
6+
import redis.clients.jedis.ScanResult;
7+
8+
public class Scan implements ScanStrategy<String> {
9+
10+
11+
public ScanResult<String> scan(Jedis jedis, String cursor, ScanParams scanParams) {
12+
return jedis.scan(cursor, scanParams);
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.redis_scan.strategy.impl;
2+
3+
import com.baeldung.redis_scan.strategy.ScanStrategy;
4+
import redis.clients.jedis.Jedis;
5+
import redis.clients.jedis.ScanParams;
6+
import redis.clients.jedis.ScanResult;
7+
8+
public class Sscan implements ScanStrategy<String> {
9+
10+
private String key;
11+
12+
13+
public Sscan(String key) {
14+
super();
15+
this.key = key;
16+
}
17+
18+
public String getKey() {
19+
return key;
20+
}
21+
22+
public void setKey(String key) {
23+
this.key = key;
24+
}
25+
26+
public ScanResult<String> scan(Jedis jedis, String cursor, ScanParams scanParams) {
27+
return jedis.sscan(key, cursor, scanParams);
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.redis_scan.strategy.impl;
2+
3+
import com.baeldung.redis_scan.strategy.ScanStrategy;
4+
import redis.clients.jedis.Jedis;
5+
import redis.clients.jedis.ScanParams;
6+
import redis.clients.jedis.ScanResult;
7+
import redis.clients.jedis.Tuple;
8+
9+
public class Zscan implements ScanStrategy<Tuple> {
10+
11+
private String key;
12+
13+
14+
public Zscan(String key) {
15+
super();
16+
this.key = key;
17+
}
18+
19+
20+
@Override
21+
public ScanResult<Tuple> scan(Jedis jedis, String cursor, ScanParams scanParams) {
22+
return jedis.zscan(key, cursor, scanParams);
23+
}
24+
25+
}

0 commit comments

Comments
 (0)