zoukankan      html  css  js  c++  java
  • 使用spring-data-redis做缓存

         说到缓存,首先肯定是spring-cache了.

         spring-cache使用一个CacheManager来进行管理缓存,为了使用我们的redis作为缓存源,需要向spring注册一个CacheManager

    @Bean(name = "redisCacheManager")
        public CacheManager cacheManager(@SuppressWarnings("rawtypes") RedisTemplate redisTemplate) throws IOException {
    
            RedisCacheManager manage = new RedisCacheManager(redisTemplate);
            log.info("redis 默认过期时间 {} 秒", redisDefaultExpiration);
         //这里可以设置一个默认的过期时间 manage.setDefaultExpiration(redisDefaultExpiration); manage.setExpires(
    this.loadFromConfig()); return manage; }

    具体redisTemplate请参考上一篇:http://www.cnblogs.com/lic309/p/5056248.html

      我们都知道spring-cache是不能够在注解上配置过期时间的,那么如何自己定义每个缓存的过期时间呢。

      首先有个方法叫做:setDefaultExpiration,这里可以设置一个默认的过期时间。

      其次还有一个setExpires方法:

      

    public void setExpires(Map<String, Long> expires) {
            this.expires = (expires != null ? new ConcurrentHashMap<String, Long>(expires) : null);
        }

        其接受一个Map类型,key为缓存的value,value为long

      比如在Map中put一个

      

    map.put("AccountCache",1000L);

       那么所有的AccountCache的缓存过期时间为1000秒

     

    @Cacheable(value = "AccountCache", key = "T(com.morequ.usercenter.util.ConfigurationPropertys).ACCOUNTFINDBYID+#id")
        public Account findById(long id) {
    ...
    }

       

  • 相关阅读:
    阿里云ECS linux通过rinetd 端口转发来访问内网服务
    阿里云ECS linux通过iptables 配置SNAT代理网关,实现局域网上网
    适用于CentOS6.x系统的15项优化脚本
    ELK学习笔记
    MAC OSX环境下cordova+Ionic的安装配置
    Windows下 VM12虚拟机安装OS X 10.11 和VM TOOLS
    cordova 下载更新
    android adb常用命令
    ionic实现双击返回键退出功能
    ionic ngCordova插件安装
  • 原文地址:https://www.cnblogs.com/lic309/p/5056396.html
Copyright © 2011-2022 走看看