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

@@ -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);
}
}