[MGNLCACHE-45] Option to set lifetime on allowed cache mappings Created: 14/Nov/05  Updated: 14/Mar/14  Resolved: 14/Mar/14

Status: Closed
Project: Cache Modules
Component/s: configuration
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Minor
Reporter: Thomas Duffey Assignee: Unassigned
Resolution: Outdated Votes: 0
Labels: None
Remaining Estimate: 3d
Time Spent: Not Specified
Original Estimate: 3d

Template:
Acceptance criteria:
Empty
Date of First Response:

 Description   

Add a new "lifetime" node to the allowed cache URI mappings. This node specifies an amount of time that a page matching this URI can be retrieved from the cache. This adds some flexibility to pages where you have some dynamic content but for performance reasons want to show a snapshot of that dynamic content and have it refreshed periodically.

I currently have patches against 2.1.3 and will look into updating them for 2.2 but the implementation is pretty simple:

1) Update info.magnolia.cms.beans.config.Cache.cacheCacheableURIMappings() to retrieve the "lifetime" node and store its value along with the "allowed" value in the cachedCacheableURIMapping map, e.g.,

NodeData lifetime = container.getNodeData(CACHE_LIFETIME_NODE);
long l = lifetime.getLong();

// values[0] := allowed
// values[1] := lifetime, <= 0 is eternity
cachedCacheableURIMapping.put(p, new Object[]

{ BooleanUtils.toBooleanObject(allow), new Long(l) }

);

2) Add new getLifetime(HttpServletRequest request) method to info.magnolia.cms.beans.config.Cache. It looks just like isCacheable(HttpServletRequest request) except it retrieves the lifetime value from the map.

3) Update info.magnolia.cms.beans.config.Cache.isCacheable() to retrieve the "allowed" value from the cachedCacheableURIMapping map. This assumes an implementation like in (1) above where I'm storing both the value of "allowed" and the lifetime in the value of the map.

4) Add new isExpired(HttpServletRequest request) method to info.magnolia.cms.beans.runtime.Cache. This checks that the sum of the cached item's creation time and lifetime is less than the current time, e.g.,

public static boolean isExpired(HttpServletRequest request) {
long ctime = Cache.getCreationTime(request);
long lifetime = info.magnolia.cms.beans.config.Cache.getLifetime(request);
return lifetime <= 0 ? false : new java.util.Date().getTime() > (ctime + lifetime);
}

5) Update info.magnolia.cms.core.CacheHandler.cacheURI() to check if an item that is currently cached is expired. If this is the case, the item must be removed from the cache and the cache handler should continue, e.g.,

if (Cache.isCached(request) || CacheHandler.hasRedirect(request)) {
if (Cache.isExpired(request))

{ CacheHandler.flushResource(request); }

else {
return;
}

6) Add new flushResource(HttpServletRequest request) method to info.magnolia.cms.core.CacheHandler that removes the requested page from the cache, e.g.,

public static void flushResource(HttpServletRequest request) {
flushResource(CACHE_DIRECTORY + DEFAULT_STORE + Path.getURI(request));
flushResource(CACHE_DIRECTORY + COMPRESSED_STORE + Path.getURI(request));
}

7) Modify info.magnolia.cms.servlets.EntryServlet to check if a cached item is expired, e.g.,

// try to stream from cache first
if (cacheable && Cache.isCached(req) && !Cache.isExpired(req)) {
if (CacheHandler.streamFromCache(req, res))

{ return; // if success return }

}

That's about it, there is still some weirness if you restart Tomcat w/o clearing out the cache but otherwise it's working fine. It would be more kind to the users to specify the lifetime in seconds or minutes rather than milliseconds.



 Comments   
Comment by Philipp Bracher [ 21/Nov/05 ]

Sounds great. Can you create a patch?

Comment by Philipp Bracher [ 17/May/06 ]

cache is now plugable

Comment by Magnolia International [ 14/Mar/14 ]

This request dates from pre-3.0 times. With 3.0, the cache module became pluggable; with 3.6, and perhaps already before, one could configure ehcache settings to control the lifetime of cached pages. Of course if I'm missing something, please reopen this issue and we'll look at it again.

Generated at Sun Feb 11 23:51:40 CET 2024 using Jira 9.4.2#940002-sha1:46d1a51de284217efdcb32434eab47a99af2938b.