Uploaded image for project: 'Cache Modules'
  1. Cache Modules
  2. MGNLCACHE-45

Option to set lifetime on allowed cache mappings

XMLWordPrintable

    • Icon: New Feature New Feature
    • Resolution: Outdated
    • Icon: Minor Minor
    • None
    • None
    • configuration
    • None

      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.

        Acceptance criteria

              Unassigned Unassigned
              tduffey Thomas Duffey
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved:

                  Estimated:
                  Original Estimate - 3d
                  3d
                  Remaining:
                  Remaining Estimate - 3d
                  3d
                  Logged:
                  Time Spent - Not Specified
                  Not Specified