The framework's Cache service is a simple file cache for data.
Its methods support PSR-16. The caching works as follows:
Data is stored in the cache with a unique key, specifying a ttl in seconds.
Within this time, starting from cache creation, cache requests by this key return cached data, which remains unchanged.
The cache can be forcibly cleared by key or entirely at any time.
If the cache was not created, cleared, or expired, a new cache will be created for the specified duration.
The built-in service implementation supports main PHP data types—strings, numeric values, arrays, objects (via serialization).
If you need more advanced caching features, add another implementation to the container, replacing or supplementing the current one. This could be the github.com/symfony/cache component.
Methods for using Cache in controllers (and all classes inheriting from Hleb\Base\Container) using the example of retrieving cache by key:
Example of retrieving cache from Cache in application code:
The Cache object can also be accessed through dependency injection via the Hleb\Reference\Interface\Cache interface.
To simplify examples, further ones will only use access through Hleb\Static\Cache.
The most challenging aspect of this caching method (besides invalidation) is choosing a unique key that uniquely identifies the cached data.
For instance, if you're caching data obtained from a database with a specific query, the key should include information about this query, as well as the database name if a similar query could be made from different databases.
In this example, a test verification result will be added to the cache with an expiration period of one minute. Naturally, in real conditions, you should choose data for caching where forming it is more resource-intensive than using the cache.
The methods get(), set(), and has() have been used here respectively for retrieving, adding to the cache, and checking its existence by key.
These three methods are replaced by a single method getConform(), which operates with a Closure function to get data if they are not found in the cache.
Example with a closure function that uses an external context:
The entire cache within the framework is cleared by using the clear() method, but caution must be taken with a large amount of cache. This call should be used rather infrequently, and it can also be done via a console command:
$php console --clear-cache
Clearing the entire cache will only affect the cached template data and the framework data added by the Cache service. The TWIG templating engine has its own cache implementation, and a separate console command is provided for clearing it.
If there is a need to delete the cache by one of the keys, this can be done using the delete() method.
To have the framework automatically track the maximum cache size, you need to configure the 'max.cache.size' option in the /config/common.php file.
The value is represented as an integer in megabytes.
Due to the uneven distribution of cache in the files, this will be an approximate tracking of the maximum directory size for cache files.
If caching is not occurring, make sure the 'app.cache.on' setting is enabled in the /config/common.php file; this is recommended to be disabled in debug mode.
← Response Logging →