Redis 批量操作获取Hash Key, Value
1. 获取批量key//模糊匹配redis keySet<String> keys = stringRedisTemplate.keys("*" + RedisKeyUtils.getAllCacheCircleUserKey(userId.toString()) + "*");2. 根据key批量获取hash value/*** 批量获取hashKey value** @param
·
1. 获取批量key
//模糊匹配redis key
Set<String> keys = stringRedisTemplate.keys("*" + RedisKeyUtils.getAllCacheCircleUserKey(userId.toString()) + "*");
2. 根据key批量获取hash value
/**
* 批量获取hashKey value
*
* @param keys
* @return
*/
public static Map<String, Map> hgetAll(StringRedisTemplate redisTemplate, Set<String> keys) {
return (Map<String, Map>) redisTemplate.execute((RedisCallback) con -> {
Iterator<String> it = keys.iterator();
Map<String, Map> mapList = new HashMap<>();
while (it.hasNext()) {
String key = it.next();
Map<byte[], byte[]> result = con.hGetAll(key.getBytes());
Map ans;
if (CollectionUtils.isEmpty(result)) {
return new HashMap<>(0);
}
ans = new HashMap<>(result.size());
for (Map.Entry entry : result.entrySet()) {
ans.put(new String((byte[]) entry.getKey()), new String((byte[]) entry.getValue()));
}
mapList.put(key, ans);
}
return mapList;
});
}
3.返回数据
使用查询key作为主键,返回hash 里面的所有key 和value 用map接收
更多推荐
已为社区贡献2条内容
所有评论(0)