zoukankan      html  css  js  c++  java
  • SpringBoot @Cacheable Redis 设置缓存过期时间

    1.x 设置
    @Bean
    @Primary
    public CacheManager cacheManager(RedisTemplate redisTemplate) {
    RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate);

    Map<String, Long> expires = new HashMap<>();

    expires.put("timeout", 60L);

    // 设置超时
    // 根据特定名称设置有效时间
    redisCacheManager.setExpires(expires);

    // 设置默认的时间
    redisCacheManager.setDefaultExpiration(cacheDefaultExpiration);

    return redisCacheManager;

    }
    使用方式:
    转载:传送门


    @Configuration
    //@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 3600 * 12)//最大过期时间
    @EnableCaching
    public class RedisConfig {
    @Bean
    public CacheManager cacheManager(RedisTemplate redisTemplate) {
    RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
    //设置缓存过期时间
    Map<String, Long> expires = new HashMap<>();
    expires.put("12h", 3600 * 12L);
    expires.put("1h", 3600 * 1L);
    expires.put("10m", 60 * 10L);
    rcm.setExpires(expires);
    // rcm.setDefaultExpiration(60 * 60 * 12);//默认过期时间
    return rcm;
    }
    }


    //----------------------------------------------------------

    @Cacheable(value = "12h", key = "#root.methodName")
    @Override
    public List<User> getUserArticleRank() {
    //获得排行榜前10名的用户,每12小时刷新一次
    return userRepository.findTop10ByArticleSize();
    }
    2.x 设置
    转载:传送门

    /**
    * 2.XX版本的配置
    *
    */
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory factory) {
    RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig(); // 生成一个默认配置,通过config对象即可对缓存进行自定义配置
    config = config.entryTtl(Duration.ofMinutes(2)) // 设置缓存的默认过期时间,也是使用Duration设置
    .disableCachingNullValues(); // 不缓存空值

    // 设置一个初始化的缓存空间set集合
    Set<String> cacheNames = new HashSet<>();
    cacheNames.add("catalog_test_id");
    cacheNames.add("catalog_test_name");

    // 对每个缓存空间应用不同的配置
    Map<String, RedisCacheConfiguration> configMap = new HashMap<>();
    configMap.put("catalog_test_id", config);
    configMap.put("catalog_test_name", config.entryTtl(Duration.ofMinutes(5)));

    RedisCacheManager cacheManager = RedisCacheManager.builder(factory) // 使用自定义的缓存配置初始化一个cacheManager
    .initialCacheNames(cacheNames) // 注意这两句的调用顺序,一定要先调用该方法设置初始化的缓存名,再初始化相关的配置
    .withInitialCacheConfigurations(configMap)
    .build();
    return cacheManager;
    }
    @CacheConfig(cacheNames = "catalog_test_name")
    public class SsoCache{
    @Cacheable(keyGenerator = "wiselyKeyGenerator")
    public String getTokenByGsid(String gsid)
    }

    -------------------------------------
    使用(name中增加“#”,后面是过期时间,不加则走默认时间)

    @Cacheable(cacheNames = "catalog_test_name#120", unless = "#result==null")
    public UserEntity findUserByUserName(String userName) {
    return userRepository.findUserByUserName(userName);
    }

     转自:https://blog.csdn.net/qq_28114159/article/details/106098180

  • 相关阅读:
    LeetCode:Validate Binary Search Tree
    二叉树的非递归遍历(非递归使用栈、非递归不使用栈)
    scala 基础语法
    scala VS python2 (linux or shell)
    web压力测试工具
    Elasticsearch cluster health: yellow unassigned shards
    GC overhead limit exceeded
    linux定时任务的设置
    linux CPU占用率高(转)
    JQuery Sparkline 说明文档
  • 原文地址:https://www.cnblogs.com/javalinux/p/14358511.html
Copyright © 2011-2022 走看看