-
Notifications
You must be signed in to change notification settings - Fork 2
Iterators
In the iterators package you can find iterable/iterators thay will resolve SCAN, HSCAN, SSCAN and ZSCAN operations.
Each iterable can give multiple iteators or be used into a foreach block. The iterable only marks the structure to retrieve the data and the iterator retrieves the data from redis. No change to the data is done (except if you use the remove operation, after next operation).
For example, get all the keys from redis of 'mydata' group into a comma-separated string:
ScanIterable scanIterable = new ScanIterable(jedisPool, "mydata:*");
Iterator<String> iterator = scanIterable.iterator();
StringBuilder sb = new StringBuilder();
while(iterator.hasNext()) {
sb.append(iterator.next()).append(",");
}
Or with for block
ScanIterable scanIterable = new ScanIterable(jedisPool, "mydata:*");
StringBuilder sb = new StringBuilder();
for(String key: scanIterable) {
sb.append(key).append(",");
}
Or for all entries on a map
HScanIterable hscanIterable = new HScanIterable(jedisPool, "mymap");
Iterator<Map.Entry<String,String>> iterator = hscanIterable.iterator();
StringBuilder sb = new StringBuilder();
while(iterator.hasNext()) {
Map.Entry<String,String> entry = iterator.next();
sb.append(entry.getKey() + ":" + entry.getValue() + ",");
}
The iterators will try to make one call to Redis to return one resutl in every hasNext() operation, but this is not guaranteed. But it is guaranteed:
- hasNext will return true only if more data exists
- next will return only one result if hasNext is true
Also, xSCAN operations can rarely return duplicates results, and this is not controlled... you must control it. See ScanUtil.retrieveListOfKeys code to see how to avoid it (tip: use a Set)
ScanUtil class has some shorthand methods to redis SCAN methods