The Spiral Framework provides a component for data caching. The component provides implementation of PSR-16
cache storage in files
and PHP arrays
.
It also provides an easy way to integrate your own storages by configuration file.
The component is available by default in the application bundle.
To enable the component, you just need to add Spiral\Cache\Bootloader\CacheBootloader
to the bootloader's list,
which is located in the class of your application.
protected const LOAD = [
// ...
\Spiral\Cache\Bootloader\CacheBootloader::class,
// ...
];
The configuration file for this component should be located at app/config/cache.php
. Within this file, you may
configure available storages and default storage.
For example, the configuration file might look like this:
use Spiral\Cache\Storage\ArrayStorage;
use Spiral\Cache\Storage\FileStorage;
return [
/**
* -------------------------------------------------------------------------
* Default storage
* -------------------------------------------------------------------------
*
* The key of one of the registered cache storages to use by default.
*/
'default' => env('CACHE_STORAGE', 'array'),
/**
* -------------------------------------------------------------------------
* Aliases
* -------------------------------------------------------------------------
*
* Aliases, if you want to use domain specific storages.
*/
'aliases' => [
'user-data' => 'array'
],
/**
* -------------------------------------------------------------------------
* Storages
* -------------------------------------------------------------------------
*
* Here you may define all of the cache "storages" for your application as well as their types.
*/
'storages' => [
'array' => [
'type' => 'array',
],
'file' => [
'type' => 'file',
'path' => directory('runtime') . 'cache',
],
],
/**
* -------------------------------------------------------------------------
* Aliases for storage types
* -------------------------------------------------------------------------
*/
'typeAliases' => [
'array' => ArrayStorage::class,
'file' => FileStorage::class,
],
];
To work with the cache, you can use Psr\SimpleCache\CacheInterface
or Spiral\Cache\CacheManager
.
The cache storage can be injected from a container using the Psr\SimpleCache\CacheInterface
. It will refer to the
default storage.
namespace App\Service;
use Psr\SimpleCache\CacheInterface;
class MyService
{
public function __construct(
private readonly CacheInterface $cache,
) {
}
public function someMethod(): void
{
// save data in the cache
$this->cache->set(key: 'key', value: ['some' => 'data'], ttl: 3600);
\dump($this->cache->has('key')); // true
// retrieve data from the cache
$data = $this->cache->get('key');
// delete data from the cache
$this->cache->delete('key');
// delete all data
$this->cache->clear();
// work with multiple items
$this->cache->setMultiple(values: ['key' => ['some' => 'data'], 'other' => ['foo' => 'bar']], ttl: 3600);
$data = $this->cache->getMultiple(['key', 'other']);
$this->cache->deleteMultiple(['key', 'other']);
}
}
Using the Spiral\Cache\CacheManager
, you can get a specific storage by storage string key from config.
namespace App\Service;
use Spiral\Cache\CacheManager;
class MyService
{
public function __construct(
private readonly CacheManager $cache,
) {
}
public function someMethod(): void
{
// save data in the cache
$this->cache
->storage('array')
->set(key: 'key', value: ['some' => 'data'], ttl: 3600);
\dump($this->cache->storage('array')->has('key')); // true
// retrieve data from the cache
$data = $this->cache
->storage('array')
->get('key');
// delete data from the cache
$this->cache
->storage('array')
->delete('key');
// delete all data
$this->cache
->storage('array')
->clear();
// work with multiple items
$this->cache
->storage('array')
->setMultiple(values: ['key' => ['some' => 'data'], 'other' => ['foo' => 'bar']], ttl: 3600);
$data = $this->cache
->storage('array')
->getMultiple(['key', 'other']);
$this->cache
->storage('array')
->deleteMultiple(['key', 'other']);
}
}
Note
You can use thecache
andcacheManager
prototype properties.
Event | Description |
---|---|
Spiral\Cache\Event\CacheHit | The Event will be fired when data is successfully retrieved from the cache. |
Spiral\Cache\Event\CacheMissed | The Event will be fired if the requested data is not found in the cache. |
Spiral\Cache\Event\KeyDeleted | The Event will be fired after the data is removed from the cache. |
Spiral\Cache\Event\KeyWritten | The Event will be fired after the data is stored in the cache. |
Note
To learn more about dispatching events, see the Events section in our documentation.