From 1a63f206fe0d2fc1b0148bd47dbafdea462d26e5 Mon Sep 17 00:00:00 2001 From: Franck Garcon Date: Tue, 10 Dec 2019 16:53:43 +0100 Subject: [PATCH] gestion du cache --- Cms/CacheManager.php | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/Cms/CacheManager.php b/Cms/CacheManager.php index dbca01f..abff15b 100644 --- a/Cms/CacheManager.php +++ b/Cms/CacheManager.php @@ -14,7 +14,7 @@ class CacheManager { private $cacheItemPool; - + private $requestStack; private $isCacheActive; @@ -26,7 +26,7 @@ public function __construct(TagAwareAdapterInterface $cacheItemPool, RequestStac $this->isCacheActive = $isCacheActive; } - public function isCacheable(BlockTypeInterface $blockType) : bool + public function isCacheable(BlockTypeInterface $blockType): bool { if (!$blockType instanceof CacheableBlockInterface) { return false; @@ -35,7 +35,7 @@ public function isCacheable(BlockTypeInterface $blockType) : bool return $this->isCacheActive && $this->requestStack->getCurrentRequest()->isMethod('GET'); } - private function getCacheConfig(BlockTypeInterface $blockType, Block $block) : array + private function getCacheConfig(BlockTypeInterface $blockType, Block $block): array { $resolver = new OptionsResolver(); $resolver->setRequired('cache_key'); @@ -43,7 +43,7 @@ private function getCacheConfig(BlockTypeInterface $blockType, Block $block) : a $resolver->setRequired('vary'); $resolver->setDefaults([ - 'cache_key' => 'block_'.$blockType->getType().'_'.$block->getId(), + 'cache_key' => 'block_' . $blockType->getType() . '_' . $block->getId(), 'expires_after' => '5 minutes', 'vary' => '', 'vary_path_info' => true, @@ -55,22 +55,22 @@ private function getCacheConfig(BlockTypeInterface $blockType, Block $block) : a return $resolver->resolve($block->getConfiguration()['cache'] ?? []); } - private function getCacheKeyWithVariation(array $config) : string + private function getCacheKeyWithVariation(array $config): string { $request = $this->requestStack->getCurrentRequest(); $key = $config['cache_key']; if ($config['vary_path_info']) { - $key .= Urlizer::urlize($request->getPathInfo()).'__'; + $key .= Urlizer::urlize($request->getPathInfo()) . '__'; } if ($config['vary_query_string'] && $request->getQueryString()) { - $key .= 'qs_'.Urlizer::urlize($request->getQueryString()).'__'; + $key .= 'qs_' . Urlizer::urlize($request->getQueryString()) . '__'; } - foreach (explode(',',$config['vary']) as $vary) { + foreach (explode(',', $config['vary']) as $vary) { $vary = trim($vary); - $key .= $vary.'_'.Urlizer::urlize($request->headers->get($vary)).'__'; + $key .= $vary . '_' . Urlizer::urlize($request->headers->get($vary)) . '__'; } return $key; @@ -94,10 +94,10 @@ public function write(BlockTypeInterface $blockType, Block $block, $content) $config = $this->getCacheConfig($blockType, $block); $cache = $this->cacheItemPool->getItem($this->getCacheKeyWithVariation($config)); - $cache->expiresAfter(\DateInterval::createFromDateString($config['expires_after'])); + $cache->expiresAfter(\DateInterval::createFromDateString($config['expires_after'] ?? '1 second')); $cache->tag($config['cache_key']); - if ($content instanceof Response) { + if ($content instanceof Response) { if (!$content->isCacheable()) { return; } @@ -118,7 +118,6 @@ public function invalidateByTag(BlockTypeInterface $blockType, Block $block) $config = $this->getCacheConfig($blockType, $block); - $this->cacheItemPool->invalidateTags([ $config['cache_key'] ]); + $this->cacheItemPool->invalidateTags([$config['cache_key']]); } - -} \ No newline at end of file +}