使用spring-data-redis作缓存

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

     spring-cache使用一个CacheManager来进行管理缓存,为了使用咱们的redis做为缓存源,须要向spring注册一个CacheManagerredis

@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.htmlspring

  咱们都知道spring-cache是不可以在注解上配置过时时间的,那么如何本身定义每一个缓存的过时时间呢。缓存

  首先有个方法叫作:setDefaultExpiration,这里能够设置一个默认的过时时间。this

  其次还有一个setExpires方法:spa

  

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

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

  好比在Map中put一个htm

  

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

   那么全部的AccountCache的缓存过时时间为1000秒blog

 

@Cacheable(value = "AccountCache", key = "T(com.morequ.usercenter.util.ConfigurationPropertys).ACCOUNTFINDBYID+#id")
    public Account findById(long id) {        ...    }
相关文章
相关标签/搜索