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

spring集成Ehcache

2016-05-22 22:56 274 查看
         在配置中加上:

     <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">    

        <property name="configLocation"  value="classpath:ehcache.xml"/>   

    </bean>   

      

    <!-- 支持缓存注解 -->  

    <cache:annotation-driven cache-manager="cacheManager" />  

      

    <!-- 默认是cacheManager -->  

    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">    

        <property name="cacheManager"  ref="cacheManagerFactory"/>    

    </bean>  

加上ehcache的jar,

注解:

@Cacheable(value="Theme")取缓存Theme中的数据。

@Cacheable(value = "Token", key="#Token")  取缓存Token中,key为Token的元素

@CacheEvict(value = "Token", key="#user.token") 删除缓存Token中,key为user.token的元素

Ehcache配置:

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

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

    

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

    <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="30" timeToLiveSeconds="30" overflowToDisk="false"/>  

  

    <cache name="Token"           

    maxElementsInMemory="10000"           

    eternal="true"          

    overflowToDisk="false"           

    timeToIdleSeconds="0"           

    timeToLiveSeconds="600"          

    memoryStoreEvictionPolicy="LFU" />

</ehcache>

对于mybatis的使用ehcache,在xml中加入

<!--mybatis ehcache缓存配置 -->

    <!-- 以下两个<cache>标签二选一,第一个可以输出日志(以及缓存命中率),第二个不输出日志 -->

     <cache type="org.mybatis.caches.ehcache.LoggingEhcache" />

     <cache type="org.mybatis.caches.ehcache.EhcacheCache"/> 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: