在简单研究了memcached和redis之后,又看了同样很流行的ehcache缓存。
在JavaWeb项目中加入ehcache缓存技术,所依赖的主要jar包: ehcache.jar backport-util-concurrent.jar
免积分下载地址:http://download.csdn.net/detail/kkkder/9638071
Demo是从网上找的如下:
package com.ehcache;
import java.io.Serializable;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
public class EhCache
{
public static void main(String[] args) {
//创建一个缓存管理器
CacheManager singletonManager = CacheManager.create();
//建立一个缓存实例
Cache memoryOnlyCache = new Cache("testCache", 5000, false, false, 5, 2);
//在内存管理器中添加缓存实例
singletonManager.addCache(memoryOnlyCache);
Cache cache = singletonManager.getCache("testCache");
//使用缓存
Element element = new Element("key1", "value1");
cache.put(element);
cache.put(new Element("key1", "value2"));
element = cache.get("key1");
Serializable value = element.getValue();
System.out.println("value:"+value);
int elementsInMemory = cache.getSize();
System.out.println("elementsInMemory:"+elementsInMemory);
long elementsInMemory2 = cache.getMemoryStoreSize();
System.out.println("elementsInMemory2:"+elementsInMemory2);
Object obj = element.getObjectValue();
cache.remove("key1");
System.out.println("obj:"+obj);
singletonManager.shutdown();
}
}