redis 存取问题

      今天在写短信接口时候,要把验证码存到缓存里面。因为之前别人已经写的有案例,按照之前写的,获取 值、存到数据库,存到redis。 因为有过期时间,需要传过期时间。但是怎么都是不出来。。。 

     源码:

@Override
public String sendRandomCode(SmsDto smsDto, Integer seconds) throws Exception {
String code = RandomUtils.generateNumberString(6);
String content = code + " :为您的随机验证码";
logger.info(smsDto.getMobile() + " 的随机验证码: " + code);
String smsParams2 = smsParams.replace("{mobile}", smsDto.getMobile()).replace("{content}", paraTo16(content));
Map<String, Object> result = getSend(smsUrl, smsParams2);
// 保存发送日志
Sms sms = this.getSmsLog(smsDto, content);
JSONObject jsonObject = new JSONObject();
if (MSGConstant.FALSE.equals(result.get("result"))) {
jsonObject.put("msg", "发送失败");
jsonObject.put("code", "102");
return jsonObject.toString();
}
sms.setSendStatus("Y");
smsService.saveSmsLog(sms);
// 加入缓存
System.out.println(RedisKeysUtil.common + smsDto.getMobile());
cacheClient.add(RedisKeysUtil.common + smsDto.getMobile(), code, StringUtils.isEmpty(seconds + "") ? 600 : seconds);
// 加入缓存计算限制发送间隔
cacheClient.add(RedisKeysUtil.msg_length + smsDto.getMobile(), "不允许发送", 120);
jsonObject.put("msg", "发送成功");
jsonObject.put("code", "0");
return jsonObject.toString();
}


-----------------------------------------------------------------------------------------------


觉得没什么问题啊,最后 发现 原来是 传到redis的时间 是2、、、 2s后过期,redis是以秒为单位的。 这个小问题。。。
原文地址:https://www.cnblogs.com/thinkingandworkinghard/p/6429944.html