EhCache

ehcache 是优秀的基于硬盘策略的缓存技术。其优点 详见百度。。。这里 只是说明 ehcahe的简单使用已经基于spring-cache 的集成。

首先需要ehcache的配置文件:

<ehcache updateCheck="false">
    <diskStore path="./ehcache"/>
    <defaultCache
            maxElementsInMemory="500"
            maxElementsOnDisk="0"
            eternal="false"
            overflowToDisk="false"
            diskPersistent="false"
            timeToIdleSeconds="60"
            timeToLiveSeconds="0"
            diskSpoolBufferSizeMB="50"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"
            />

    <cache name="HttpClient"
           maxElementsInMemory="300"
           maxElementsOnDisk="0"
           eternal="false"
           overflowToDisk="false"
           diskPersistent="false"
           timeToIdleSeconds="60"
           timeToLiveSeconds="0"
           diskSpoolBufferSizeMB="50"
           diskExpiryThreadIntervalSeconds="120"
           memoryStoreEvictionPolicy="LRU"
            />
</ehcache>

通过CacheManager 生成实例初始化 后 调用相关API 如:getCache(),cache.get(key)获取相关的缓存集合或者缓存集合中的缓存元素element。


spring-cache 有默认的缓存机制 如下配置即可:

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

如果需要集成ehcache 则只需要设置cacheManager 属性cacheManager

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
     <property name="cacheManager">
         <bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
             <property name="configLocation" value="classpath:ehcache-spring.xml"/>
         </bean>
     </property>
 </bean>

当然要想缓存真正生效还需要添加注解(注意xmlschema中需要声明cache的定义路径)

<cache:annotation-driven/>

通过相关设置  只需要使用@Cacheable 等spring提供的接口即可使用相关缓存,其他注解请百度查询


转载于:https://my.oschina.net/ehomeud/blog/548706

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值