Ehcache

作者: 米刀灵 | 来源:发表于2016-08-03 17:47 被阅读304次

添加依赖

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

需要添加仓库才能找到ehcache依赖

<repositories>
    ...
    <repository>
        <id>jboss-maven2-release-repository</id>
        <url>https://repository.jboss.org/nexus/content/groups/developer/ </url>
    </repository>
</repositories>

在src/mian/resource添加ehcache.xml

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

    <diskStore path="java.io.tmpdir" />
    <!--- <diskStore path="d:\\ehcache"/> --->
    
    <cache name="movieFindCache" 
        maxEntriesLocalHeap="10000"
        maxEntriesLocalDisk="1000" 
        eternal="false" 
        diskSpoolBufferSizeMB="20"
        timeToIdleSeconds="300" timeToLiveSeconds="600"
        memoryStoreEvictionPolicy="LFU" 
        transactionalMode="off">
        <persistence strategy="localTempSwap" />
    </cache>
</ehcache>
<!--  
name:Cache的唯一标识
maxElementsInMemory:内存中最大缓存对象数
maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大
eternal:Element是否永久有效,一但设置了,timeout将不起作用
overflowToDisk:配置此属性,当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中 
timeToIdleSeconds:设置Element在失效前的允许闲置时间。仅当element不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大 
timeToLiveSeconds:设置Element在失效前允许存活时间。最大时间介于创建时间和失效时间之间。仅当element不是永久有效时使用,默认是0.,也就是element存活时间无穷大  
diskPersistent:是否缓存虚拟机重启期数据  
diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒 
diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区 
memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)  
-->

先看一个单独使用的例子,之后和spring结合

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class HelloEhCache{
    public static void main(String[] args) {
        //1. Create a cache manager
        //CacheManager cm = CacheManager.create();使用默认配置文件创建 
        CacheManager cm = CacheManager.create("/ehcache.xml");
        
        //cm.addCache("movieFindCache2");设置一个名为movieFindCache2的新cache,属性为默认 
        
        //2. Get a cache called "movieFindCache", declared in ehcache.xml
        Cache cache = cm.getCache("movieFindCache");
        
        //3. Put few elements in cache
        cache.put(new Element("1","Jan"));
        cache.put(new Element("2","Feb"));
        cache.put(new Element("3","Mar"));
        
        //4. Get element from cache
        Element ele = cache.get("2");
        
        //5. Print out the element
        String output = (ele == null ? null : ele.getObjectValue().toString());
        System.out.println(output);
        
        //6. Is key in cache?
        System.out.println(cache.isKeyInCache("3"));
        System.out.println(cache.isKeyInCache("10"));
        
        //7. shut down the cache manager
        cm.shutdown();   
    }
}

要点:

CacheManager cm = CacheManager.create("/ehcache.xml");
Cache cache = cm.getCache("movieFindCache");
cache.put(new Element("1","Jan"));

参考:
http://sishuok.com/forum/posts/list/315.html
http://www.mkyong.com/ehcache/ehcache-hello-world-example/)
ehcache另有商用版BigMemory,参考:http://carlosfu.iteye.com/blog/2239561

相关文章

网友评论

      本文标题:Ehcache

      本文链接:https://www.haomeiwen.com/subject/fhyfsttx.html