Redis 批量操作获取Hash Key, Value

1. 获取批量key

//模糊匹配redis key
Set keys = stringRedisTemplate.keys("*" + RedisKeyUtils.getAllCacheCircleUserKey(userId.toString()) + "*");

2. 根据key批量获取hash value

    /**
     * 批量获取hashKey value
     *
     * @param keys
     * @return
     */
    public static Map hgetAll(StringRedisTemplate redisTemplate, Set keys) {
        return (Map) redisTemplate.execute((RedisCallback) con -> {
            Iterator it = keys.iterator();
            Map mapList = new HashMap<>();
            while (it.hasNext()) {
                String key = it.next();
                Map 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接收

你可能感兴趣的:(redis)