记一次redis 基于spring实现类对同一个KEY序列化内容不同导致一次事故

我们的场景是这样的 我们对一个key:比如list.point.card:1

@Resource
private RedisTemplate<String, Long> redisTemplate;

private ListOperations<String, Long> listOperations;

@Before
public void init() {
listOperations = this.redisTemplate.opsForList();
}
对KEY通过列表数据结构往右推入队列一个集合
listOperations.rightPushAll(key, Lists.newArrayList(1111L,2222L));

然后我们调用
@Autowired
private StringRedisTemplate redisTemplate;
redisTemplate.expire(key, 10,TimeUnit.SECONDS);
本来的意图是想对上面的那个key设置过期时间,但因为
RedisTemplate默认对KEY的序列化是按JKD序列化方式来的,也就是存入reids的KEY是一串乱码

 而用stringredisTemplate.expire其实是纯粹对list.point.card:1这个KEY加上了过期时间(尽管无效,因为压根没这个KEY存在),起不了对序列化之后的那个KEY起到过期时间  

引起的问题是这个KEY对应的内容一直存在。这个时候 但凡key对应的内容发送变化,你用原来的内容去业务操作,会发生问题!!!

原文地址:https://www.cnblogs.com/zhangfengshi/p/14633651.html