//redis批量存值
public void batchSetListObj(List<String> keys, Object values) {
//获取redis配置序列化工具
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = (Jackson2JsonRedisSerializer<Object>) redisTemplate.getValueSerializer();
List<Object> list;
if(values instanceof List){
list = (List<Object>)values;
}else {
return;
}
// 使用管道进行批量操作
redisTemplate.executePipelined((RedisCallback<Object>) connection -> {
for (int i = 0; i < keys.size(); i++) {
String key = keys.get(i).toString();
Object value = list.get(i);
connection.set(key.getBytes(), jackson2JsonRedisSerializer.serialize(value));
}
return null;
});
}
public void batchDelete(String key,List<String> keys) {
List<String> caches = new ArrayList<>();
for (String s : keys) {
caches.add(key+"::"+s);
}
redisTemplate.delete(caches);
}
Redis管道批量存值
最新推荐文章于 2024-11-04 20:00:00 发布