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

@@ -0,0 +1,72 @@
package com.tiesheng.util.service;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.json.JSONUtil;
import com.tiesheng.util.ServletKit;
import com.tiesheng.util.exception.ApiException;
import com.tiesheng.util.service.cache.TsCacheHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class TsCacheService {
@Autowired
TsCacheHandler tsCacheHandler;
/**
* 获取一个缓存
*
* @return
*/
public static TsCacheService of() {
return SpringUtil.getBean(TsCacheService.class);
}
public void put(String key, String value) {
tsCacheHandler.put(key, value);
}
public void put(String key, String value, long timeout) {
tsCacheHandler.put(key, value, timeout);
}
public void putObj(String key, Object value, long timeout) {
tsCacheHandler.put(key, JSONUtil.toJsonStr(value), timeout);
}
public String get(String key) {
return tsCacheHandler.get(key);
}
public String get(String key, long timeout) {
return tsCacheHandler.get(key, timeout);
}
public <T> T getObj(String key, Class<T> tClass, long timeout) {
return JSONUtil.toBean(get(key, timeout), tClass, true);
}
public void remove(String key) {
tsCacheHandler.remove(key);
}
///////////////////////////////////////////////////////////////////////////
// 图形验证码
///////////////////////////////////////////////////////////////////////////
/**
* 验证 图片验证码
*/
public void verifyCaptcha(String value) {
String captchaKey = ServletUtil.getHeader(ServletKit.getRequest(), "captcha", "utf-8");
String cache = get(captchaKey);
if (StrUtil.isEmpty(cache) || !StrUtil.equals(cache, value)) {
throw new ApiException("验证码不正确");
}
remove(captchaKey);
}
}