Cached actions
When an action is configured as cacheable, when a request is received for that action, the cached output will be returned instead of executing the method. This can provide extremely efficient and short response times as the engine will only load the minimal set of modules needed to attend the cached request.
To activate caching for an action, set the isCache
setup key to true when creating the Action object.
To use a different cache provider, TTL or prefix, specify also the cacheProviderName
, cacheTtl
or cachePrefix
setup keys. See Working with Cache for more details on this concepts.
For example, if we wanted to activate the cache for the action we used in the first example on this section, it would look like this:
Furthermore, if we wanted to use a cache provider and TTL different from the default ones:
The default cache provider is called engine
, and uses APCu for a fast yet basic caching mechanism. See Working with Cache to use other more advanced alternatives like Redis.
Removing an action from cache
If you need to remove an action from cache before its TTL expiration time arrives, use the Action::clearCache, like this:
For complex actions with variable path components or parameters, you must specify the specific path components or parameters values for which you want to clear the cache as an argument.
For example, for this action that has one fixed and one variable path component to attend requests like /product/479
:
If we wanted to clear the cache for the request to the product with id 479
, we would do this:
See this example working in the Cherrycake documentation examples site.
Last updated