您的位置:首页 > 编程语言 > Java开发

EhCache与spring整合

2014-03-21 17:31 211 查看
ehcache.xml 

<?xml version="1.0" encoding="UTF-8"?>

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

    xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"  

    monitoring="autodetect">

    

    <diskStore path="java.io.tmpdir"/>

    

    <defaultCache

        maxElementsInMemory="10000"

        eternal="false"

        timeToIdleSeconds="120"

        timeToLiveSeconds="1200"

        maxElementsOnDisk="10000000"

        overflowToDisk="true"

        memoryStoreEvictionPolicy="LRU"/>

     <!-- Service层缓存-->

     <cache

      name="SERVICE_CACHE"

      maxElementsInMemory="10000"

        eternal="false"

        timeToIdleSeconds="120"

        timeToLiveSeconds="1200"

        maxElementsOnDisk="10000000"

        overflowToDisk="true"

        memoryStoreEvictionPolicy="LRU"/>

     <!-- Dao层缓存 -->

     <cache

      name="DAO_CACHE"

      maxElementsInMemory="10000"

        eternal="false"

        timeToIdleSeconds="120"

        timeToLiveSeconds="1200"

        maxElementsOnDisk="10000000"

        overflowToDisk="true"

        memoryStoreEvictionPolicy="LRU"/>

</ehcache>

spring配置文件

<!-- spring 缓存配置注解驱动 支持@Cacheable等 -->
<cache:annotation-driven cache-manager="applicationCacheManager"  />
<bean id="applicationCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcache" />
</bean>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
</bean>

java类中

@Cacheable(value="SERVICE_CACHE",key="as")
public List<Course> getCourse(int count) {
try {
return courseDao.getCourse(count);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

架包引入注意必须是sping3.1及以上
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: