when a job need to update alot redis k/v
use normal command loop is low efficiency plan
that cause tcp connection time-wait exceed several ten thousand.
so we use redis pipline operation merge command connection.
Use RedisTemplate
read obj
Set User
class deserializer first
1 | private static final Jackson2JsonRedisSerializer userSerializer = new Jackson2JsonRedisSerializer(User.class); |
pip execute multi get command
1 | List<User> result = redisTemplate.executePipelined(new RedisCallback<List<User>>() { |
Use RedisTemplate
write obj
Same as above use User
class serializer first
1 | private static final Jackson2JsonRedisSerializer userSerializer = new Jackson2JsonRedisSerializer(User.class); |
pip execute multi set command
1 | redisTemplate.executePipelined(new RedisCallback<List>() { |
Caution
connection
return is byte array, it need deserializer method.doInRedis
return must be null, the return value has been take over byexecutePipelined
.1
2
3if (result != null)
throw new InvalidDataAccessApiUsageException(
"Callback cannot return a non-null value as it gets overwritten by the pipeline");don’t call
connection.closePipeline()
, it will return result and no deserialize.- Deserialize need deserializer object. like these
https://blog.csdn.net/xiaoliu598906167/article/details/82218525
https://blog.csdn.net/huilixiang/article/details/19484921
https://blog.csdn.net/xiaolyuh123/article/details/78682200
https://my.oschina.net/yuyidi/blog/499951
https://www.cnblogs.com/EasonJim/p/7803067.html#autoid-2-6-0