Http Cache - Invalidation
Preventing stale content is very important. In the HTTP cache, it is the responsibility of the developer to do this, as the HTTP cache is so flexible and extensible, it is impossible for it to know when to invalidate itself. That is why the developer must instruct the HTTP cache when to flush the content. However, there are some OOTB helper classes out there, next to the HttpCacheEngine and HttpCacheStore services which can be referenced to invalidate the cache as well.
HttpCacheInvalidationJobConsumer will invalidate a path (and references of the path if configured) by consuming a sling job with the topic
So in theory you can write your own component, that will based on your business logic needs create a sling job with this topic, and the path (key
CacheInvalidationJobConstants.PAYLOAD_KEY_DATA_CHANGE_PATH) that needs to be invalidated.
OOTB we provided the JCRNodeChangeEventHandler below. You can leverage this or use this as example to create your own variant. Custom cache invalidation events could be sling event handlers for JCR specific events, sling filter to trap replication events, workflow steps, etc.
Configuring JCR Node change invalidator (JCRNodeChangeEventHandler) Since v2.5.0/3.14.0
Sample cache invalidation job creator. Watches for changes in JCR nodes and creates sling jobs for cache invalidation.
event.filterJCR paths to be watched for changes. Expressed in LDAP syntax.
Enabling Reference-based invalidations (HttpCacheInvalidationJobConsumer). Since v2.5.0/3.1.0
Invalidate resources that reference the change resource.
The HttpCacheEngine service has 2 methods for cache invalidation.
1: isPathPotentialToInvalidate(String path).
This executes the regex statement from all cache config’s
httpcache.config.invalidation.oak.paths values, and returns if there is at least one match.
This is good to narrow down a filter and save performance in your service / servlet / workflow.
2: invalidateCache(String keyString)
- CacheKey with the String constructor get’s constructed using buildCacheKey(String resourcePath) from the factory.
- Calls isInvalidatedBy on the entries’ CacheKey objects in ALL stores.
- The AbstractCacheKey provides an implementation for isInvalidatedBy that checks if the resourcePath is equal.
- This makes sense because if the underlying resource changes, you generally want to invalidate.
- If true, it will clear the corresponding entry from the store
Note: Be careful overriding isInvalidatedBy, this can lead to the OOTB
HttpCacheInvalidationJobConsumer not working.