Redis命令级别实现:
SCAN cursor [MATCH pattern] [COUNT count]
Spring-Data-Redis实现:
@Override
public List<Map<Object, Object>> getAllRedisVal1() {
List<Map<Object, Object>> list = new ArrayList<>();
//key("*") 获取所有键
Set<String> keys = redisTemplate.keys("*");
for (String key : keys) {
HashMap<Object, Object> map = new HashMap<>();
//String类型的键值获取
if(redisTemplate.type(key).code()=="string") {
Object value = redisTemplate.opsForValue().get(key);
map.put("key", key);
map.put("value", value);
list.add(map);
}
//zset类型的键值获取
if(redisTemplate.type(key).code()=="zset"){
Object value=redisTemplate.opsForZSet().range(key,0,-1);
map.put("key", key);
map.put("value", value);
list.add(map);
}
//set类型的键值获取
if(redisTemplate.type(key).code()=="set"){
Object value=redisTemplate.opsForZSet().range(key,0,-1);
map.put("key", key);
map.put("value", value);
list.add(map);
}
//list类型的键值获取
if(redisTemplate.type(key).code()=="list"){
Object value=redisTemplate.opsForZSet().range(key,0,-1);
map.put("key", key);
map.put("value", value);
list.add(map);
}
//hash类型的键值获取
if(redisTemplate.type(key).code()=="hash"){
Object value=redisTemplate.opsForZSet().range(key,0,-1);
map.put("key", key);
map.put("value", value);
list.add(map);
}
}
return list;
}
改进keys("*")的方案:
@Override
public List<Map<Object, Object>> getAllRedisVal2(Integer pageSize, Integer pageNumber) {
List<Map<Object, Object>> list = new ArrayList<>();
int startIndex=pageSize*(pageNumber-1)+1;
//使用scan的方法
Map<Object,Object> result = (Map<Object, Object>) redisTemplate.execute(new RedisCallback() {
@Nullable
@Override
public Object doInRedis(RedisConnection redisConnection) throws DataAccessException {
Cursor<byte[]> results = redisConnection.scan(new ScanOptions.ScanOptionsBuilder().match("*").build());
List<String> keys = new LinkedList<>();
Map<Object,Object> map=new HashMap<>();
try {
int index = 0;
while (results.hasNext()) {
if(startIndex<=index&&index<pageSize+startIndex) {
keys.add(new String(results.next(), "UTF-8"));
}
else results.next();
index++;
}
map.put("count",index);
map.put("keys",keys);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return map;
}
});
List<String> keys= (List<String>) result.get("keys");
for (String key : keys) {
HashMap<Object, Object> map = new HashMap<>();
if (redisTemplate.type(key).code() == "string") {
Object value = redisTemplate.opsForValue().get(key);
map.put("key", key);
map.put("value", value);
list.add(map);
} else if (redisTemplate.type(key).code() == "zset") {
Object value = redisTemplate.opsForZSet().range(key, 0, -1);
map.put("key", key);
map.put("value", value);
list.add(map);
} else if (redisTemplate.type(key).code() == "set") {
Object value = redisTemplate.opsForSet().members(key);
map.put("key", key);
map.put("value", value);
list.add(map);
} else if (redisTemplate.type(key).code() == "list") {
Object value = redisTemplate.opsForList().range(key, 0, -1);
map.put("key", key);
map.put("value", value);
list.add(map);
} else if (redisTemplate.type(key).code() == "hash") {
Object value = redisTemplate.opsForHash().entries(key);
map.put("key", key);
map.put("value", value);
list.add(map);
}
else if(redisTemplate.type(key).code()=="none"){
Object value = redisTemplate.opsForHash().entries(key);
map.put("key", key);
map.put("value", value);
list.add(map);
}
}
Map<Object,Object> map=new HashMap<>();
map.put("count",result.get("count"));
list.add(map);
return list;
}
Jedis方案
待添加。。。。。。
这些方法还是未解决在reids数据量较多时候的查询速度问题,后期会继续添加优化方案
参考资料: