现在的位置: 首页 > 综合 > 正文

spring配置ehchace缓存

2018年04月21日 ⁄ 综合 ⁄ 共 2151字 ⁄ 字号 评论关闭

今天在看ehcache的用法, 怕忘记, 先记录下来

1. 现在bean中配置ehcache.xml文件的路径

	<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
		<property name="configLocation">
			<value>ehcache.xml</value>
		</property>
	</bean>

2. 配置ehcache的工厂

        <bean id="cache1" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
                <property name="cacheManager">
                    <ref local="cacheManager"/>
                </property>
                <property name="cacheName">
                        <value>cache1</value>
                </property>
         </bean>

在这里的cacheName的属性值, 在ehcache中有对应的配置

3. 配置ehcache.xml文件

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd">
    <!--配置缓存在磁盘上的存储位置-->
    <diskStore path="java.io.tmpdir"/>

    <!--暂未学习-->
    <cacheManagerEventListenerFactory class="" properties=""/>

    <!--暂未学习-->
    <cacheManagerPeerListenerFactory
            class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"/>

    <!--默认的缓存配置, 如果cachaName的属性值在这里没有找到对应的name,
        那么将使用默认的缓存配置-->
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="true"
            maxElementsOnDisk="10000000"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"
            />

    <cache name="cache1"
           maxElementsInMemory="10000"
           maxElementsOnDisk="1000"
           eternal="false"
           overflowToDisk="true"
           timeToIdleSeconds="20"
           timeToLiveSeconds="20"
           memoryStoreEvictionPolicy="LFU"
            />
</ehcache>

cache中的属性如下: 

maxElementsInMemory: 指定内存中存放的最大的缓存对象的数量

maxElementsOnDisk: 指定磁盘中存放的最大的的缓存对象的数量

overflowToDisk: 指定当内存达到上限的时候是否能溢出到磁盘中, 默认为true

timeToIdleSeconds: (暂未弄明白和下面的区别)

timeToLiveSeconds:(暂未弄明白和上面的区别)

memoryStoreEvictionPolicy: 如果达到了maxElementsInMemory的上限, 那么此属性用来指定清理原来的元素的策略. 有3种策略, LRU(最近最少使用), FIFO(先进先出), 或LFU(较少使用),
默认为LRU

eternal: 如果为true, 那么元素将永久不会过期. 默认为false

diskPeisistent: 这个还没弄懂什么意思

diskExpiryThreadIntervalSeconds: 磁盘清理缓存线程的时间间隔. 默认为120秒(未弄懂)

本次学习只是简单的学了一些ehcache的配置, 其中的内容是看了网上的文章加上现有的代码后进行的简单概括总结, 并未深入的了解其中的原理, 也没有有真正实现的代码, 但是还是要先记下来, 其中有些还是没弄懂的, 待以后再继续补充. 

以下是一些文章的地址, 可以他们写得更好

http://blog.csdn.net/hz_chenwenbiaoTMB/article/details/5755450

http://blog.csdn.net/java000/article/details/2697892

http://blog.csdn.net/yangfanend/article/details/7661885

http://chenjumin.iteye.com/blog/684926

http://raychase.iteye.com/blog/1545906

好了, 也算是第一篇文章, 心情有点沉重....



抱歉!评论已关闭.