perf:移除TimedCacheService、TimedCacheHelper,新增TsCacheService,同时实现了内存缓存、Redis缓存

This commit is contained in:
曾文豪
2024-07-25 17:35:01 +08:00
parent d357fa7c85
commit 8ea34c3ee0
14 changed files with 267 additions and 132 deletions

View File

@@ -5,12 +5,12 @@ import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import com.tiesheng.annotation.token.TokenIgnore;
import com.tiesheng.util.service.TsCacheService;
import com.tiesheng.web.config.template.ToolTemplateHandler;
import com.tiesheng.web.pojos.dto.*;
import com.tiesheng.web.pojos.vo.TemplateInfoVO;
import com.tiesheng.web.pojos.vo.PicVerifyVo;
import com.tiesheng.web.service.FileUploadService;
import com.tiesheng.web.service.TimedCacheService;
import com.tiesheng.util.exception.ApiException;
import com.tiesheng.util.pojos.ApiResp;
import org.springframework.beans.factory.annotation.Autowired;
@@ -31,7 +31,7 @@ import java.util.stream.Collectors;
public class ToolController {
@Autowired
TimedCacheService timedCacheService;
TsCacheService tsCacheService;
@Autowired
FileUploadService fileUploadService;
@Autowired
@@ -51,7 +51,7 @@ public class ToolController {
PicVerifyVo vo = new PicVerifyVo();
vo.setBase64(lineCaptcha.getImageBase64Data());
vo.setKey(IdUtil.simpleUUID());
timedCacheService.setImageCode(vo.getKey(), lineCaptcha.getCode());
tsCacheService.put(vo.getKey(), lineCaptcha.getCode());
return ApiResp.respOK(vo);
}
@@ -113,7 +113,7 @@ public class ToolController {
/**
* 合并文件
*
* @param fileMd5
* @param dto
* @return
*/
@TokenIgnore

View File

@@ -1,82 +0,0 @@
package com.tiesheng.web.service;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.ServletUtil;
import com.tiesheng.util.ServletKit;
import com.tiesheng.util.TimedCacheHelper;
import com.tiesheng.util.exception.ApiException;
import org.springframework.stereotype.Service;
/**
* @author hao
* @ProjectName cmcc
* @Copyright Hangzhou ShuoChuang Technology Co.,Ltd All Right Reserved
* @Description 这里是对文件的描述
* @data 2019-06-19
* @note 这里写文件的详细功能和改动
* @note
*/
@Service
public class TimedCacheService {
/**
* 清空数据
*
* @param key
*/
public void clearByKey(String key) {
TimedCacheHelper.getTimedCache().remove(key);
}
/**
* 放入缓存
*
* @param key
* @param val
*/
public void putCache(String key, String val) {
TimedCacheHelper.getTimedCache().put(key, val);
}
/**
* 获取缓存
*
* @param key
* @return
*/
public String getCache(String key) {
return TimedCacheHelper.getTimedCache().get(key);
}
///////////////////////////////////////////////////////////////////////////
// 图片验证码缓存
///////////////////////////////////////////////////////////////////////////
/**
* 缓存 图片验证码
*
* @param session
* @param value
*/
public void setImageCode(String captcha, String value) {
putCache(captcha, value);
}
/**
* 验证 图片验证码
*/
public void verifyImage(String value) {
String captchaKey = ServletUtil.getHeader(ServletKit.getRequest(), "captcha", "utf-8");
String cache = getCache(captchaKey);
if (StrUtil.isEmpty(cache) || !StrUtil.equals(cache, value)) {
throw new ApiException("验证码不正确");
}
clearByKey(captchaKey);
}
}