使用spring cacheManager配置Guava Cache和Redis Cache
来源:互联网 发布:分析英语句子成分软件 编辑:程序博客网 时间:2024/06/10 23:44
简单两步,利用spring的cacheManager配置Guava Cache,同时还可以配置以redis实现的cache。使用时通过配置自由切换不同的cache实现。
1、创建配置类:
/** * Cache配置類,用于缓存方法返回的数据 * @author XuJijun * */@Configuration@EnableCachingpublic class CacheConfig { public static final int DEFAULT_MAXSIZE = 50000; public static final int DEFAULT_TTL = 10; /** * 定義cache名稱、超時時長(秒)、最大size * 每个cache缺省10秒超时、最多缓存50000条数据,需要修改可以在构造方法的参数中指定。 */ public enum Caches{ messageContent(8640000), getSomeData, qiniuUpToken(1800, 1), getCommonAds(60), getAndAssembleAreaSpecificAds(60); Caches() { } Caches(int ttl) { this.ttl = ttl; } Caches(int ttl, int maxSize) { this.ttl = ttl; this.maxSize = maxSize; } private int maxSize=DEFAULT_MAXSIZE; //最大數量 private int ttl=DEFAULT_TTL; //过期时间(秒) public int getMaxSize() { return maxSize; } public void setMaxSize(int maxSize) { this.maxSize = maxSize; } public int getTtl() { return ttl; } public void setTtl(int ttl) { this.ttl = ttl; } } /** * 创建基于guava的Cache Manager * @return */ @Bean @Primary public CacheManager guavaCacheManager() { SimpleCacheManager cacheManager = new SimpleCacheManager(); //把各个cache注册到cacheManager中,GuavaCache实现了org.springframework.cache.Cache接口 ArrayList<GuavaCache> caches = new ArrayList<GuavaCache>(); for(Caches c : Caches.values()){ caches.add(new GuavaCache(c.name(), CacheBuilder.newBuilder().recordStats().expireAfterWrite(c.getTtl(), TimeUnit.SECONDS).maximumSize(c.getMaxSize()).build())); } cacheManager.setCaches(caches); return cacheManager; } @Autowired private JedisCluster jedisCluster; /** * 创建基于redis的Cache Manager * @return */ @Bean public CacheManager redisCacheManager() { JedisClusterCacheManager cacheManager = new JedisClusterCacheManager(jedisCluster); ArrayList<JedisClusterCache> caches = new ArrayList<JedisClusterCache>(); //把各个cache注册到cacheManager中,JedisClusterCache实现了org.springframework.cache.Cache接口 for(Caches c: Caches.values()){ caches.add(new JedisClusterCache(c.name(), jedisCluster, c.getTtl())); } cacheManager.setCaches(caches); return cacheManager; }}
2、使用:
/** * 根据模块获取有效的、通用的广告列表 * * @author XuJijun * @param mType 广告模块 * @return */ @Override @Cacheable("getCommonAds") public List<Advert> getCommonAds(int mType) { return advertDAO.getAdvertsByModuleType(mType); }
注:@Cacheable注解里面指定了value=cacheName,这个注解的其他主要参数:
- cacheManager:指定由哪个cacheManager(比如可以指定为"redisCacheManager")来管理这个cache,不指定的话使用spring注解@Primary的那个;
- key:指定key的SpEL表达式,不指定的话,使用方法的参数作为key;
- GuavaCache需要依赖Guava的jar包:
<dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>19.0</version></dependency>
1 0
- 使用spring cacheManager配置Guava Cache和Redis Cache
- 使用spring cacheManager配置Guava Cache和Redis Cache
- spring结合redis实现缓存,主要是cache和cachemanager,建议看看源码,确实很好用
- spring guava cache
- Spring Cache + Redis配置备忘
- guava cache使用例子
- Guava Cache使用笔记
- Guava Cache使用
- guava cache使用例子
- Guava Cache使用笔记
- guava Cache使用心得
- guava cache 使用
- Guava cache简单使用
- google Guava Cache使用
- Guava Cache使用笔记
- 对Guava Cache的封装和使用
- spring redis cache使用思考
- spring+redis(redis当cache使用)
- 会场安排问题 [活动安排问题(一个地点最多容纳的活动个数)]
- Python:数据结构
- Opencv Mat与Iplimage的相互转换
- HashMap HashTable ConcurrentMap
- saltstack
- 使用spring cacheManager配置Guava Cache和Redis Cache
- 五子棋危险判断 - 图与连线
- 2016-4-26 linux 分布式框架以及相关模型
- 第14章css样式设置小技巧
- iOS 静态库
- yii2使用 db log
- iOS 简单实用的圆形进度条
- tomcat 设置启动参数(linux 和 windows)
- MVC中的KendoGrid里的一种传值方式