注意事项

在使用redis的时候,key不要有中文,之前碰到过一个bug找了一天才解决,就是key中有中文,设置了key和value,并且在10秒中之后失效,本地测试是没问题的,但是上线后失效没有成功,把key中的中文去掉之后才失效成功,本地的redis版本是2.8.9,线上的redis版本是5.0.9,以下是当时的代码

  1. /**
  2. * 将出入场的车牌存放到redis中
  3. * @param payCarcome 出入场记录
  4. */
  5. private void putPayCarcome2Redis(PayCarcome payCarcome){
  6. // 在redis中存放出入场记录,防止车尾被出口或入口的摄像头抓拍到生成出入场记录
  7. // 车牌号中有中文,payCarcome.getPlateNumber().substring(1)去除中文
  8. String redisKey = "payCarcome:" + payCarcome.getParkId() + ":" + payCarcome.getPlateNumber().substring(1);
  9. redisCacheService.put(redisKey, System.currentTimeMillis());
  10. LOGGER.info("将出入场记录存放到redis中,key:" + redisKey + ",当前时间:" + DateUtils.getCurrentDateStr(DateUtils.DATETIME_PATTERN));
  11. // 15秒后失效
  12. String timeOutStr = EConfig.getOtherConfigPropertiesValue("payCarcomeRedisKeyTimeOut");
  13. // 默认10秒失效
  14. int timeOut = 10;
  15. if (!StringUtil.isEmpty(timeOutStr)){
  16. timeOut = Integer.parseInt(timeOutStr);
  17. }
  18. boolean expire = redisCacheService.expire(redisKey, timeOut);
  19. if (expire){
  20. LOGGER.info("redis设置失效成功,key:" + redisKey + ",timeOut:" + timeOut);
  21. }else {
  22. LOGGER.error("redis设置失效失败,key:" + redisKey + ",timeOut:" + timeOut);
  23. }
  24. }