2011-07-08 11 views
5

मैं कैश से केवल वैध वस्तुओं को प्राप्त करने की कोशिश कर रहा हूं। अगर मैं List list = cache.getKeys(); करता हूं तो यह उन चाबियों को वापस कर देगा जो समाप्त हो चुके हैं। हालांकि मैं श्रोता जोड़ने और कुंजी को हटाने की कोशिश करता हूं, लेकिन मेरे श्रोता notifyElementExpired को कभी भी नहीं कहा जाता है।Ehcache तत्व समाप्त होने पर कुंजी को कैसे निकालें?

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

मैं अपने कैश करने के लिए अपने श्रोता कोड जोड़ दिया है:

यहाँ मेरी कोड है। और देखा कि elementExpired ईवेंट आग जब मैं कैश से तत्व प्राप्त करने का प्रयास करता हूं। तो यह आलसी या मांग पर कार्रवाई है।

आप सूची से समाप्त होने वाले तत्वों को निकालने के लिए .getKeysWithExpiryCheck() विधि का उपयोग कर सकते हैं।

1

कैश से चाबियों की गिनती प्राप्त करने से पहले, आप नीचे की अवधि समाप्त हो चुके तत्वों को बेदखल कर सकते हैं। कैश.evictExpiredElements();

तो, आपको केवल सक्रिय कुंजी मिल जाएगी।

संबंधित मुद्दे