模拟短信发送业务
需求分析:用户发送get请求,请求参数为电话号码,响应验证码并缓存(10秒),获得验证码,然后用户发送post请求,参数为电话号和验证码,业务层对比缓存验证码和用户发送过来的是否一致,一致则响应true,不一致或者缓存中为空(过了10秒自动清空)则响应false
启动类
package cn.itchen;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching//必须加
public class SpringbootcacheApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootcacheApplication.class, args);
}
}
1.创建模块,勾选Spring web,Lombok
2.创建实体
package cn.itchen.domain;
import lombok.Data;
/**
* @author 张家琛
* @version 1.0
* @date 2022/4/30 22:58
*/
@Data
public class SMSCode {
private String tele;
private String code;
}
.3.创建SMSController
package cn.itchen.controller;
import cn.itchen.domain.SMSCode;
import cn.itchen.service.SMSService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* @author 张家琛
* @version 1.0
* @date 2022/4/30 22:28
*/
@RestController
@RequestMapping("/sms")
public class SMSController {
@Autowired
private SMSService service;
@GetMapping
public String getCode(String tele){
return service.getCodeToSMS(tele);
}
@PostMapping
public boolean checkCode(SMSCode smsCode){
return service.checkCode(smsCode);
}
}
4.创建SMSService(文中忽略接口只写实现)
package cn.itchen.service.impl;
import cn.itchen.domain.SMSCode;
import cn.itchen.service.SMSService;
import cn.itchen.utils.Encryption;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
/**
* @author 张家琛
* @version 1.0
* @date 2022/4/30 22:30
*/
@Service
public class SMSServiceImpl implements SMSService {
@Autowired
private Encryption encryption;
@Override
// 存进去,然后每次都取
// @Cacheable(value = "cacheSpace",key = "#tele")
// 缓存位置对应到excache.xml的非默认配置中的name=cacheSpace
@CachePut(value = "cacheSpace",key = "#tele") // 只存不取
public String getCodeToSMS(String tele) {
return encryption.getCode(tele);
}
@Override
public boolean checkCode(SMSCode smsCode) {
String code=smsCode.getCode();
// 必须得code.equals,反过来写equals不行,因为encryption.get 10秒后清缓存拿到的数据为空
// ,而null是不能进行null.equals(..)的
return code.equals(encryption.get(smsCode.getTele()));
}
}
5.然后工具类
Encryption:
package cn.itchen.utils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
/**
* @author 张家琛
* @version 1.0
* @date 2022/4/30 22:31
*/
@Component
public class Encryption {
//简易加密算法
public String getCode(String tele) {
long encryptionNum = 1298556;
long firstResult = tele.hashCode() ^ encryptionNum;
long secondResult = firstResult ^ System.currentTimeMillis();
long result=secondResult%1000000>0?secondResult%1000000:-secondResult%1000000;
return String.format("%06d",result);
}
// 这套东西要想生效,必须走Spring处理(现在在Component类中)
@Cacheable(value = "cacheSpace",key = "#tele")
public String get(String tele){
return null;
}
}
6.导入ehcache专用配置
ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
<diskStore path="D:\ehcache" />
<!--默认缓存策略 -->
<!-- external:是否永久存在,设置为true则不会被清除,此时与timeout冲突,通常设置为false-->
<!-- diskPersistent:是否启用磁盘持久化-->
<!-- maxElementsInMemory:最大缓存数量-->
<!-- overflowToDisk:超过最大缓存数量是否持久化到磁盘-->
<!-- timeToIdleSeconds:最大不活动间隔,设置过长缓存容易溢出,设置过短无效果,可用于记录时效性数据,例如验证码-->
<!-- timeToLiveSeconds:最大存活时间-->
<!-- memoryStoreEvictionPolicy:缓存清除策略-->
<defaultCache
eternal="false"
diskPersistent="false"
maxElementsInMemory="1000"
overflowToDisk="false"
timeToIdleSeconds="60"
timeToLiveSeconds="60"
memoryStoreEvictionPolicy="LRU" />
<cache
name="cacheSpace"
eternal="false"
diskPersistent="false"
maxElementsInMemory="1000"
overflowToDisk="false"
timeToIdleSeconds="10"
timeToLiveSeconds="10"
memoryStoreEvictionPolicy="LRU" />
<!-- 设置10秒钟之后缓存过期-->
</ehcache>
7.修改qpplication.yml文件中缓存策略为ehcache:
spring:
cache:
# 选用ehcache
# Spring内置缓存Simple(内存级得缓存)
# type: simple
type: ehcache
8.postman测试:
没到10秒过期:
到10秒过期之后: