SpringBoot Redis 设置缓存过期时间

avatar 2018年05月24日04:17:53 6 22590 views
博主分享免费Java教学视频,B站账号:Java刘哥
RedisConfig.java
  1. @Configuration
  2. //@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 3600 * 12)//最大过期时间
  3. @EnableCaching
  4. public class RedisConfig {
  5.     @Bean
  6.     public CacheManager cacheManager(RedisTemplate redisTemplate) {
  7.         RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
  8.         //设置缓存过期时间
  9.         Map<String, Long> expires = new HashMap<>();
  10.         expires.put("12h"3600 * 12L);
  11.         expires.put("1h"3600 * 1L);
  12.         expires.put("10m"60 * 10L);
  13.         rcm.setExpires(expires);
  14. //        rcm.setDefaultExpiration(60 * 60 * 12);//默认过期时间
  15.         return rcm;
  16.     }
  17. }
    UserServiceImpl.java
  1. @Cacheable(value = "12h", key = "#root.methodName")
  2.  @Override
  3.    public List<User> getUserArticleRank() {
  4.        //获得排行榜前10名的用户,每12小时刷新一次
  5.        return userRepository.findTop10ByArticleSize();
  6.    }
  通过 redis 客户端查看过期时间  
  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0