疯狂的狮子li
2021-07-04 c26073afeb59ff898bb46a59031728fb99985a32
update 优化缓存配置 增加spring-cache演示案例
已修改3个文件
已添加1个文件
127 ■■■■■ 文件已修改
ruoyi-admin/src/main/resources/application.yml 15 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-demo/src/main/java/com/ruoyi/demo/controller/RedisCacheController.java 70 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-framework/src/main/java/com/ruoyi/framework/config/RedisConfig.java 8 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/RedissonProperties.java 34 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-admin/src/main/resources/application.yml
@@ -262,6 +262,21 @@
  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 æ¯«ç§’
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<String> 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<String> 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<String> test3(String key, String value){
        return AjaxResult.success("操作成功", value);
    }
}
ruoyi-framework/src/main/java/com/ruoyi/framework/config/RedisConfig.java
@@ -19,6 +19,7 @@
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
@@ -78,8 +79,13 @@
     */
    @Bean
    public CacheManager cacheManager(RedissonClient redissonClient) {
        List<RedissonProperties.CacheGroup> cacheGroup = redissonProperties.getCacheGroup();
        Map<String, CacheConfig> 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);
    }
ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/RedissonProperties.java
@@ -2,10 +2,11 @@
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 é…ç½®å±žæ€§
@@ -36,6 +37,11 @@
     * å•机服务配置
     */
    private SingleServerConfig singleServerConfig;
    /**
     * ç¼“存组
     */
    private List<CacheGroup> cacheGroup;
    @Data
    @NoArgsConstructor
@@ -98,4 +104,30 @@
    }
    @Data
    @NoArgsConstructor
    public static class CacheGroup {
        /**
         * ç»„id
         */
        private String groupId;
        /**
         * ç»„过期时间
         */
        private long ttl;
        /**
         * ç»„最大空闲时间
         */
        private long maxIdleTime;
        /**
         * ç»„最大长度
         */
        private int maxSize;
    }
}