pom文件
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>${ehcache.core}</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-jgroupsreplication</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>${hibernate.version}</version>
<exclusions>
<exclusion>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
</exclusion>
</exclusions>
</dependency>
1.在web文件下配置spring-cache.xml的路径
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:config/spring/spring-application.xml,
classpath:config/spring/spring-support.xml,
classpath:config/spring/spring-mybatis.xml,
classpath:config/spring/spring-cache.xml,
classpath:config/spring/spring-quartz.xml
</param-value>
</context-param>
2.配置spring-cache.xm文件
注意:spring-cache.xml 要放在resources文件下不能有子文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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/cache
http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">
<description>ehcache缓存配置管理文件</description>
<!-- 启用缓存注解开关 -->
<cache:annotation-driven cache-manager="cacheManager"/>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
<!-- 这里不加上或者改成false,项目会启动不起来,还没找到问题 -->
<property name="shared" value="true"></property>
</bean>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcache"/>
</bean>
2.配置ehcache.xml文件
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd">
<diskStore path="D:/tempCase1" />
<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="18000" timeToLiveSeconds="36000" overflowToDisk="true"
maxElementsOnDisk="10000000" diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" />
<cache name="plateform" maxElementsInMemory="200000" eternal="false"
timeToIdleSeconds="18000" timeToLiveSeconds="36000" overflowToDisk="true" />
</ehcache>
3.(1)在serviceIpml上使用注解
@Override
@Cacheable(value = "plateform", key = "'house_'+#cmutyId+'_'+ #uid")
public List<User> findUser(long cmutyId, long uid) {
System.err.println("运行数据库。。。。。");
}
(2)@CacheEvict的方法和@Cache的方法放到一个java文件中写,他俩在两个java文件的话,会导致@CacheEvict失效@CacheEvict注解的方法必须是controller层直接调用,service里间接调用不生效
参考文章:http://www.cnblogs.com/jingmoxukong/p/5975994.html