java ehcache 过期_java – Ehcache如何在元素过期时删除键?

本文探讨了在Java使用Ehcache时如何处理元素过期的问题。作者通过设置时间戳并添加缓存监听器尝试在元素过期时删除键,但发现notifyElementExpired方法未被调用。代码示例展示了如何创建缓存、设置过期时间以及添加事件监听器的过程。
摘要由CSDN通过智能技术生成

我试图从缓存中只获取有效对象.如果我做List List = cache.getKeys();它将返回过期的密钥.我虽然添加了一个监听器并尝试自己删除密钥,但我的监听器notifyElementExpired永远不会被调用.

这是我的代码:

public static void main(String[] args) throws Exception {

// TODO Auto-generated method stub

CacheManager.getInstance().addCache("test");

Cache cache = CacheManager.getInstance().getCache("test");

cache.getCacheConfiguration().setTimeToLiveSeconds(10);

cache.getCacheEventNotificationService().registerListener(new CacheEventListener() {

public void notifyRemoveAll(Ehcache arg0) {

// TODO Auto-generated method stub

System.out.println("notifyRemoveAll cache=" + arg0);

}

public void notifyElementUpdated(Ehcache arg0, Element arg1)

throws CacheException {

// TODO Auto-generated method stub

System.out.println("notifyElementUpdated cache=" + arg0 + " element=" + arg1);

}

public void notifyElementRemoved(Ehcache arg0, Element arg1)

throws CacheException {

// TODO Auto-generated method stub

System.out.println("notifyElementRemoved cache=" + arg0 + " element=" + arg1);

}

public void notifyElementPut(Ehcache arg0, Element arg1)

throws CacheException {

// TODO Auto-generated method stub

System.out.println("notifyElementPut cache=" + arg0 + " element=" + arg1);

}

public void notifyElementExpired(Ehcache arg0, Element arg1) {

// TODO Auto-generated method stub

System.out.println("notifyElementExpired cache=" + arg0 + " element=" + arg1);

}

public void notifyElementEvicted(Ehcache arg0, Element arg1) {

// TODO Auto-generated method stub

System.out.println("notifyElementEvicted cache=" + arg0 + " element=" + arg1);

}

public void dispose() {

// TODO Auto-generated method stub

System.out.println("dispose");

}

public Object clone() throws CloneNotSupportedException {

throw new CloneNotSupportedException();

}

});

//cache.getCacheConfiguration().setTimeToLiveSeconds(0);

String key = "key";

String value = "value1";

System.out.println("created at = " + new Date());

//cache.put(new Element(key, value, false, new Integer(1), new Integer(5)));

cache.put(new Element(key, value, false, new Integer(1), new Integer(5)));

System.out.println("key=" + key + "will expired at object=" + new Date(cache.get(key).getExpirationTime()));

Thread.sleep(7000);

@SuppressWarnings("unchecked")

List list = cache.getKeys();

System.out.println("current time = " + new Date());

for (Object item : list) {

System.out.println("created at = " + new Date());

//System.out.println("key=" + item + " object=" + new Date(cache.get(item).getExpirationTime()));

//System.out.println(item + " isExpired=" + cache.get(item).isExpired());

}

Thread.sleep(30000);

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值