From 8ea34c3ee0c4336125163cbb33107997599b00da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E6=96=87=E8=B1=AA?= <980287353@qq.com> Date: Thu, 25 Jul 2024 17:35:01 +0800 Subject: [PATCH] =?UTF-8?q?perf=EF=BC=9A=E7=A7=BB=E9=99=A4TimedCacheServic?= =?UTF-8?q?e=E3=80=81TimedCacheHelper=EF=BC=8C=E6=96=B0=E5=A2=9ETsCacheSer?= =?UTF-8?q?vice=EF=BC=8C=E5=90=8C=E6=97=B6=E5=AE=9E=E7=8E=B0=E4=BA=86?= =?UTF-8?q?=E5=86=85=E5=AD=98=E7=BC=93=E5=AD=98=E3=80=81Redis=E7=BC=93?= =?UTF-8?q?=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- springboot-ademo/pom.xml | 6 ++ .../demo/controller/TestController.java | 21 +++-- .../src/main/resources/application-test.yml | 2 + .../config/ding/PlatformDingConfig.java | 11 +-- .../config/wxmp/PlatformWxmpConfig.java | 13 +-- springboot-util/pom.xml | 11 +++ .../com/tiesheng/util/TimedCacheHelper.java | 25 ------ .../tiesheng/util/config/EncryptConfig.java | 8 +- .../tiesheng/util/service/TsCacheService.java | 72 ++++++++++++++++ .../util/service/cache/TsCacheHandler.java | 17 ++++ .../service/cache/TsCacheRedisHandler.java | 56 +++++++++++++ .../service/cache/TsCacheTimedHandler.java | 67 +++++++++++++++ .../web/controller/ToolController.java | 8 +- .../web/service/TimedCacheService.java | 82 ------------------- 14 files changed, 267 insertions(+), 132 deletions(-) delete mode 100644 springboot-util/src/main/java/com/tiesheng/util/TimedCacheHelper.java create mode 100644 springboot-util/src/main/java/com/tiesheng/util/service/TsCacheService.java create mode 100644 springboot-util/src/main/java/com/tiesheng/util/service/cache/TsCacheHandler.java create mode 100644 springboot-util/src/main/java/com/tiesheng/util/service/cache/TsCacheRedisHandler.java create mode 100644 springboot-util/src/main/java/com/tiesheng/util/service/cache/TsCacheTimedHandler.java delete mode 100644 springboot-web/src/main/java/com/tiesheng/web/service/TimedCacheService.java diff --git a/springboot-ademo/pom.xml b/springboot-ademo/pom.xml index f9aab08..4be6889 100644 --- a/springboot-ademo/pom.xml +++ b/springboot-ademo/pom.xml @@ -36,6 +36,12 @@ easyexcel + + redis.clients + jedis + 3.7.0 + + diff --git a/springboot-ademo/src/main/java/com/tiesheng/demo/controller/TestController.java b/springboot-ademo/src/main/java/com/tiesheng/demo/controller/TestController.java index 7844f05..2ebbd36 100644 --- a/springboot-ademo/src/main/java/com/tiesheng/demo/controller/TestController.java +++ b/springboot-ademo/src/main/java/com/tiesheng/demo/controller/TestController.java @@ -9,23 +9,25 @@ import com.alibaba.excel.EasyExcel; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.read.listener.ReadListener; import com.tiesheng.annotation.token.TokenIgnore; -import com.tiesheng.util.pojos.TokenBean; -import com.tiesheng.web.service.CoreLogService; -import com.tiesheng.web.service.CoreMessageService; -import com.tiesheng.web.util.ProcessImportConsumer; import com.tiesheng.database.config.DbBackupConfig; import com.tiesheng.demo.pojos.PoiBean; import com.tiesheng.demo.pojos.TestFile; -import com.tiesheng.util.config.TsTokenConfig; import com.tiesheng.message.pojos.MessageReqResp; import com.tiesheng.message.pojos.UserChannel; import com.tiesheng.util.config.EncryptConfig; import com.tiesheng.util.config.GlobalConfig; import com.tiesheng.util.config.Ip2regionConfig; +import com.tiesheng.util.config.TsTokenConfig; import com.tiesheng.util.pojos.ApiResp; import com.tiesheng.util.pojos.FileUploadPath; +import com.tiesheng.util.service.TsCacheService; +import com.tiesheng.web.service.CoreLogService; +import com.tiesheng.web.service.CoreMessageService; +import com.tiesheng.web.util.ProcessImportConsumer; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletResponse; import java.io.File; @@ -53,6 +55,8 @@ public class TestController { CoreLogService coreLogService; @Autowired DbBackupConfig dbBackupConfig; + @Autowired + TsCacheService tsCacheService; @RequestMapping("/index") @@ -100,8 +104,13 @@ public class TestController { @RequestMapping("/export") + @TokenIgnore public ApiResp export() { +// tsCacheService.put("fdfhdfd", "fdfdfdfd"); + String s = tsCacheService.get("fdfhdfd"); + LogFactory.get().info("export: " + s); + // List list = new ArrayList<>(); // list.add(new TestFile("11111")); // list.add(new TestFile("22222")); diff --git a/springboot-ademo/src/main/resources/application-test.yml b/springboot-ademo/src/main/resources/application-test.yml index 1946203..a33c08c 100644 --- a/springboot-ademo/src/main/resources/application-test.yml +++ b/springboot-ademo/src/main/resources/application-test.yml @@ -7,6 +7,8 @@ spring: url: jdbc:mysql://47.96.30.85:3306/com_tiesheng_web?useSSL=false&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&queryTimeout=5400&allowMultiQueries=true&serverTimezone=GMT%2B8 username: com_tiesheng_web password: 4Xo$XheGFc + redis: + url: redis://kyF0zUL3011111@47.96.30.85:6234 platform: wxmp: diff --git a/springboot-platform/src/main/java/com/tiesheng/platform/config/ding/PlatformDingConfig.java b/springboot-platform/src/main/java/com/tiesheng/platform/config/ding/PlatformDingConfig.java index dfb8eaf..f827773 100644 --- a/springboot-platform/src/main/java/com/tiesheng/platform/config/ding/PlatformDingConfig.java +++ b/springboot-platform/src/main/java/com/tiesheng/platform/config/ding/PlatformDingConfig.java @@ -10,8 +10,8 @@ import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import cn.hutool.log.LogFactory; import com.tiesheng.platform.config.ding.bean.*; -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; @@ -94,7 +94,7 @@ public class PlatformDingConfig { */ private String getAccessToken(String service) { DingConfigBean dingConfigBean = getConfigBean(service); - String accessToken = TimedCacheHelper.getTimedCache().get(CACHE_ACCESS_TOKEN + dingConfigBean.getAppKey(), false); + String accessToken = TsCacheService.of().get(CACHE_ACCESS_TOKEN + dingConfigBean.getAppKey(), -1); if (!StrUtil.isEmpty(accessToken)) { return accessToken; } @@ -106,7 +106,8 @@ public class PlatformDingConfig { String response = HttpUtil.get("https://oapi.dingtalk.com/gettoken", query); JSONObject respJson = JSONUtil.parseObj(response); accessToken = respJson.getStr("access_token"); - TimedCacheHelper.getTimedCache().put(CACHE_ACCESS_TOKEN + dingConfigBean.getAppKey(), accessToken, respJson.getLong("expires_in")); + TsCacheService.of().put(CACHE_ACCESS_TOKEN + dingConfigBean.getAppKey(), + accessToken, respJson.getLong("expires_in")); return accessToken; } @@ -120,7 +121,7 @@ public class PlatformDingConfig { */ private String getJsapiTicket(String service) { DingConfigBean dingConfigBean = getConfigBean(service); - String jsapiTicket = TimedCacheHelper.getTimedCache().get(CACHE_JSAPI_TICKET + dingConfigBean.getAppKey(), false); + String jsapiTicket = TsCacheService.of().get(CACHE_JSAPI_TICKET + dingConfigBean.getAppKey(), -1); if (StrUtil.isEmpty(jsapiTicket)) { DingResponse respJson = doRequest(service, "https://oapi.dingtalk.com/get_jsapi_ticket", null, new TypeReference>() { @@ -129,7 +130,7 @@ public class PlatformDingConfig { throw new ApiException(respJson.getErrmsg()); } jsapiTicket = respJson.getTicket(); - TimedCacheHelper.getTimedCache().put(CACHE_JSAPI_TICKET + dingConfigBean.getAppKey(), + TsCacheService.of().put(CACHE_JSAPI_TICKET + dingConfigBean.getAppKey(), jsapiTicket, respJson.getExpiresIn()); } return jsapiTicket; diff --git a/springboot-platform/src/main/java/com/tiesheng/platform/config/wxmp/PlatformWxmpConfig.java b/springboot-platform/src/main/java/com/tiesheng/platform/config/wxmp/PlatformWxmpConfig.java index 93a5aa5..018eb62 100644 --- a/springboot-platform/src/main/java/com/tiesheng/platform/config/wxmp/PlatformWxmpConfig.java +++ b/springboot-platform/src/main/java/com/tiesheng/platform/config/wxmp/PlatformWxmpConfig.java @@ -12,8 +12,8 @@ import com.tiesheng.platform.config.wxmp.bean.WxConfigBean; import com.tiesheng.platform.config.wxmp.bean.WxJsapiSignature; import com.tiesheng.platform.config.wxmp.bean.WxOAuth2AccessToken; import com.tiesheng.platform.config.wxmp.bean.WxUserInfo; -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; @@ -63,17 +63,17 @@ public class PlatformWxmpConfig { */ private String getAccessToken(String service) { WxConfigBean configBean = getConfigBean(service); - String accessToken = TimedCacheHelper.getTimedCache().get(CACHE_ACCESS_TOKEN + configBean.getAppId(), false); + String accessToken = TsCacheService.of().get(CACHE_ACCESS_TOKEN + configBean.getAppId(), -1); if (StrUtil.isEmpty(accessToken)) { Map query = new HashMap<>(10); query.put("grant_type", "client_credential"); query.put("appid", configBean.getAppId()); query.put("secret", configBean.getAppSecret()); String response = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token", query); - LogFactory.get().info("getAccessToken: " + response); JSONObject respJson = JSONUtil.parseObj(response); accessToken = respJson.getStr("access_token"); - TimedCacheHelper.getTimedCache().put(CACHE_ACCESS_TOKEN + configBean.getAppId(), accessToken, respJson.getLong("expires_in")); + TsCacheService.of().put(CACHE_ACCESS_TOKEN + configBean.getAppId(), accessToken, + respJson.getLong("expires_in")); } return accessToken; } @@ -86,7 +86,7 @@ public class PlatformWxmpConfig { */ private String getJsapiTicket(String service) { WxConfigBean configBean = getConfigBean(service); - String jsapiTicket = TimedCacheHelper.getTimedCache().get(CACHE_JSAPI_TICKET + configBean.getAppId(), false); + String jsapiTicket = TsCacheService.of().get(CACHE_JSAPI_TICKET + configBean.getAppId(), -1); if (StrUtil.isEmpty(jsapiTicket)) { Map query = new HashMap<>(10); query.put("access_token", getAccessToken(service)); @@ -95,7 +95,8 @@ public class PlatformWxmpConfig { LogFactory.get().info("getJsapiTicket: " + response); JSONObject respJson = JSONUtil.parseObj(response); jsapiTicket = respJson.getStr("ticket"); - TimedCacheHelper.getTimedCache().put(CACHE_JSAPI_TICKET + configBean.getAppId(), jsapiTicket, respJson.getLong("expires_in")); + TsCacheService.of().put(CACHE_JSAPI_TICKET + configBean.getAppId(), jsapiTicket, + respJson.getLong("expires_in")); } return jsapiTicket; } diff --git a/springboot-util/pom.xml b/springboot-util/pom.xml index 2d79ddb..3e6fc21 100644 --- a/springboot-util/pom.xml +++ b/springboot-util/pom.xml @@ -68,6 +68,17 @@ 1.68 + + org.springframework.boot + spring-boot-starter-data-redis + + + io.lettuce + lettuce-core + + + + diff --git a/springboot-util/src/main/java/com/tiesheng/util/TimedCacheHelper.java b/springboot-util/src/main/java/com/tiesheng/util/TimedCacheHelper.java deleted file mode 100644 index 8fc8c4f..0000000 --- a/springboot-util/src/main/java/com/tiesheng/util/TimedCacheHelper.java +++ /dev/null @@ -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 timedCache; - - /** - * 获取一个默认2分钟过期的缓存 - */ - public static TimedCache getTimedCache() { - if (timedCache == null) { - synchronized (TimedCacheHelper.class) { - if (timedCache == null) { - timedCache = CacheUtil.newTimedCache(2 * 60 * 1000); - timedCache.schedulePrune(1000); - } - } - } - return timedCache; - } - -} diff --git a/springboot-util/src/main/java/com/tiesheng/util/config/EncryptConfig.java b/springboot-util/src/main/java/com/tiesheng/util/config/EncryptConfig.java index 0439d66..3f276cc 100644 --- a/springboot-util/src/main/java/com/tiesheng/util/config/EncryptConfig.java +++ b/springboot-util/src/main/java/com/tiesheng/util/config/EncryptConfig.java @@ -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("账号或密码错误"); } } diff --git a/springboot-util/src/main/java/com/tiesheng/util/service/TsCacheService.java b/springboot-util/src/main/java/com/tiesheng/util/service/TsCacheService.java new file mode 100644 index 0000000..0ba73dd --- /dev/null +++ b/springboot-util/src/main/java/com/tiesheng/util/service/TsCacheService.java @@ -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 getObj(String key, Class 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); + } + +} diff --git a/springboot-util/src/main/java/com/tiesheng/util/service/cache/TsCacheHandler.java b/springboot-util/src/main/java/com/tiesheng/util/service/cache/TsCacheHandler.java new file mode 100644 index 0000000..83e53af --- /dev/null +++ b/springboot-util/src/main/java/com/tiesheng/util/service/cache/TsCacheHandler.java @@ -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); + +} diff --git a/springboot-util/src/main/java/com/tiesheng/util/service/cache/TsCacheRedisHandler.java b/springboot-util/src/main/java/com/tiesheng/util/service/cache/TsCacheRedisHandler.java new file mode 100644 index 0000000..691f2dc --- /dev/null +++ b/springboot-util/src/main/java/com/tiesheng/util/service/cache/TsCacheRedisHandler.java @@ -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 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); + } + + +} diff --git a/springboot-util/src/main/java/com/tiesheng/util/service/cache/TsCacheTimedHandler.java b/springboot-util/src/main/java/com/tiesheng/util/service/cache/TsCacheTimedHandler.java new file mode 100644 index 0000000..b189a3b --- /dev/null +++ b/springboot-util/src/main/java/com/tiesheng/util/service/cache/TsCacheTimedHandler.java @@ -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 timedCache; + + /** + * 获取一个默认2分钟过期的缓存 + */ + private static TimedCache 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); + } + + +} diff --git a/springboot-web/src/main/java/com/tiesheng/web/controller/ToolController.java b/springboot-web/src/main/java/com/tiesheng/web/controller/ToolController.java index e1b2447..8966601 100644 --- a/springboot-web/src/main/java/com/tiesheng/web/controller/ToolController.java +++ b/springboot-web/src/main/java/com/tiesheng/web/controller/ToolController.java @@ -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 diff --git a/springboot-web/src/main/java/com/tiesheng/web/service/TimedCacheService.java b/springboot-web/src/main/java/com/tiesheng/web/service/TimedCacheService.java deleted file mode 100644 index 0457e2e..0000000 --- a/springboot-web/src/main/java/com/tiesheng/web/service/TimedCacheService.java +++ /dev/null @@ -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); - } - - -}