需要的ehcache.jar包
ehcache.xml 配置:
<ehcache>
<!-- Sets the path to the directory where cache .data files are created.
If the path is a Java System Property it is replaced by
its value in the running VM.
The following properties are translated:
user.home - User's home directory
user.dir - User's current working directory
java.io.tmpdir - Default temp file path -->
<diskStore path="java.io.tmpdir"/>
<!--Default Cache configuration. These will applied to caches programmatically created through
the CacheManager.
The following attributes are required for defaultCache:
maxInMemory - Sets the maximum number of objects that will be created in memory
eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element
is never expired.
timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
if the element is not eternal. Idle time is now - last accessed time
timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
if the element is not eternal. TTL is now - creation time
overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
has reached the maxInMemory limit.
-->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/>
<!--Predefined caches. Add your cache configuration settings here.
If you do not have a configuration for your cache a WARNING will be issued when the
CacheManager starts
The following attributes are required for defaultCache:
name - Sets the name of the cache. This is used to identify the cache. It must be unique.
maxInMemory - Sets the maximum number of objects that will be created in memory
eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element
is never expired.
timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
if the element is not eternal. Idle time is now - last accessed time
timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
if the element is not eternal. TTL is now - creation time
overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
has reached the maxInMemory limit.
-->
<!-- Sample cache named sampleCache1
This cache contains a maximum in memory of 10000 elements, and will expire
an element if it is idle for more than 5 minutes and lives for more than
10 minutes.
If there are more than 10000 elements it will overflow to the
disk cache, which in this configuration will go to wherever java.io.tmp is
defined on your system. On a standard Linux system this will be /tmp"
-->
<cache name="cacheName1"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"
/>
<cache name="cacheName2"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="0"
timeToLiveSeconds="1200"
overflowToDisk="true"
/>
<cache name="cacheName3"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="600"
timeToLiveSeconds="6000"
overflowToDisk="true"
/>
<!-- Place configuration for your caches following -->
</ehcache>
java 代码:
package com.secoo.stocks.util;
import java.io.Serializable;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
public class CacheUtil {
private CacheManager manager = new CacheManager();
public void getValueCache(){
Cache cache = manager.getCache("cacheName1");
Element element = cache.get("key");
if(element == null){
element = new Element("key", "数据");
cache.put(element);
}else{
System.out.println(element.getValue());
// cache.remove(key);
}
// Serializable value = element.getValue();
// System.out.println("序列化后的值为:"+value.toString());
// Object value1 = element.getObjectValue();
// System.out.println("未序列化的值为:"+value1.toString());
// int elementsInMemory = cache.getSize();
// System.out.println("得到缓存的对象数量:"+elementsInMemory);
// long elementsInMemory1 = cache.getMemoryStoreSize();
// System.out.println("得到缓存对象占用内存的数量:"+elementsInMemory1);
// long elementsInMemory2 = cache.getDiskStoreSize();
// System.out.println("得到缓存对对象占用磁盘的数量:"+elementsInMemory2);
// int hits = cache.getHitCount();
// System.out.println("得到缓存读取的命中次数:"+hits);
// int hits1 = cache.getMemoryStoreHitCount();
// System.out.println("得到内存中缓存读取的命中次数:"+hits1);
// int hits2 =cache.getDiskStoreHitCount();
// System.out.println("得到磁盘中缓存读取的命中次数:"+hits2);
// int hits3 = cache.getMissCountNotFound();
// System.out.println("得到缓存读取的丢失次数:"+hits3);
// int hits4 = cache.getMissCountExpired();
// System.out.println("得到缓存读取的已经被销毁的对象丢失次数:"+hits4);
}
}
Cache属性配置注释:
name:缓存名称。
maxElementsInMemory:缓存最大个数, 内存中最多可缓存 10000 个 Element。
eternal:对象是否永久有效,一但设置了,timeout将不起作用。
timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。
overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。
diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。
maxElementsOnDisk:硬盘最大缓存个数。
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:内存数量最大时是否清除。
(注:部分来自其他文章)