Caching for chained collection filters is slightly more complex because there are two opportunities for caching: caching for the first collection filter in the chain and caching for the chain itself.
Consider a filter chain that chains together three filters: StartEndDateFilter
, AFilter
, and BFilter
. The filter chain passes the collection to StartEndDateFilter
, which, in turn, passes a reduced collection to AFilter
, and so on from AFilter
to BFilter
. The final collection is returned to filter chain instance.
Caching the content produced by each collection filter that’s part of a chain is inefficient because the context largely influences the output produced be a collection filter. A change to the context might change the output of one collection filter, which would alter the output of all collection filters that follow it in the chain. For example assume that on March 15, StartEndDateFilter
returns a collection that includes objects A
, B
, C
, and D
. AFilter
processes this collection and produces a subset, which BFilter
reduces further to produce a final collection set. Caching the content for each individual filter is wasteful if you consider that March 16 could cause StartEndDateFilter
to return a completely different set of objects, such as G
, H
, T
, and R
. Cached content produced by downstream collection filters would not apply.
As a result, caching is available at two points during the chained filter lifecycle: for the first collection filter’s output collection and the chained filter’s final output collection. In order to cache the chain results, the cached Map is constructed in a slightly different way. The CacheCollectionKey
has an array of context objects, one for each collection filter in the chain. Since the array must have a context object that represents each collection filter, but not all collection filters have a context keys, the array contains the following:
A context key for each collection filter that produces a context key while filtering the collection
A null placeholder for each collection filter that filters the collection, but doesn’t have a context object
A reference to the filter itself for each collection filter that can’t filter the collection
To determine whether cached content can be reused, the current CacheCollectionKey
is compared to previously generated ones.
In this example, cached content would be saved for StartEndDateFilter
and the ChainedFilter
instance, assuming that caching is enabled for both of them. However, regardless of the caching settings on the remaining collection filters in the chain, they will never be cached because when they are executed, their consultCache
and updateCache
servlet bean properties are explicitly set to false
.