http://blog.csdn.net/tonytfjing/article/details/39251507
http://my.oschina.net/duoduo3369/blog/173924
http://blog.csdn.net/jadyer/article/details/12257865
1 maven pom.xml
<!-- spring ehcache 整合 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.10.0</version> </dependency> <dependency> <groupId>com.googlecode.ehcache-spring-annotations</groupId> <artifactId>ehcache-spring-annotations</artifactId> <version>1.2.0</version> </dependency>
2 ehcache.xml
<?xml version="1.0" encoding="UTF-8"?> <ehcache dynamicConfig="false" monitoring="off" updateCheck="false" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd"> <!-- 定义缓存策略 eternal="false" // 元素是否永恒,如果是就永不过期(必须设置) maxEntriesLocalHeap="1000" // 堆内存中最大缓存对象数,0没有限制(必须设置) overflowToDisk="false" // 当缓存达到maxElementsInMemory值是,是否允许溢出到磁盘(必须设置) diskPersistent="false" // 磁盘缓存在VM重新启动时是否保持(默认为false) timeToIdleSeconds="0" // 导致元素过期的访问间隔(秒为单位). 当eternal为false时,这个属性才有效,0表示可以永远空闲,默认为0 timeToLiveSeconds="600" // 元素在缓存里存在的时间(秒为单位). 0 表示永远存在不过期 memoryStoreEvictionPolicy="LFU" // 当达到maxElementsInMemory时,如何强制进行驱逐默认使用"最近使用(LRU)"策略,其它还有先入先出FIFO,最少使用LFU,较少使用LRU --> <defaultCache eternal="false" maxEntriesLocalHeap="0" timeToIdleSeconds="30" timeToLiveSeconds="30" /> <cache name="myCache" maxEntriesLocalHeap="1000" timeToIdleSeconds="30" timeToLiveSeconds="30" /> </ehcache>
3 spring-cache.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd" > <!-- 缓存配置 --> <!-- 启用缓存注解功能(请将其配置在Spring主配置文件中) --> <cache:annotation-driven cache-manager="cacheManager"/> <!-- Spring自己的基于java.util.concurrent.ConcurrentHashMap实现的缓存管理器(该功能是从Spring3.1开始提供的) <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager"> <property name="caches"> <set> <bean name="myCache" class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"/> </set> </property> </bean> --> <!-- 若只想使用Spring自身提供的缓存器,则注释掉下面的两个关于Ehcache配置的bean,并启用上面的SimpleCacheManager即可 --> <!-- Spring提供的基于的Ehcache实现的缓存管理器 value="classpath:ehcache.xml" src目录下 value="/WEB-INF/config/other/ehcache.xml" --> <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:shared="true"> <property name="configLocation" value="/WEB-INF/classes/config/other/ehcache.xml" /> </bean> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" > <property name="cacheManager" ref="cacheManagerFactory" /> </bean> </beans>
4 spring4 注解使用
@Service("KpiDailyService") public class KpiDailyServiceImpl implements KpiDailyService { Logger log = Logger.getLogger(KpiDailyServiceImpl.class); @Autowired NewkpidailyMapper mapper; @Override @Cacheable(value="myCache", key="#id") public Newkpidaily getKpiDailyById(String id) { // TODO Auto-generated method stub log.info(" invoke getKpiDailyById ;id="+id); return mapper.selectByPrimaryKey(id); } }