craftcms / element-api

Create a JSON API/Feed for your elements in Craft.
MIT License
498 stars 57 forks source link

[FR] Cache clearing CLI and/or utility #136

Closed yoannisj closed 3 years ago

yoannisj commented 4 years ago

Description

It would be great to add a craft CLI command and/or a checkbox in the CP's clear-caches utility, to clear all element-api caches. This action could then be triggered on deployments and element change related events (save, publish, expire, etc.).

Currently, the only way to clear element-api caches reliably is to flush all data caches with Craft::$app->getCaches()->flush(), but this is a bit aggressive as it potentially clears cache entries from other plugins (and I believe as of Craft 3.5 also rendered template caches).

A less aggressive approach would be to clear the cache entry for a given element when it gets saved (probably also when an entry gets published or expires), as suggested here, but this won't work if an endpoint returns related element data (when the related element gets saved, getting the cache key for each and every endpoint where this element is included can quickly become quite tricky).

For this use case (not so uncommon in my own experience for what it's worth), I believe linking the element-api cache to element query caches (similar to how template caches are handled) would be the most efficient approach.

Relate issues #47, #55, #122

brandonkelly commented 3 years ago

Just released Element API 2.7.0, which adds the ability to clear Element API response caches from the Caches utility, or via the craft invalidate-tags/element-api command.