Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
RE: EventListener for PortalCache in liferay
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**********"); }}
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**********"); }}
The bigger question is why you are trying to do this...
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>
<cache
eternal="true"
maxElementsInMemory="100000"
name="XXXX"
overflowToDisk="false"
>
</cache>
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...
If you had started with this question earlier, we probably could have helped you avoid trying to build out all of this code...
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).
The default in an unconfigured cache is zero. But Liferay includes a <defaultCache /> tag which resets all of these normal defaults:
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.
<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.
Copyright © 2025 Liferay, Inc
• Privacy Policy
Powered by Liferay™