添加缓存的逻辑的原则:缓存逻辑不能影响正常的业务逻辑执行。
使用场景一:短信验证
短信校验是常用到的一项保证安全的,短信的验证码都是随机产生的,而且信息都是有时间限制的,一旦过了那个时间的有效期,验证信息就会自动删除。
//手机短信验证码 设置一分钟过期
ValueOperations<String, String> ops = this.template.opsForValue();
String key = "phone_token";
if (!this.template.hasKey(key)) {
ops.set(key, "125639");
ops.getOperations().expire(key, 1, TimeUnit.MINUTES);
System.out.println("the key is " + key + "------ the value is " + ops.get(key));
} else {
System.out.println("exist key");
}