Facets 2.0.4 introduced caching for for facets on Search API based views. It works in existing setups or if you add a facet to an existing view. But as soon as the view is saved again the caching of facets and of the view itself breaks silently.

The issue is caused by this code:

  public function alterCacheMetadata(CacheableMetadata $cache_metadata) {
    // A view can have multiple displays, but when information is gathered about
    // all the displays' metadata, it initializes the query plugin only once for
    // the first display. However, we need to collect cacheability metadata for
    // every single cacheable display in the view, thus we are resetting the
    // query to its original unprocessed state.
    $query = $this->getQuery(TRUE)->getSearchApiQuery();
    $query->preExecute();
    // Allow modules that alter the query to add their cache metadata to the
    // view.
    $cache_metadata->addCacheableDependency($query);
  }

PreExecute() triggers the QueryPreExecuteEvent which facets subscribes to alter the query. But facets also adds it cache metadata to the query. To do so it merges the cache metadata of the view. But at this moment the metadata of the view is not final but about to be detected. So that "circular dependency" ends up in a max.age of "0" in most cases, so that max-age "0" will be stored in the view's configuration. As a consequence neither the view, nor the query, nor the facet will be cached after a view is saved.

The behavior of facets within the event subscriber is correct at execution time but not when saving the view. But within the event subscriber we can't identify the different use-cases. Therefor I suggest to tag the query to indicate that a view is saved.

CommentFileSizeAuthor
#2 3295564.patch948 bytesmkalkbrenner

Comments

mkalkbrenner created an issue. See original summary.

mkalkbrenner’s picture

StatusFileSize
new948 bytes
mkalkbrenner’s picture

Status: Active » Needs review
borisson_’s picture

Status: Needs review » Reviewed & tested by the community
drunken monkey’s picture

Status: Reviewed & tested by the community » Needs review
Related issues: +#3296848: Document existing query tags

Sounds and looks sensible, thanks a lot for reporting this and providing a patch!
Slightly rephrased the comment and then committed. Thanks again, also to Joris for reviewing!

I also created #3296848: Document existing query tags to brainstorm how we could best document this new tag, so people implementing an event handler know to look out for it (in case they are affected).

drunken monkey’s picture

Status: Needs review » Fixed

w01f’s picture

I've been following this issue and just updated - but the facet summaries still seems to break if I enable either Search API (tag-based) or Search API (time-based) caching.

Is that a different issue? Unrelatedly, the view also breaks if I enable ajax.

borisson_’s picture

@W01F, those are different issues that should probably end up in the facets queue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.