【Encache】encache2.5.2缓存的使用

0.需要的jar包:

1.首先要了解缓存清除策略,官方文档给出的有

LRU - least recently used(最近最少使用)
LFU - least frequently used(最不经常使用)
FIFO - first in first out, the oldest element by creation time(清除最早缓存的数据,不关心是否经常使用)

2.一个简单的使用

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
 
    <diskStore path="G:/development/workspace/test/WebContent/cache/temporary"/><!-- 达到内存上限后缓存文件保存位置 -->
    
    <defaultCache 
        maxElementsInMemory="10000" <!-- 最大内存占用,超出后缓存保存至文件 -->
        memoryStoreEvictionPolicy="LRU" <!-- 缓存废弃策略,LRU表示最少使用的优先清除,此值对应之前3种策略 -->
        eternal="false"  
        timeToIdleSeconds="1" <!-- 空闲时间,超出此时间未使用缓存自动清除 -->
        timeToLiveSeconds="1" <!-- 清除时间,缓存保留的最长时间 -->
        overflowToDisk="false" <!-- 是否往硬盘写缓存数据 -->
        diskPersistent="false" />
        
    <!-- 测试 -->
    <cache 
        name="cache_test" <!-- 缓存名称 -->
        memoryStoreEvictionPolicy="LRU" 
        maxElementsInMemory="1" 
        eternal="false"
        timeToIdleSeconds="7200"
        timeToLiveSeconds="7200" 
        overflowToDisk="true" />
</ehcache>

测试代码:

package encache;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public class FirstCache {
    private static final Logger log = LoggerFactory.getLogger(FirstCache.class.getName());

    // 1.创建CacheManager
//    CacheManager cacheManager = CacheManager.create("E:/MavenEclipseWorkspace/encache/target/classes/ehcache.xml");// 也可以通过URL制定
    //一般用下面这种方式创建Cachemanager
     CacheManager cacheManager = new  CacheManager();//默认读取classpath目录下面的ehcache.xml

    public FirstCache() {
        // 2.创建Cache
        Cache cache = cacheManager.getCache("cache_test");
        // 3.存取元素
        cache.put(new Element("firstCache", "第一个缓存元素"));

        // 4.获取元素
        Element element = cache.get("firstCache");
        log.info("获取的缓存元素是:{}", element);
        long creationTime = element.getCreationTime();
        long expirationTime = element.getExpirationTime();
        log.info("creationTime: {}", new Date(creationTime));
        log.info("expirationTime: {}", new Date(expirationTime));

        int diskStoreSize = cache.getDiskStoreSize();
        int cacheSize = cache.getKeys().size();
        log.info("diskStoreSize:{}", diskStoreSize);
        log.info("cacheSize: {}", cacheSize);
    }

    public static void main(String[] args) throws Exception {
        new FirstCache();
    }
}

 结果:(注意存活时间是1s)

2018-09-09 11:54:53 [encache.FirstCache]-[INFO] 获取的缓存元素是:[ key = firstCache, value=第一个缓存元素, version=1, hitCount=1, CreationTime = 1536465293257, LastAccessTime = 1536465293275 ]
2018-09-09 11:54:53 [encache.FirstCache]-[INFO] creationTime: Sun Sep 09 11:54:53 CST 2018
2018-09-09 11:54:53 [encache.FirstCache]-[INFO] expirationTime: Sun Sep 09 11:54:54 CST 2018
2018-09-09 11:54:53 [encache.FirstCache]-[INFO] diskStoreSize:1
2018-09-09 11:54:53 [encache.FirstCache]-[INFO] cacheSize: 1

猜你喜欢

转载自www.cnblogs.com/qlqwjy/p/9613269.html