1. Go to this page and download the library: Download ruvents/spiral-cache library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ruvents / spiral-cache example snippets
use Ruvents\SpiralCache\CacheBootloader;
class App extends Kernel
{
protected const LOAD = [
...
CacheBootloader::class,
]
}
declare(strict_types=1);
return [
// Array of named cache pools.
'pools' => [
'localCache' => new ArrayAdapter(), // can be any object that implements CacheItemPoolInterface
],
// Optional, default value is 'default'. Item with specified key must be
// present in 'pools' array.
'default' => 'localCache',
// Optional, will use default cache pool if omitted. Item with specified key
// must be present in 'pools' array.
'controllerPool' => 'localCache',
];
use Psr\Cache\CacheItemPoolInterface;
/**
* @Route('/list', methods="GET")
*/
public function list(CacheItemPoolInterface $localCache): ResponseInterface
{
if ($localCache->hasItem('list')) {
return $localCache->getItem('list')->get();
}
$response = ...;
$item = $localCache->getItem('list');
$item->set($response);
$localCache->save($item);
return $response;
}
use Psr\SimpleCache\CacheInterface;
/**
* @Route('/list', methods="GET")
*/
public function list(CacheInterface $localCache): ResponseInterface
{
if ($localCache->has('list')) {
return $localCache->get('list');
}
$response = ...;
$localCache->set('list', $response);
return $response;
}
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
/**
* @Route('/list', methods="GET")
*/
public function list(CacheInterface $localCache): ResponseInterface
{
return $localCache->get('list', static function (ItemInterface $item) {
$item->expiresAfter(3600);
$response = ...;
return $response;
});
}
use Symfony\Contracts\Cache\TagAwareCacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
/**
* @Route('/list', methods="GET")
*/
public function list(TagAwareCacheInterface $localCache): ResponseInterface
{
if ($localCache->hasItem('list')) {
return $localCache->getItem('list')->get();
}
$response = ...;
$item = $localCache->getItem('list');
$item->set($response);
$item->tag(['api.list']);
$localCache->save($item);
return $response;
}
use Ruvents\SpiralCache\Annotation\Cached;
/**
* @Route('/list', methods="GET")
*/
#[Cached('+4 hours')]
public function list(): ResponseInterface
{
$response = ...;
return $response;
}
use Ruvents\SpiralCache\Annotation\Cached;
/**
* @Route('/list', methods="GET")
*/
#[Cached('+4 hours', key: [self::class, 'keyGenerator'])]
public function list(): ResponseInterface
{
$response = ...;
return $response;
}
public static function keyGenerator(array $context): string
{
$key = ...; // any logic here
return $key;
}
use Ruvents\SpiralCache\Annotation\Cached;
#[Cached('+4 hours', if: [self::class, 'cacheCondition'])]
public function list(): ResponseInterface
{
$response = ...;
return $response;
}
public static function cacheCondition(array $context): string
{
$key = ...; // any logic here
return $key;
}
use Ruvents\SpiralCache\Annotation\Cached;
#[Cached('+4 hours', tags: ['api.list'])]
public function list(): ResponseInterface
{
$response = ...;
return $response;
}
... somewhere else in code ...
public function clearCache(TagAwareCacheInterface $localCache): string
{
$localCache->invalidateTags(['api.list']);
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.