zl程序教程

您现在的位置是:首页 >  其他

当前栏目

RedisTemplate批量获取值

批量 获取 RedisTemplate
2023-06-13 09:14:21 时间

multiGet方式

    /**
     * 同时获取redis多个key值
     * @author www.itze.cn
     **/
    public List<Object> mGetTypeGetValue(Set matchKey) {
        try {
            return redisTemplate.opsForValue().multiGet(matchKey);
        } catch (Exception e) {
            log.info("异常:", e);
            e.printStackTrace();
        }
        return null;
    }

plpeline方式,推荐使用该方法

    /**
     * 批量获取key值对应的Value
     * @author www.itze.cn
     * @param matchKey
     * @return
     */
    public List<Object> pipeLineTypeGetValue(Set<String> matchKey) {
        return redisTemplate.executePipelined((RedisCallback<Object>) connection -> {
            StringRedisConnection conn = (StringRedisConnection) connection;
            for (String key : matchKey) {
                conn.get(key);
            }
            return null;
        });
    }

提示

plpeline方式共享一个连接,查询返回的结果,和键的顺序是一一对应的,如果没查到,会返回null值 可以结合文章:RedisTemplate使用Redis scan批量获取Redis key方法使用