From c26073afeb59ff898bb46a59031728fb99985a32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90li?= <15040126243@163.com> Date: Sun, 4 Jul 2021 02:13:24 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=20=E5=A2=9E=E5=8A=A0spring-cache=E6=BC=94?= =?UTF-8?q?=E7=A4=BA=E6=A1=88=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/application.yml | 15 ++++ .../demo/controller/RedisCacheController.java | 70 +++++++++++++++++++ .../ruoyi/framework/config/RedisConfig.java | 8 ++- .../config/properties/RedissonProperties.java | 34 ++++++++- 4 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 ruoyi-demo/src/main/java/com/ruoyi/demo/controller/RedisCacheController.java diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml index 9fec4ae7..1d3415eb 100644 --- a/ruoyi-admin/src/main/resources/application.yml +++ b/ruoyi-admin/src/main/resources/application.yml @@ -262,6 +262,21 @@ feign: circuitbreaker: enabled: true +--- # redisson 缓存配置 +redisson: + cacheGroup: + # 用例: @Cacheable(cacheNames="groupId", key="#XXX") 方可使用缓存组配置 + - groupId: redissonCacheMap + # 组过期时间(脚本监控) + ttl: 60000 + # 组最大空闲时间(脚本监控) + maxIdleTime: 60000 + # 组最大长度 + maxSize: 0 + - groupId: testCache + ttl: 1000 + maxIdleTime: 500 + --- # 分布式锁 lock4j 全局配置 lock4j: # 获取分布式锁超时时间,默认为 3000 毫秒 diff --git a/ruoyi-demo/src/main/java/com/ruoyi/demo/controller/RedisCacheController.java b/ruoyi-demo/src/main/java/com/ruoyi/demo/controller/RedisCacheController.java new file mode 100644 index 00000000..4588f138 --- /dev/null +++ b/ruoyi-demo/src/main/java/com/ruoyi/demo/controller/RedisCacheController.java @@ -0,0 +1,70 @@ +package com.ruoyi.demo.controller; + +import com.ruoyi.common.core.domain.AjaxResult; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.CachePut; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * spring-cache 演示案例 + * + * @author Lion Li + */ +// 类级别 缓存统一配置 +//@CacheConfig(cacheNames = "redissonCacheMap") +@RequiredArgsConstructor(onConstructor_ = @Autowired) +@RestController +@RequestMapping("/demo/cache") +public class RedisCacheController { + + /** + * 测试 @Cacheable + * + * 表示这个方法有了缓存的功能,方法的返回值会被缓存下来 + * 下一次调用该方法前,会去检查是否缓存中已经有值 + * 如果有就直接返回,不调用方法 + * 如果没有,就调用方法,然后把结果缓存起来 + * 这个注解「一般用在查询方法上」 + * + * cacheNames 为配置文件内 groupId + */ + @Cacheable(cacheNames = "redissonCacheMap", key = "#key", condition = "#key != null") + @GetMapping("/test1") + public AjaxResult test1(String key, String value){ + return AjaxResult.success("操作成功", value); + } + + /** + * 测试 @CachePut + * + * 加了@CachePut注解的方法,会把方法的返回值put到缓存里面缓存起来,供其它地方使用 + * 它「通常用在新增方法上」 + * + * cacheNames 为 配置文件内 groupId + */ + @CachePut(cacheNames = "redissonCacheMap", key = "#key", condition = "#key != null") + @GetMapping("/test2") + public AjaxResult test2(String key, String value){ + return AjaxResult.success("操作成功", value); + } + + /** + * 测试 @CacheEvict + * + * 使用了CacheEvict注解的方法,会清空指定缓存 + * 「一般用在更新或者删除的方法上」 + * + * cacheNames 为 配置文件内 groupId + */ + @CacheEvict(cacheNames = "redissonCacheMap", key = "#key", condition = "#key != null") + @GetMapping("/test3") + public AjaxResult test3(String key, String value){ + return AjaxResult.success("操作成功", value); + } + +} diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/RedisConfig.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/RedisConfig.java index 95c7572f..bc286574 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/RedisConfig.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/RedisConfig.java @@ -19,6 +19,7 @@ import org.springframework.context.annotation.Configuration; import java.io.IOException; import java.util.HashMap; +import java.util.List; import java.util.Map; /** @@ -78,8 +79,13 @@ public class RedisConfig extends CachingConfigurerSupport { */ @Bean public CacheManager cacheManager(RedissonClient redissonClient) { + List cacheGroup = redissonProperties.getCacheGroup(); Map config = new HashMap<>(); - config.put("redissonCacheMap", new CacheConfig(30*60*1000, 10*60*1000)); + for (RedissonProperties.CacheGroup group : cacheGroup) { + CacheConfig cacheConfig = new CacheConfig(group.getTtl(), group.getMaxIdleTime()); + cacheConfig.setMaxSize(group.getMaxSize()); + config.put(group.getGroupId(), cacheConfig); + } return new RedissonSpringCacheManager(redissonClient, config, JsonJacksonCodec.INSTANCE); } diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/RedissonProperties.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/RedissonProperties.java index 99db89ef..b6e289d8 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/RedissonProperties.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/RedissonProperties.java @@ -2,11 +2,12 @@ package com.ruoyi.framework.config.properties; import lombok.Data; import lombok.NoArgsConstructor; -import org.redisson.client.codec.Codec; import org.redisson.config.TransportMode; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; +import java.util.List; + /** * Redisson 配置属性 * @@ -37,6 +38,11 @@ public class RedissonProperties { */ private SingleServerConfig singleServerConfig; + /** + * 缓存组 + */ + private List cacheGroup; + @Data @NoArgsConstructor public static class SingleServerConfig { @@ -98,4 +104,30 @@ public class RedissonProperties { } + @Data + @NoArgsConstructor + public static class CacheGroup { + + /** + * 组id + */ + private String groupId; + + /** + * 组过期时间 + */ + private long ttl; + + /** + * 组最大空闲时间 + */ + private long maxIdleTime; + + /** + * 组最大长度 + */ + private int maxSize; + + } + }