SpringBoot缓存-ehcache

模拟短信发送业务

需求分析:用户发送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



    

    
    
    
    
    
    
    
    
    

    
    

7.修改qpplication.yml文件中缓存策略为ehcache:

spring:
  cache:
#    选用ehcache
#    Spring内置缓存Simple(内存级得缓存)
#    type: simple
    type: ehcache

8.postman测试:

SpringBoot缓存-ehcache_第1张图片

没到10秒过期:

SpringBoot缓存-ehcache_第2张图片

 到10秒过期之后:

SpringBoot缓存-ehcache_第3张图片

 

 

你可能感兴趣的:(springboot,缓存)