注意事项
在使用redis的时候,key不要有中文,之前碰到过一个bug找了一天才解决,就是key中有中文,设置了key和value,并且在10秒中之后失效,本地测试是没问题的,但是上线后失效没有成功,把key中的中文去掉之后才失效成功,本地的redis版本是2.8.9,线上的redis版本是5.0.9,以下是当时的代码
/*** 将出入场的车牌存放到redis中* @param payCarcome 出入场记录*/private void putPayCarcome2Redis(PayCarcome payCarcome){// 在redis中存放出入场记录,防止车尾被出口或入口的摄像头抓拍到生成出入场记录// 车牌号中有中文,payCarcome.getPlateNumber().substring(1)去除中文String redisKey = "payCarcome:" + payCarcome.getParkId() + ":" + payCarcome.getPlateNumber().substring(1);redisCacheService.put(redisKey, System.currentTimeMillis());LOGGER.info("将出入场记录存放到redis中,key:" + redisKey + ",当前时间:" + DateUtils.getCurrentDateStr(DateUtils.DATETIME_PATTERN));// 15秒后失效String timeOutStr = EConfig.getOtherConfigPropertiesValue("payCarcomeRedisKeyTimeOut");// 默认10秒失效int timeOut = 10;if (!StringUtil.isEmpty(timeOutStr)){timeOut = Integer.parseInt(timeOutStr);}boolean expire = redisCacheService.expire(redisKey, timeOut);if (expire){LOGGER.info("redis设置失效成功,key:" + redisKey + ",timeOut:" + timeOut);}else {LOGGER.error("redis设置失效失败,key:" + redisKey + ",timeOut:" + timeOut);}}
