Ehcache 简单的监控

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("--------------------------------------------------------");
    }
}

 

代码见附件:EhCache-Cluster-Tester.zip 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值