1.web页面显示:ehcache缓存信息
index.jsp
监控列表:
2. main函数测试代码(来源网络,并加以修改):
package test;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import net.sf.ehcache.Statistics;
/**
#############################################################################
# DESCRIBE ehcache 缓存操作DEMO
# AUTHOR 悠~游 (原创地址:http://www.uusam.com/uu/blog/?p=60)
# DATE 2006-7-10
# COMPANY FLX
# PORJECT ehcache-demo
#
# 这篇文章写得比较早,ehcache变化很大,代码方法都变了,我使用ehcache2.x,并对代码进行了相应修改 add by nassir wen 2012-07-23
#############################################################################
*/
public class Demo {
static CacheManager manager = new CacheManager();
public static void main(String[] args) throws InterruptedException {
DateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String[] cacheNames = manager.getCacheNames();
System.out.println("读取的缓存列表为:");
for (int i = 0; i < cacheNames.length; i++) {
System.out.println("--" + (i + 1) + " " + cacheNames[i]);
}
Cache cache = manager.getCache("mycache");
Element element = new Element("key1", "value1");
cache.put(element);
element = cache.get("key1");
Serializable value = element.getValue();
System.out.println("序列化后的值为:" + value.toString());
element = cache.get("key1");
Object value1 = element.getObjectValue();
System.out.println("未序列化的值为:" + value1.toString());
int elementSize = cache.getSize();
System.out.println("得到缓存的对象数量:" + elementSize);
//缓存元素集合
System.out.println("-----------------------缓存元素统计数据---------------------------------");
List keys = cache.getKeys();
for (Object key : keys) {
Element ele = cache.get(key);
System.out.println("内容: " + ele.getValue());
System.out.println("创建时间: " + sf.format(ele.getCreationTime()));
System.out.println("最后访问时间: " + sf.format(ele.getLastAccessTime()));
System.out.println("过期时间: " + sf.format(ele.getExpirationTime()));
System.out.println("最后更新时间: " + sf.format(ele.getLastUpdateTime()));
System.out.println("命中次数: " + ele.getHitCount());
System.out.println("存活时间: " + ele.getTimeToLive() + "ms");
System.out.println("空闲时间: " + ele.getTimeToIdle() + "ms");
}
System.out.println("--------------------------------------------------------");
System.out.println("-----------------------缓存总统计数据---------------------------------");
long elementsInMemory1 = cache.getMemoryStoreSize();
System.out.println("得到缓存对象占用内存的数量:" + elementsInMemory1);
long elementsInMemory2 = cache.getDiskStoreSize();
System.out.println("得到缓存对对象占用磁盘的数量:" + elementsInMemory2);
//获取缓存统计对象
Statistics stat = cache.getStatistics();
long hits = stat.getCacheHits();
System.out.println("得到缓存读取的命中次数:" + hits);
long memoryHits = stat.getInMemoryHits();
System.out.println("得到内存中缓存读取的命中次数:" + memoryHits);
long diskHits = stat.getOnDiskHits();
System.out.println("得到磁盘中缓存读取的命中次数:" + diskHits);
long cacheMisses = stat.getCacheMisses();
System.out.println("得到缓存读取的丢失次数:" + cacheMisses);
long evictionCount = stat.getEvictionCount();
System.out.println("得到缓存读取的已经被销毁的对象丢失次数:" + evictionCount);
System.out.println("--------------------------------------------------------");
}
}