The 5.7 branch of Magnolia reached End-of-Life on December 31, 2023, as specified in our End-of-life policy. This means the 5.7 branch is no longer maintained or supported. Please upgrade to the latest Magnolia release. By upgrading, you will get the latest release of Magnolia featuring significant improvements to the author and developer experience. For a successful upgrade, please consult our Magnolia 6.2 documentation. If you need help, please contact info@magnolia-cms.com.

The Advanced Cache module is collection of advanced cache strategies to help minimize load on the server while ensuring fresh content is served to the users.

Installing

Maven is the easiest way to install the module. Add the following to your bundle. The parent POM of your webapp project should set the latest version of the module automatically. Should you need to use a specific module version, you can define it using the <version/> in the dependency.

<dependency>
  <groupId>info.magnolia.advancedcache</groupId>
  <artifactId>magnolia-advanced-cache</artifactId>
</dependency>

Uninstalling

  1. Set cache policy in /modules/cache/config/contentCaching/defaultPageCache/cachePolicy@class back to default value info.magnolia.module.cache.cachepolicy.Default.
  2. Remove the /modules/advanced-cache node and its subnodes.
  3. Shut down Magnolia, remove the Advanced Cache module JAR (magnolia-advanced-cache.jar) from WEB-INF/lib and start up Magnolia again.

Caching strategies

Serving old content while re-caching

Using this strategy, cache is not completely cleaned on content update and entries are retained. After update, when the first request comes in, fresh cache entry generation is triggered and all further requests for the same entry are served from old cache content until the new entry is ready.

  • Set /modules/cache/config/contentCaching/defaultPageCache/cachePolicy@class to info.magnolia.module.advancedcache.ServeUntilRecachedCachePolicy
  • Set /modules/cache/config/contentCaching/defaultPageCache/flushPolicy/policies/flushAll@class to info.magnolia.module.advancedcache.NotifyFlushListeningPolicy

See also: Timestamp for last flushing

Eager re-caching

Using this strategy, cache keeps a list of the most frequently served entries. The system attempts to refresh these entries as soon as it detects an update to content. All other entries are re-cached on request. It is possible to configure the number of entries to re-cache immediately and to set the lifetime of the most served content:

  • Set /modules/cache/config/contentCaching/defaultPageCache/cachePolicy@class to info.magnolia.module.advancedcache.EagerRecacheCachePolicy
  • Set /modules/cache/config/contentCaching/defaultPageCache/flushPolicy/flushAll@class to info.magnolia.module.advancedcache.EagerRecacheFlushPolicy
  • Set /modules/cache/config/contentCaching/defaultPageCache/executors/store/cacheContent@class to info.magnolia.module.cache.executor.Store

By default, statistics about served pages are retained indefinitely. However, you can force the system to flush the cache after each content update. To do this:

  • Create a resetAfterUpdate property node under flushPolicy.
  • Set the value to true.

To change the number of pages to be eagerly re-cached:

  • Create an eagerRecache property node under flushPolicy.
  • Set the value to the number of top most entries in the list you wish to have eagerly re-cached on every update. By default top 100 entries are eagerly re-cached.

See also:  Timestamp for last flushing

Changing waiting time

By default Magnolia waits for 10 seconds, while still serving an old version of the content, before attempting to re-cache the given entry. This timeout is controlled by the blockingTimeout property configured in the Ehcache back-end . The blockingTimeout applies to the whole cache factory. If you want to change the waiting time for a particular cache policy, use the timeout property.

Change the waiting time for the default server and flush policies by adding a timeout node data with value being the number of milliseconds to wait (i.e. default value is equivalent of 10 000).

Changes to cache policies and executors are applied immediately. Setting incorrect values can render the Magnolia instance inaccessible. If this happens, try the Groovy Rescue app.

See also   Cache core module .

Timestamp for last flushing

The advanced flush policies update a lastUpdateTimestamp property to record the last flushing event. The timestamp allows the corresponding cache policy to identify which cache entries need to be regenerated. When you delegate cache to an advanced cache policy, create a property at a path that includes the name of your custom policy.

Example 1: Serving old content while re-caching

  • /modules/advanced-cache/config/notifyFlushListeningPolicy@lastUpdateTimeStamp (default)
  • /modules/advanced-cache/config/notifyFlushListeningPolicy/<custom_cache_name>@lastUpdateTimeStamp (custom)

Example 2: Eager re-caching

  • /modules/advanced-cache/config/eagerRecacheFlushPolicy@lastUpdateTimeStamp (default)
  • /modules/advanced-cache/config/eagerRecacheFlushPolicy/<custom_cache_name>@lastUpdateTimeStamp (custom)

where  <custom_cache_name>  is the name of your custom policy.

Multisite cache configuration

When content is published, the cache on the public instance is flushed to show the new content. This increases the load on the server and affects all sites in a multisite environment. You can configure multiple cache configurations to ensure that only cache entries that belong to the same subree (site) as the published content are flushed. Use Advanced Cache App module for easy configuration or just do it manually:

  1. Set /modules/advanced-cache/config@createSeparateCachesForEachSite=true. This creates caches for each site defined. It uses the defaultPageCache configuration for each site if  /modules/cache/config/contentCaching/SITE-NAME doesn't exist.
  2. Set /server/filters/cache@class=info.magnolia.module.advancedcache.filter.SiteAwareCacheFilter.This filter chooses the right site-specific cache to use so you don't need to have separate cache filters per cache.
  3. Create /modules/cache/config/contentCaching/defaultPageCache/flushPolicy/policies/flushSiteAware@class=info.magnolia.module.advancedcache.SiteAwareFlushAllListeningPolicy.
  4. For all site-aware workspaces (those which have separate subtrees for each site):
    1. Exclude the workspace from the default flush policy: /modules/cache/config/contentCaching/defaultPageCache/flushPolicy/policies/flushAll/excludedWorkspaces@WORKSPACE_NAME=WORKSPACE_NAME.
    2. Register this workspace to site aware flush policy: /modules/cache/config/contentCaching/defaultPageCache/flushPolicy/policies/flushSiteAware/workspaces@WORKSPACE_NAME=WORKSPACE_NAME.
  5. Restart your instance.