setFastCache() public method

Does not use cache invalidation mechanism. Notes: Not practical for load balances or php-pm scenarios. Not even practical when you use PHP in PHP-FPM, because there are several php instances as well which do nothing know about cache refreshing in this fast cache. Is only useful for configuration purposes at bootstrap or in combination with the invalidation mechanism (which is then the same as if you would call setDistributedCache())
public setFastCache ( string $key, mixed $value, integer $lifeTime = null ) : boolean
$key string
$value mixed Only simple data types. Serialize your value if you have objects/arrays.
$lifeTime integer
return boolean
Beispiel #1
0
 /**
  * @param string $lang
  * @param bool $force
  * @return array|null|string
  *
  * @throws \Jarves\Exceptions\BundleNotFoundException
  */
 public function loadMessages($lang = 'en', $force = false)
 {
     if (!$this->isValidLanguage($lang)) {
         $lang = 'en';
     }
     if ($this->messages && isset($this->messages['__lang']) && $this->messages['__lang'] == $lang && $force == false) {
         return null;
     }
     if (!$lang) {
         return null;
     }
     $code = 'core/lang/' . $lang;
     $this->messages = $this->cacher->getFastCache($code);
     $md5 = '';
     $bundles = array();
     foreach ($this->jarves->getBundles() as $bundleName => $bundle) {
         $path = $bundle->getPath();
         if ($path) {
             $path .= "/Resources/translations/{$lang}.po";
             if (file_exists($path)) {
                 $md5 .= @filemtime($path);
                 $bundles[] = $bundleName;
             }
         }
     }
     $md5 = md5($md5);
     if (!$this->messages || count($this->messages) == 0 || !isset($this->messages['__md5']) || $this->messages['__md5'] != $md5) {
         $this->messages = array('__md5' => $md5, '__plural' => $this->getPluralForm($lang), '__lang' => $lang);
         foreach ($bundles as $key) {
             $file = $this->jarves->resolvePath("@{$key}/{$lang}.po", 'Resources/translations');
             $po = $this->getLanguage($file);
             $this->messages = array_merge($this->messages, $po['translations']);
         }
         $this->cacher->setFastCache($code, $this->messages);
     }
     include_once $this->getPluralPhpFunctionFile($lang);
     return $this->messages;
 }
Beispiel #2
0
 /**
  * Creates a Node object based on given $routeName or current route.
  *
  * @param string|null $routeName
  *
  * @return Node
  */
 public function createPageFromRoute($routeName = null)
 {
     if (!$routeName) {
         $routeName = $this->pageStack->getRequest()->attributes->get('_route');
         if (!$routeName) {
             throw new \RuntimeException('Could not detect route name');
         }
     }
     $reflection = new \ReflectionClass($this->router->getGenerator());
     $key = 'jarves_routes';
     $cache = $this->cacher->getFastCache($key);
     $validCache = false;
     $routes = [];
     if ($cache) {
         $validCache = $cache['time'] === filemtime($reflection->getFileName()) && isset($cache['routes']) && is_string($cache['routes']);
         if ($validCache) {
             $routes = unserialize($cache['routes']);
         }
     }
     if (!$validCache) {
         $routes = $this->router->getRouteCollection()->all();
         $this->cacher->setFastCache($key, ['time' => filemtime($reflection->getFileName()), 'routes' => serialize($routes)]);
     }
     if (!isset($routes[$routeName])) {
         throw new \RuntimeException("Route with name `{$routeName}` does not exist");
     }
     $route = $routes[$routeName];
     $url = $this->router->generate($routeName, $this->pageStack->getRequest()->attributes->all());
     $page = Node::createPage($route->getOption('title'), parse_url($url)['path'], $route->getOption('theme'), $route->getOption('layout'));
     if ($route->getOption('meta')) {
         foreach ((array) $route->getOption('meta') as $key => $value) {
             $page->meta->set($key, $value);
         }
     }
     return $page;
 }
Beispiel #3
0
 /**
  * Loads all configurations from all registered bundles (BundleName/Resources/config/jarves*.xml)
  * @param Cacher $cacher
  *
  * @return null|callable returns a callable that should be called when config stuff have been registered
  */
 public function loadBundleConfigs(Cacher $cacher)
 {
     $cached = $cacher->getFastCache('core/configs');
     $bundles = array_keys($this->kernel->getBundles());
     $configs = new Configuration\Configs($this);
     $hashes = [];
     foreach ($bundles as $bundleName) {
         $hashes[] = $configs->getConfigHash($bundleName);
     }
     $hash = md5(implode('.', $hashes));
     if ($cached) {
         $cached = unserialize($cached);
         if (is_array($cached) && $cached['md5'] == $hash) {
             $this->configs = $cached['data'];
             $this->configs->setCore($this);
         }
     }
     if (!$this->configs) {
         $this->configs = new Configuration\Configs($this, $bundles);
         return function () use($hash, $cacher) {
             $cached = serialize(['md5' => $hash, 'data' => $this->configs]);
             $cacher->setFastCache('core/configs', $cached);
         };
     }
 }