RE: EventListener for PortalCache in liferay

kesu ks, modified 5 Years ago. Junior Member Posts: 27 Join Date: 8/31/14 Recent Posts
Hi 

I want to write event listners for liferay default Ecache on events like on removal ,on expire.I have implemented PortalCacheListener and overrided the methods but none of the methods are getting called on add/removal of cache objects? whats the correct way to override these events hooks or portlet?
public class PortalCacheEventListener implements PortalCacheListener {
      @Override
    public void dispose() {    }
    @Override
    public void notifyEntryEvicted(PortalCache portalCache, Serializable key, Object value, int timeToLive)
            throws PortalCacheException {
        System.out.println("notifyEntryEvicted**********key******"+key+"****value****"+value+"********timeToLive******"+timeToLive);    }  
    @Override
    public void notifyEntryExpired(PortalCache portalCache, Serializable key, Object value, int timeToLive)
            throws PortalCacheException {
        System.out.println("notifyEntryExpired**********key******"+key+"****value****"+value+"********timeToLive******"+timeToLive);  
   @Override
    public void notifyEntryPut(PortalCache portalCache, Serializable key, Object value, int timeToLive)
            throws PortalCacheException {
        System.out.println("notifyEntryPut portalCache.getPortalCacheName()"+portalCache.getPortalCacheName());
            System.out.println("notifyEntryPut**********key******"+key+"****value****"+value+"********timeToLive******"+timeToLive);
    }  
    @Override
    public void notifyEntryRemoved(PortalCache portalCache, Serializable key, Object value, int timeToLive)
            throws PortalCacheException {
        System.out.println("notifyEntryRemoved**********key******"+key+"****value****"+value+"********timeToLive******"+timeToLive);
    }  
    @Override
    public void notifyEntryUpdated(PortalCache portalCache, Serializable key, Object value, int timeToLive)
            throws PortalCacheException {    }  
    @Override
    public void notifyRemoveAll(PortalCache portalCache) throws PortalCacheException {
        System.out.println("notifyRemoveAll**********");    }}
thumbnail
David H Nebinger, modified 5 Years ago. Liferay Legend Posts: 14933 Join Date: 9/2/06 Recent Posts
The bigger question is why you are trying to do this...
kesu ks, modified 5 Years ago. Junior Member Posts: 27 Join Date: 8/31/14 Recent Posts
Hi David,I am storing some values in cache with the cache settings as below eternal="true" .But after some time cache values are getting cleared so i want to know the root cause for this
<cache
        eternal="true"
        maxElementsInMemory="100000"
        name="XXXX"
        overflowToDisk="false"
          >
    </cache>
thumbnail
David H Nebinger, modified 5 Years ago. Liferay Legend Posts: 14933 Join Date: 9/2/06 Recent Posts
If you don't specify timeToIdleSeconds, it inherits the default which is 10 minutes.

If you had started with this question earlier, we probably could have helped you avoid trying to build out all of this code...
kesu ks, modified 5 Years ago. Junior Member Posts: 27 Join Date: 8/31/14 Recent Posts
Hi David,Ecache document says setting the 
eternal
 attribute, when set to “true”, overrides 
timeToLive
 and 
timeToIdle
 so that no expiration can take place.Even i have set timeToIdleSeconds="0" so that cache never expires.But still after some time cache is getting cleared there is no pattern so i was trying to find our  which event is getting trriggerd.
  • timeToIdleThe maximum number of seconds an element can exist in the cache without being accessed. The element expires at this limit and will no longer be returned from the cache. The default value is 0, which means no timeToIdle (TTI) eviction takes place (infinite lifetime).
thumbnail
David H Nebinger, modified 5 Years ago. Liferay Legend Posts: 14933 Join Date: 9/2/06 Recent Posts
The default in an unconfigured cache is zero. But Liferay includes a <defaultCache /> tag which resets all of these normal defaults:

<defaultcache eternal="false" maxelementsinmemory="10000" overflowtodisk="false" timetoidleseconds="600">
</defaultcache>



So the info in the docs is correct, by I am also correct.

And, FYI, an eternal cache is a bad idea. Everything you store in the cache takes runtime memory, limiting your capacity and available resources. Caching is meant to alleviate heavy hits on a slow data sink, it is not meant to just hold things around forever. Not sure what you are trying to do with your eternal cache, but I am sure there are much better ways to deal with this information.