项目中可使用redis pipeline优化批量redis操作。
下面的程序判断批量redisKey是否存在:
public List<Object> hasRedisKey(List<String> redisKeyList) { try { List<Object> resultList = redisTemplate.executePipelined(new SessionCallback<List<Objects>>() { @Override public List<Objects> execute(RedisOperations operations) throws DataAccessException { for (String redisKey : redisKeyList) { operations.hasKey(redisKey); } return null; } }); return resultList; } catch (Exception e) { log.error("hasRedisKey() error, param: {}", redisKeyList, e); } return Lists.newArrayList(); }
redis数据库中存在以下key:
入参和返回结果如下:
如果希望入参命令和响应结果一一对应,可使用如下方式:
public void jedisPipelined() { JedisConnection jedisConnection = (JedisConnection) redisTemplate.getConnectionFactory().getConnection(); Jedis nativeConnection = jedisConnection.getNativeConnection(); Pipeline pipelined = nativeConnection.pipelined(); Response<Boolean> existsKey1 = pipelined.exists("1"); Response<Boolean> existsKey2 = pipelined.exists("2"); Response<Boolean> existsKey3 = pipelined.exists("3"); Response<Boolean> existsKey4 = pipelined.exists("4"); Response<Boolean> existsKey5 = pipelined.exists("5"); Response<Boolean> existsKey6 = pipelined.exists("6"); Response<Boolean> existsKey7 = pipelined.exists("7"); Response<Boolean> existsKey8 = pipelined.exists("8"); Response<Boolean> existsKey9 = pipelined.exists("9"); Response<Boolean> existsKey10 = pipelined.exists("10"); pipelined.sync(); Boolean result1 = existsKey1.get(); Boolean result2 = existsKey2.get(); Boolean result3 = existsKey3.get(); Boolean result4 = existsKey4.get(); Boolean result5 = existsKey5.get(); Boolean result6 = existsKey6.get(); Boolean result7 = existsKey7.get(); Boolean result8 = existsKey8.get(); Boolean result9 = existsKey9.get(); Boolean result10 = existsKey10.get(); }
程序运行结果:
参考文档:https://help.aliyun.com/zh/redis/use-cases/use-pipelining-to-batch-issue-commands
标签:exists,get,redis,Redis,管道,pipelined,Response,Boolean From: https://www.cnblogs.com/NguyenVm/p/17665485.html