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,25 +0,0 @@
package com.tiesheng.util;
import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
public class TimedCacheHelper {
private static volatile TimedCache<String, String> timedCache;
/**
* 获取一个默认2分钟过期的缓存
*/
public static TimedCache<String, String> getTimedCache() {
if (timedCache == null) {
synchronized (TimedCacheHelper.class) {
if (timedCache == null) {
timedCache = CacheUtil.newTimedCache(2 * 60 * 1000);
timedCache.schedulePrune(1000);
}
}
}
return timedCache;
}
}

View File

@@ -8,8 +8,8 @@ import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.symmetric.SM4;
import com.tiesheng.util.ServletKit;
import com.tiesheng.util.TimedCacheHelper;
import com.tiesheng.util.exception.ApiException;
import com.tiesheng.util.service.TsCacheService;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@@ -104,12 +104,12 @@ public class EncryptConfig {
String salt = decrypt(encrypted).substring(0, saltSize);
String inputEncrypted = passwdCreate(inputPasswd, salt);
if (!StrUtil.equals(inputEncrypted, encrypted)) {
String clientIp = ServletKit.getClientIP();
int num = NumberUtil.parseInt(TimedCacheHelper.getTimedCache().get(clientIp, false));
String clientIp = "passwdVerify_" + ServletKit.getClientIP();
int num = NumberUtil.parseInt(TsCacheService.of().get(clientIp, -1));
if (num > 5) {
throw new ApiException("登录失败已达6次请10分钟后再试");
}
TimedCacheHelper.getTimedCache().put(clientIp, String.valueOf(num + 1), 10 * 60 * 1000);
TsCacheService.of().put(clientIp, String.valueOf(num + 1), 10 * 60 * 1000);
throw new ApiException("账号或密码错误");
}
}

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

View File

@@ -0,0 +1,17 @@
package com.tiesheng.util.service.cache;
public interface TsCacheHandler {
String name();
void put(String key, String value);
void put(String key, String value, long timeout);
String get(String key);
String get(String key, long timeout);
void remove(String key);
}

View File

@@ -0,0 +1,56 @@
package com.tiesheng.util.service.cache;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
@Configuration
@ConditionalOnProperty("spring.redis.url")
public class TsCacheRedisHandler implements TsCacheHandler {
@Resource
RedisTemplate<String, String> redisTemplate;
@Override
public String name() {
return "RedisCache";
}
@Override
public void put(String key, String value) {
redisTemplate.opsForValue().set(key, value);
}
@Override
public void put(String key, String value, long timeout) {
redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.MICROSECONDS);
}
@Override
public String get(String key) {
return redisTemplate.opsForValue().get(key);
}
@Override
public String get(String key, long timeout) {
if (timeout > 0) {
return redisTemplate.opsForValue().getAndExpire(key, timeout, TimeUnit.MICROSECONDS);
} else if (timeout == 0) {
return redisTemplate.opsForValue().getAndDelete(key);
}
return redisTemplate.opsForValue().get(key);
}
@Override
public void remove(String key) {
redisTemplate.delete(key);
}
}

View File

@@ -0,0 +1,67 @@
package com.tiesheng.util.service.cache;
import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnMissingBean(value = TsCacheHandler.class, ignored = {TsCacheTimedHandler.class, TsCacheTimedHandler.class})
public class TsCacheTimedHandler implements TsCacheHandler {
private static volatile TimedCache<String, String> timedCache;
/**
* 获取一个默认2分钟过期的缓存
*/
private static TimedCache<String, String> getTimedCache() {
if (timedCache == null) {
synchronized (TsCacheTimedHandler.class) {
if (timedCache == null) {
timedCache = CacheUtil.newTimedCache(2 * 60 * 1000);
timedCache.schedulePrune(1000);
}
}
}
return timedCache;
}
@Override
public String name() {
return "TimedCache";
}
@Override
public void put(String key, String value) {
getTimedCache().put(key, value);
}
@Override
public void put(String key, String value, long timeout) {
getTimedCache().put(key, value, timeout);
}
@Override
public String get(String key) {
return getTimedCache().get(key);
}
@Override
public String get(String key, long timeout) {
String value = getTimedCache().get(key, false);
if (timeout > 0) {
getTimedCache().put(key, value, timeout);
} else if (timeout == 0) {
getTimedCache().remove(key);
}
return value;
}
@Override
public void remove(String key) {
getTimedCache().remove(key);
}
}