Spring Ehcache 整合

来源:互联网 发布:java web 高并发 编辑:程序博客网 时间:2024/06/15 18:36

需要的jar包

    <dependency>            <groupId>net.sf.ehcache</groupId>            <artifactId>ehcache</artifactId>            <version>2.8.2</version>        </dependency>

spring 集成Ehcache配置

    <bean id="ehcache"        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">        <property name="configLocation" value="classpath:ehcache.xml" />    </bean>    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">        <property name="cacheManager" ref="ehcache" />    </bean>    <!-- 启用缓存注解开关 -->    <cache:annotation-driven cache-manager="cacheManager" />

ehcache 内存配置:ehcache.xml

<?xml version="1.0" encoding="UTF-8"?><ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"> <!--      磁盘存储:将缓存中暂时不使用的对象,转移到硬盘,类似于Windows系统的虚拟内存     path:指定在硬盘上存储对象的路径  -->  <diskStore path="java.io.tmpdir/ehcache"/>  <!-- 默认缓存 -->  <defaultCache          maxEntriesLocalHeap="10000"          eternal="false"          timeToIdleSeconds="120"          timeToLiveSeconds="120"          maxEntriesLocalDisk="10000000"          diskExpiryThreadIntervalSeconds="120"          memoryStoreEvictionPolicy="LRU"/>  <cache name="data"         maxElementsInMemory="1000"          eternal="true"        timeToIdleSeconds="12000"          timeToLiveSeconds="12000"          maxEntriesLocalDisk="10000000"          diskExpiryThreadIntervalSeconds="120"         overflowToDisk="false"         memoryStoreEvictionPolicy="LRU"/></ehcache>

详细介绍:

  • defaultCache : 默认的缓存配置信息,如果不加特殊说明,则所有对象按照此配置项处理

  • maxElementsInMemory : 设置了缓存的上限,最多存储多少个记录对象

  • maxElementsOnDisk:在磁盘上缓存的element的最大数目,默认值为0,表示不限制

  • eternal : 对象是否永久有效,一但设置了,timeout将不起作用。

  • timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。

  • timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。

  • overflowToDisk:当内存中对象数量最大时,是否写入磁盘中

  • diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。

  • diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.

  • diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。

  • memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。

  • clearOnFlush:内存数量最大时是否清除。


简单demo

public class SpringCacheTest {    /**     * 执行缓存是,并执行方法体,重新吸入缓存     * @param key 键     * @param obj 值     * @return     */    @CachePut(value = "helloworld" ,key="#key")    public Object setCacheParam(String key,Object obj){        return obj;    }    /**     * 获取缓存在key中的参数     * key:      * @return 缓存中key 的值     */    @Cacheable(value = "helloworld" ,key="#key")    public Object getCacheParam(String key){        return null;    }    /**     * 清除缓存中key 的值     * @param key 键     */    @CacheEvict(value = "helloworld" ,key="#key")    public void clearCacheParam(String key){    }




封装的ehcache 工具类(非Spring整合)

public class CacheUtil {    private static final String path = "/ehcache.xml";    private URL url;    private CacheManager manager;    private static CacheUtil ehCache;    private CacheUtil(String path) {          url = getClass().getResource(path);          manager = CacheManager.create(url);  }    public static CacheUtil getInstance() {        if (ehCache == null) {            ehCache = new CacheUtil(path);        }        return ehCache;    }    public void put(String cacheName, String key, Object value) {        Cache cache = manager.getCache(cacheName);        Element element = new Element(key, value);        cache.put(element);    }    public Object get(String cacheName, String key) {        Cache cache = manager.getCache(cacheName);        Element element = cache.get(key);        return element == null ? null : element.getObjectValue();    }    public Cache get(String cacheName) {        return manager.getCache(cacheName);    }    public void remove(String cacheName, String key) {        Cache cache = manager.getCache(cacheName);        cache.remove(key);    }    public static void main(String[] args) {        CacheUtil cacheUtil = CacheUtil.getInstance();        cacheUtil.put("data", "dataName", "dataValue");        Object obj = cacheUtil.get("data","dataName");        System.out.println(obj);    }
原创粉丝点击