Example #1
0
 /**
  * @inheritdoc
  */
 public function createService(ServiceLocatorInterface $serviceLocator)
 {
     $cacheManager = new CacheManager();
     $cacheManager->setCountry(CountryResolver::getCountry());
     $cacheManager->setLanguageProvider($serviceLocator->get('framework.language-provider'));
     $options = $serviceLocator->get('Cache\\Options');
     $cacheManager->setCacheService($serviceLocator->get($options->getCacheServiceKey()));
     return $cacheManager;
 }
Example #2
0
 /**
  * If the action is meant to be cached, stops the propagation, and inject the CacheModel to his parent.
  *
  * @param MvcEvent $e
  * @return mixed
  */
 public function onDispatchPre(MvcEvent $e)
 {
     $key = $this->getKey($e->getRouteMatch());
     if (!array_key_exists($key, $this->config)) {
         return $e;
     }
     $cacheKeyConfig = $this->config[$key];
     $key .= self::KEY_SEPARATOR . CountryResolver::getCountry() . self::KEY_SEPARATOR . $this->getLanguageProvider()->getCurrentLanguage();
     if (isset($cacheKeyConfig['count'])) {
         $key .= self::KEY_SEPARATOR . mt_rand(1, $cacheKeyConfig['count']);
     }
     if (isset($cacheKeyConfig['route_params']) && true === $cacheKeyConfig['route_params']) {
         $routeParams = $e->getRouteMatch()->getParams();
         $key .= self::KEY_SEPARATOR . md5(serialize($routeParams));
     }
     $cache = $this->getCacheService($key, $cacheKeyConfig['ttl']);
     // Custom param set to be caught by the onDispatchPost method
     $e->setParam(self::EVENT_PARAM_KEY, $key);
     // If our cache requirements are met
     if (null !== ($html = $cache->getItem($key))) {
         $result = new CacheModel();
         $result->setContent($html);
         $result->setIsFetchable(true);
         $result->setCacheKey($key);
         $e->setResult($result);
         $e->stopPropagation(true);
         $model = $e->getViewModel();
         $model->addChild($result);
         return $e->getResult();
     }
 }