EhCache 使用简介_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > EhCache 使用简介

EhCache 使用简介

 2010/9/19 22:57:24  jja1982  http://jja1982.javaeye.com  我要评论(0)
  • 摘要:http://ehcache.org/lib:ehcache-core-2.2.0.jarslf4j-api-1.5.11.jarslf4j-jdk14-1.5.11.jarIncomputerscienceterms,acacheisacollectionoftemporarydatawhicheitherduplicatesdatalocatedelsewhereofistheresultofacomputation.Onceinthecache
  • 标签:EhCache 使用简介
http://ehcache.org/

lib:
ehcache-core-2.2.0.jar
slf4j-api-1.5.11.jar
slf4j-jdk14-1.5.11.jar

In computer science terms, a cache is a collection of temporary data which either duplicates data located elsewhere of is the result of a computation. Once in the cache, the data can be repeatedly accessed inexpensively.

提高CPU约束的应用程序性能
提高算法性能,通过多CPU(SMP)或者多机器(Clusters)进行并行计算,提高CPU速度。
1. Temporarily store computations that may be reused again.
2. An example from EhCache would be large web pages that have a high rendering cost. Another caching of authentication status, where authentication requires cryptographic transforms.
提高I/O约束的应用程序性能。
1. DAO caching for Hibernate
2. Web page caching, for pages generated from database.

Cache eviction algorithms
MemoryStore: LRU (Least Recently Used), LFU(Less Frequently Used), FIFO (First in First Out). Default is LRU.
DiskStore: LFU.

Cache configuration
If the CacheManager default constructor or factory method is called, Ehcache looks for a file called ehcache.xml in the top level of the classpath.
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
	<diskStore path="java.io.tmpdir" />

	<defaultCache maxElementsInMemory="0" eternal="false" overflowToDisk="true" timeToIdleSeconds="1200" timeToLiveSeconds="1200" />

	<!-- 
	Cache configuration
	
	The follow attributes are required.
	name: sets the name of the cache. This is used to identify the cache. It must be unique.
	maxElementsInMemory: sets the maximum number of objects that will be created in memory.
	maxElementsOnDisk: sets the maximum number of objects that will be maintained in the DiskStore. The default value is zero, meaning unlimited.
	eternal: sets whether elements are eternal. If eternal, timeouts are ignored and the element is never expired.
	overflowToDisk: sets whether elements can overflow to disk when the memory store has reached the maxInMemory limit.
	
	The following attributes and elements are optional.
	timeToIdleSeconds: sets the time to idle for an element before it expires. Is only used if the element is not eternal. The default value is 0. A value of 0 means that an Element can idle for infinity.
	timeToLiveSeconds: sets the time to live for an element before it expires. Is only used if the element is not eternal. The default value is 0. A value of 0 means that an Element can live for infinity.
	diskPersistent: Whether the disk store persists between restarts of the Virtual Machine. The default value is false.
	diskExpiryThreadIntervalSeconds: The number of seconds between runs of the disk expiry thread. The default value i 120 seconds. Warning: setting this to a low value is not recommended. It can cause excessive DiskStore locking and high cpu utilisation.
	diskSpoolBufferSizeMB: This is the size to allocate the DiskStore for a spool buffer. Writes are made to this area and then asynchronously written to disk. The default size is 30MB. If you get OutOfMemory errors consider lowering this value. To improve DiskStore performance consider increasing it.
	clearOnFlush: whether the MemoryStore should be cleared when flush() is called on the cache. By default, this is true i.e. the memoryStore is cleared.
	memoryStoreEvictionPolicy: Policy would be enforced upon reaching the maxElementsInmemory limit. Default policy is LRU.
	 -->
	<cache name="sampleCache" maxElementsInMemory="0" maxElementsOnDisk="1000" eternal="false" overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="600" />
</ehcache>


Sample
CacheManager manager = CacheManager.getInstance();
Cache cache = manager.getCache("sampleCache");
// get cache configuration
cache.getCacheConfiguration();
// create cache manually
manager.addCache(new Cache("test", 100, true, false, 600, 300));
Cache anotherCache = manager.getCache("test");
// obtaining cache size
int elementsInCache = cache.getSize();
long elementsInMemory = cache.getMemoryStoreSize();
long elementsInDisk = cache.getDiskStoreSize();
String[] names = manager.getCacheNames();
// put or update an element in a cache. Ehcache knows there is an existing element.
cache.put(new Element("key1", "value1"));
Element element = cache.get("key1");
// get a Serializable value form an element in a cache. So all the object can use the Element#getValue.
Serializable value1 = element.getValue();
// get a NonSerializable value from an elemetent in a cache.
Object value2 = element.getObjectValue();
cache.remove("keys1");
manager.shutdown();


Below is the source code of get element. So we only need to use Element#getValue instead of Element#getObjectValue if value is serizaliable.
/**
     * Gets the value attribute of the Element object.
     *
     * @return The value which must be Serializable. If not use {@link #getObjectValue}. If the value is not Serializable, null is returned and an info log message emitted
     * @see #getObjectValue()
     */
    public final Serializable getValue() {
        Serializable valueAsSerializable;
        try {
            valueAsSerializable = (Serializable) value;
        } catch (Exception e) {
            throw new CacheException("The value " + value + " for key " + key +
                    " is not Serializable. Consider using Element#getObjectKey()");
        }
        return valueAsSerializable;
}
/**
     * Gets the value attribute of the Element object as an Object.
     * <p/>
     * This method is provided for those wishing to use ehcache as a memory only cache
     * and enables retrieval of non-Serializable values from elements.
     *
     * @return The value as an Object.  i.e no restriction is placed on it
     * @see #getValue()
     * @since 1.2
     */
    public final Object getObjectValue() {
        return value;
    }


注意事项
1. DiskStore中的Element必须实现Serializable
Only Elements which are Serializable can be placed in the DiskStore. Any nonserializable Elements which attempt to overflow to the DiskStore will be removed instead, and a WARNING level log message emitted.

2. Object存于DiskStore时,注意使用Byte [] 代替String。从官方文档中看到两者存在很大的性能差距。
Serialization speed is affected by the size of the objects being serialized and their type. It has been found in the ElementTest test that:
---The serialization time for a Java object being a large Map of String arrays was 126ms, where the a serialized size was 249,225 bytes.
---The serialization time for a byte[] was 7ms, where the serialized size was 310,232 bytes.
Byte arrays are 20 times faster to serialize. Make user of byte arrays to increase DiskStore performance.


发表评论
用户名: 匿名