J'ai testé pour vous : le composant Cache de Symfony couplé à Redis
key_1: 'value_1'
key_2: 2
key_3: '{firstName: Ando, lastName: Larz}'# config/packages/cache.yaml
framework:
cache:
default_redis_provider: 'redis://%env(REDIS_CACHE_HOST)%:%env(REDIS_CACHE_PORT)%'
pools:
# Individual pools for different product cache usages
product.cache_pool:
adapter: 'cache.adapter.redis_tag_aware'
tags: true
default_lifetime: 28800
# Other pools ...$this->productCachePool->get(
$productCacheKey,
function (ItemInterface $item) use ($productId): CacheableProductDto {
$product = $this->productRepository->find($productId);
if (null === $product) {
throw new NotFoundHttpException();
}
$item->tag('product_tag');
$item->expiresAfter('3600');
return $cacheableProductDto;
}
);Administration du cache par les admins
Impliquent des manipulations spécifiques
trait CacheKeysTrait
{
private readonly \Redis $redisCache;
/**
* @return array<int, string>
*/
private function scanKeys(string $searchPattern): array
{
$cursor = null;
$keys = [];
do {
$retrievedKeys = $this->redisCache->scan($cursor, $searchPattern);
if (false !== $retrievedKeys) {
foreach ($retrievedKeys as $key) {
$keys[] = $key;
}
}
} while (0 !== $cursor);
return $keys;
}
}jamld6hgt:product_desert_marocain
Filtrer les clés qui sont des tags
return b($poolCompatibleKey)->startsWith("\x01");foreach ($this->cachePools as $cachePool) {
if (true === $cachePool->hasItem($poolCompatibleKey)) {
return $cachePool->getItem($poolCompatibleKey);
}
}Même fonctionnement
TODO
foreach ($this->cachePools as $cachePool) {
if (true === $cachePool->hasItem($poolCompatibleKey)) {
$deletionSuccess = $cachePool->deleteItem($poolCompatibleKey);
if (false === $deletionSuccess) {
$failedDeletions[] = $key;
}
break;
}
}if (false === $this->redisCache->get($cacheKey)) {
$this->logger->warning('Tried to delete {cacheKey} cache key, but it was not found.', [
'cacheKey' => $cacheKey,
]);
return;
}Idéal pour stocker et récupérer des items en cache
Pour les besoins plus avancés :
QR Code pour les retours