Example #1
0
 public function cache(array $params, $content, $template, &$repeat)
 {
     $key = $this->getParam($params, 'key');
     if (null === $key || empty($key)) {
         throw new \InvalidArgumentException("Missing 'key' parameter in cache arguments");
     }
     $ttl = (int) $this->getParam($params, 'ttl');
     if (null === $ttl) {
         throw new \InvalidArgumentException("Missing 'ttl' parameter in cache arguments");
     }
     if ($this->debug || $ttl < 1) {
         if (null !== $content) {
             $repeat = false;
             return $content;
         }
         return null;
     }
     /** @var CacheItemInterface $cacheItem */
     $cacheItem = $this->adapter->getItem($this->generateKey($params));
     if ($cacheItem->isHit()) {
         $repeat = false;
         return $cacheItem->get();
     }
     if ($content !== null) {
         $cacheItem->expiresAfter((int) $params['ttl'])->set($content);
         $this->adapter->save($cacheItem);
         $repeat = false;
         return $cacheItem->get();
     }
 }
Example #2
0
 public function cacheClear(CacheEvent $event)
 {
     // clear cache on thelia.cache service
     $this->adapter->clear();
     $dir = $event->getDir();
     $fs = new Filesystem();
     $fs->remove($dir);
 }
Example #3
0
 /**
  * @param AdapterInterface $cache
  * @param string $cacheKey
  * @param int $ttl
  * @return Response
  */
 private function handleCached(AdapterInterface $cache, string $cacheKey, int $ttl) : Response
 {
     $this->info(sprintf('hit: fetch from cache: %s', $cacheKey), ['application' => 'cache', 'type' => 'hit', 'cacheKey' => $cacheKey, 'ttl' => $ttl]);
     /** @var Response $response */
     $response = $cache->getItem($cacheKey)->get();
     $response->headers->set('X-Cache', 'hit');
     $response->setMaxAge($ttl);
     $response->setExpires(new DateTime(sprintf('+%d seconds', $ttl)));
     return $response;
 }