ehcache缓存问题

来源:互联网 发布:sql 截断字符串 编辑:程序博客网 时间:2024/04/29 01:27

步骤:

1.ehcache.xml

<?xml version="1.0" encoding="gbk"?><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"/>    <!--         配置自定义缓存        maxElementsInMemory:缓存中允许创建的最大对象数        eternal:缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期。        timeToIdleSeconds:缓存数据的钝化时间,也就是在一个元素消亡之前,                    两次访问时间的最大时间间隔值,这只能在元素不是永久驻留时有效,                    如果该值是 0 就意味着元素可以停顿无穷长的时间。        timeToLiveSeconds:缓存数据的生存时间,也就是一个元素从构建到消亡的最大时间间隔值,                    这只能在元素不是永久驻留时有效,如果该值是0就意味着元素可以停顿无穷长的时间。        overflowToDisk:内存不足时,是否启用磁盘缓存。        memoryStoreEvictionPolicy:缓存满了之后的淘汰算法。     注意:一个ehcache.xml中不是只能写一个cache标签,可以根据缓存的对象不同而写多个cache,其他name为其标识位。    -->    <cache name="SimplePageCachingFilter"         maxElementsInMemory="10000"         eternal="false"        overflowToDisk="false"         timeToIdleSeconds="900"         timeToLiveSeconds="1800"        memoryStoreEvictionPolicy="LFU" /></ehcache>

参数说明
name:缓存名称,配置缓存时,通过名称来调用缓存;
maxElementsOnDisk:磁盘中最大缓存的对象数,值为0表示无穷大;
maxElementsInMemory:内存中最大缓存对象数;
eternal:是否不过期,值为true表示永不过期,默认值为false;
timeToLiveSeconds:设置对象允许存在于缓存中的最长时间;
overflowToDisk:当缓存对象数达到了maxElementsInMemory设置的最大值以后,是否允许把溢出的对象写入到磁盘中;
diskSpoolBufferSizeMB:磁盘缓存区大小,默认值30MB;
timeToldleSeconds:设置允许对象处于空闲状态的最长时间,当对象离上次被访问的时间间隔超过了timeToldleSeconds属性值时,
这个对象就会过期,EhCache就会把它从缓存中清空;
memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,EhCache将会根据指定的策略去清理内存,可选策略有:
LRU(最近最少使用原则)/FIFO(先进先出)/LFU(最少访问次数)


2.spring-ehcache.xml(要配置到web.xml中)

<?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:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p"xmlns:cache="http://www.springframework.org/schema/cache"xsi:schemaLocation="http://www.springframework.org/schema/beans                          http://www.springframework.org/schema/beans/spring-beans-4.1.xsd                          http://www.springframework.org/schema/context                          http://www.springframework.org/schema/context/spring-context-4.1.xsd                        http://www.springframework.org/schema/cache                         http://www.springframework.org/schema/cache/spring-cache-4.1.xsd "><!-- Spring EHCACHE 缓存配置 --><cache:annotation-driven cache-manager="ehcacheCacheManager" /><bean id="ehcacheCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cacheManager-ref="ehcacheManager" /><bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"><property name="configLocation" value="classpath:ehcache.xml" /></bean> </beans>





3.在需要缓存的类中进行注解



0 0