缓存管理器
Spring 3.1内置了五个缓存管理器实现,如下所示:
- SimpleCacheManager
- NoOpCacheManager
- ConcurrentMapCacheManager
- CompositeCacheManager
- EhCacheCacheManager
Spring 3.2引入了另外一个缓存管理器,这个管理器可以用在基于
JCache(JSR-107)的缓存提供商之中。除了核心的Spring框架,
Spring Data又提供了两个缓存管理器:
- RedisCacheManager(来自于Spring Data Redis项目)
- GemfireCacheManager(来自于Spring Data GemFire项目)
在为Spring的缓存抽象选择缓存管理器时,我们有很
多可选方案。具体选择哪一个要取决于想要使用的底层缓存供应商。
每一个方案都可以为应用提供不同风格的缓存
使用Ehcache缓存
Ehcache是最为流行的缓存供应商之一。Ehcache网站上说它是“Java领
域应用最为广泛的缓存”。鉴于它的广泛采用,Spring提供集成
Ehcache的缓存管理器是很有意义的。这个缓存管理器也就
是EhCacheCacheManager。
package com.lf.config;
import net.sf.ehcache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
/**
* Created by LF on 2017/5/9.
*/
@Configuration
@EnableCaching
public class MyEhCacheManage {
@Bean
public EhCacheCacheManager getCaCheManage(CacheManager cm) {
return new EhCacheCacheManager(cm);
}
@Bean