Cache application data
Use the cache contract and distinguish data, configuration, and page caches.
On this page
Resolve the cache contract
use Anvyr\Loom\Contracts\CacheDriver;
$cache = app(CacheDriver::class);
$cache->set('example.greeting', 'Hello, Loom.', 300);
$greeting = $cache->get('example.greeting', 'Hello.');
The TTL is in seconds. The contract also exposes has(), delete(), clear(), and remember().
Compute on a miss
$value = $cache->remember('example.computed', 300, static function () {
return ['generated_at' => time()];
});
Choose keys that identify the data, its owner, and any dimensions that affect the result. Set a TTL appropriate to how stale the result may become, or explicitly delete the key when the underlying data changes.
Do not assume that a cache lookup provides a distributed lock or guarantees only one concurrent computation.
Select a driver
Configure cache.default and driver settings in user/config/cache.php. Available drivers are file, redis, and apcu; the default is file.
Redis and APCu need their respective PHP extensions. A file cache needs a writable cache directory. Check that the environment running HTTP requests can access the same intended cache as maintenance commands.
Clear the right layer
php loom cache:clear
php loom config:clear
php loom index:rebuild
These operations have different purposes: clearing application cache, removing compiled configuration, and rebuilding content discovery. Rebuilding the page index is not a substitute for invalidating cached application data.
See cache configuration and the CacheDriver contract.