ShardedJedisPipeline中sync()和syncAndReturnAll()区别

官网解释

 通过读取所有响应来同步管道。此操作将关闭管道。为了从流水线命令获取返回值,请捕获所执行命令的不同Response <?>。

 通过读取所有响应来同步管道。此操作将关闭管道。尽可能避免使用此版本,并使用ShardedJedisPipeline.sync(),因为它不会遍历所有响应并生成正确的响应类型(通常是浪费时间)。

如果对返回顺序没有要求,建议使用sync()

附上通过管道从缓存取数据的实现:

/**
     * 通过key批量取数据
     *
     * @param keys  key列表 这里入参是缓存key,返回的map里面也用这个作为Key值
     * @param clazz 转成的对象
     * @param <T>   key对应的map数据
     * @return
     */
    @RunTimeLog
    public <T> Map<String, T> batchGetObject(List<String> keys, Class<T> clazz) {
        //创建接收对象格式
        Map<String, T> responses = Maps.newHashMap();
        Map<String, Response<String>> redisValueMap = Maps.newHashMap();
        try {
            ShardedJedisPipeline pip = redisClient.pipeline();
            keys.stream().filter(key -> CommonUtils.checkNotNullString(key)).forEach(key -> redisValueMap.put(key, pip.get(key)));
            //通过读取所有响应来同步管道。此操作将关闭管道。为了从流水线命令获取返回值,请捕获所执行命令的不同Response <?>。
            pip.sync();
            //通过读取所有响应来同步管道。此操作将关闭管道。尽可能避免使用此版本,并使用ShardedJedisPipeline.sync(),因为它不会遍历所有响应并生成正确的响应类型(通常是浪费时间)。
            //pip.syncAndReturnAll();
            redisValueMap.keySet().forEach(key -> responses.put(key, JSON.parseObject(redisValueMap.get(key).get(), clazz)));
        } catch (Exception ex) {
            LOGGER.error("error:{}", ex);
        }
        return responses;
    }
/**
* 通过key批量取数据,如果取不到就用key带到函数参数中去查,主要用于如果缓存没有,再去数据库里查
*
* @param keys key列表 这里入参是缓存key,返回的map里面也用这个作为Key值
* @param clazz 转成的对象
* @param <T> key对应的map数据
* @return
*/
public synchronized <T> Map<String, T> batchGetObject(List<String> keys, Class<T> clazz, Function<String, T> function) {
Map<String, T> responses = this.batchGetObject(keys, clazz);
List<String> delKeys= Lists.newArrayList();
try {
responses.keySet().forEach(key -> {
if (responses.get(key) == null) {
T t = function.apply(key);
if (t != null) {
responses.put(key, t);
}else{
delKeys.add(key);
}
}
});
delKeys.forEach(key->{
responses.remove(key);
});
} catch (Exception ex) {
LOGGER.error("error:{}", ex);
}
return responses;
}

调用

    /**
     * 批量根据缓存key去redis查询数据,如果没有查询到数据,再去库里查找
     * @param storeCode
     * @param categoryCodes
     * @return
     */
    public Map<String, StoreCategoryEntity> queryBatchStoreCategoryInfoByCategoryCode(String storeCode, List<String> categoryCodes) {
        Map<String, StoreCategoryEntity> map= Maps.newHashMap();
        try {
            //循环批量生成key
            List<String> keys = Lists.newArrayList();
            categoryCodes=categoryCodes.stream().distinct().collect(Collectors.toList());
            categoryCodes.forEach(categoryCode -> {
                keys.add(CacheKeyBusiness.getStoreCategoryByCategoryCodePrefixKey(storeCode, categoryCode));
            });
            map=redisClientBusiness.batchGetObject(keys,StoreCategoryEntity.class,(key)->{
                //SFSPC:STORE_CODE_CATEGORY_CODE:{storeCode}:{categoryCode}
                //实现当无法从缓存获得数据时,解析key然后通过其他途径获得数据并返回
                String[] info = key.split(":");
                if (info.length != 4) {
                    return null;
                }
          //通过数据库去加载,并返回对象 StoreCategoryEntity storeCategoryEntity
= storeCategoryDao.queryStoreCategoryByCategoryCode(storeCode, info[3]); if (storeCategoryEntity == null) { return null; } redisClientBusiness.redisSet(key, JSON.toJSONString(storeCategoryEntity)); return storeCategoryEntity; }); } catch (Exception ex) { LOGGER.error("error:{}", ex); return map; } return map; }
 
原文地址:https://www.cnblogs.com/ff111/p/14388859.html