Ehcache如何在元素过期时删除键?

时间:2011-07-08 15:41:00

标签: java ehcache

我正在尝试从缓存中只获取有效对象。如果我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);

}

2 个答案:

答案 0 :(得分:1)

在从缓存中获取密钥数之前,您可以逐出过期的元素,如下所示。 cache.evictExpiredElements();

这样,您将只获得活动密钥。

答案 1 :(得分:0)

我已将您的侦听器代码添加到我的缓存中。当我尝试从缓存中获取元素时,注意到elementExpired事件触发了。所以它是懒惰或按需动作。

您可以使用.getKeysWithExpiryCheck()方法从列表中删除过期的元素。