Recently received a request, the background Management page, provides a simple Redis operation interface (in fact RDM itself is very good, https://github.com/uglide/RedisDesktopManager/releases/why still want to do this, RDM should only be used by operators, involving account password and other rights things, regardless of how the basic functions to achieve.
The first requirement lists the key of the Redis, and the ability to query the key list according to pattern.
Redis itself provides a info,dbsize,keys *
Info can see key number of all libraries
Dbsize is the number of current library key
Keys * This small amount of data can also be a big time to directly kill the production environment.
The key number for dbsize and keys * may be different, if you remember correctly, the keys * is the current DB valid key, and dbsize statistics are all not destroyed key (valid and not destroyed is not the same, you can understand the Redis expiration policy)
There is still a little distance from the need to realize today.
The number of keys that you want to specify pattern. This feature is Redis and will not be available later.
A request was made to Antirez on the GitHub, and the result was rejected, on the grounds that:
Hi, the KEYS are only intended for debugging since it are O (N) and performs a full keyspace scan. COUNT has the same problem but without the excuse of being useful for debugging ... (since can simply use REDIS-CLI keys ... | grep ...). So feature is not accepted. For your interest.
Crash ing ....
Flip through the document and see a scan command (incremental iteration command). Seems to be able to achieve function, go first.
public List<String> findKeysForPage(String patternKey, int pageNum, int pageSize) {
ScanOptions options = ScanOptions.scanOptions().match(patternKey).build();
RedisConnectionFactory factory = stringRedisTemplate.getConnectionFactory();
RedisConnection rc = factory.getConnection();
Cursor<byte[]> cursor = rc.scan(options);
List<String> result = new ArrayList<String>(pageSize);
int tmpIndex = 0;
int startIndex = (pageNum-1) * pageSize;
int end = pageNum * pageSize;
while (cursor.hasNext()) {
if (tmpIndex >= startIndex && tmpIndex <end) {
result.add(new String(cursor.next()));
tmpIndex++;
continue;
}
// After obtaining the data that meets the conditions, you can exit
if(tmpIndex >=end) {
break;
}
tmpIndex++;
cursor.next();
}
try {
// cursor.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
RedisConnectionUtils.releaseConnection(rc, factory);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
As above code:
1. Can page to get the key list
2. If you need to get the total number, remove the above method,
if (Tmpindex >=end) {break
;
}
Then the final Tmpindex value is the total.
Other than that:
May appear before the 1.6.0 version of Spring-redis-data,
Java.util.NoSuchElementException] with root cause
Java.util.NoSuchElementException
At Java.util.collections$emptyiterator.next (collections.java:4189)
At Org.springframework.data.redis.core.ScanCursor.moveNext (scancursor.java:215)
At Org.springframework.data.redis.core.ScanCursor.next (scancursor.java:202)
Here are the instructions:
https://jira.spring.io/browse/DATAREDIS-417
1.6.1 after the settlement.
But, Ohmygod, there are always but:
1. How to enter the Patternkey content returned a lot of results, and need to get the total number, then will traverse almost all key, generally our online key number in tens, then run up the speed is very scary, occupy online resources
The 2.scan command itself, because an incremental command only uses cursors to record an iteration state, the same element may be returned multiple times. Working with duplicate elements is the responsibility of the application. Consider, for example, that the elements returned by an iteration are used only for operations that can be safely repeated multiple times. If an element is added to the dataset during an iteration, or is removed from the dataset during an iteration, the element may be returned, or not, this is undefined (undefined).
Conclusion:
1. Use in production environment, be cautious, preferably only the function of developer permissions
2. It is best not to use the total, so relatively paging get the value, generally do not get many pages behind the content