/** * @param string $name * @return string */ public function link($name) { $path = $this->cache->load([$name, $this->debugMode]); $files = $this->files[$name]; $files = is_string($files) ? [$files] : $files; if ($path === NULL) { $unpackedFiles = $this->unpack($name, $files); $time = $this->getModifyTime($unpackedFiles); $path = $this->genDir . '/' . $this->getOutputFilename($name, $unpackedFiles, $time, $this->debugMode); $this->cache->save([$name, $this->debugMode], $path); } $genFile = "{$this->wwwDir}/{$path}"; if (!file_exists($genFile) || $this->debugMode && filemtime($genFile) < (isset($time) ? $time : ($time = $this->getModifyTime($this->unpack($name, $files))))) { $start = microtime(TRUE); $parsedFiles = $this->compile($this->unpack($name, $files), $genFile); if ($this->debugMode) { $this->statistics[$name]['time'] = microtime(TRUE) - $start; $this->statistics[$name]['parsedFiles'] = $parsedFiles; } } if ($this->debugMode) { $unpackedFiles = $this->unpack($name, $files); $this->statistics[$name]['size'] = filesize($genFile); $this->statistics[$name]['file'] = count($unpackedFiles) > 1 ? $unpackedFiles : reset($unpackedFiles); $this->statistics[$name]['date'] = isset($time) ? $time : ($time = $this->getModifyTime($unpackedFiles)); $this->statistics[$name]['path'] = $path; } return $path; }
private function loadNavigationFromCache($navigationId) { return $this->cache->load("nav-{$navigationId}", function (&$dependencies) use($navigationId) { $nodes = $this->navigationReader->getEntireNavigation($navigationId); return $this->treeBuilder->buildTree($nodes); }); }
/** * @param string $alias * @param \ReflectionClass $context * @return string * @throws Exception\NamespaceNotFoundException */ static function getClass($alias, \ReflectionClass $context) { if (!strlen($alias)) { return $alias; } if (strncmp($alias, '\\', 1) === 0) { return substr($alias, 1); } $file = $context->getFileName(); if (!isset(self::$map[$file])) { if (self::$cache === NULL) { $list = Parser::parse($file); } else { $key = self::C_FILE . $file; $list = self::$cache->load($key); if ($list === NULL) { $list = self::$cache->save($key, Parser::parse($file), array(NCache::FILES => array($file))); } } self::$map[$file] = $list; } $namespace = $context->getNamespaceName(); if (!isset(self::$map[$file][$namespace])) { throw new Exception\NamespaceNotFoundException("Namespace '{$namespace}' not found in '{$file}'."); } $parts = explode('\\', $alias); $first = array_shift($parts); if (!isset(self::$map[$file][$namespace][$first])) { return ltrim(trim($namespace, '\\') . '\\', '\\') . $alias; } $appendix = implode('\\', $parts); return self::$map[$file][$namespace][$first] . (strlen($appendix) ? '\\' . $appendix : ''); }
/** * Renders template to output. * @return void */ public function render() { if ($this->file == NULL) { // intentionally == throw new Nette\InvalidStateException("Template file name was not specified."); } $cache = new Caching\Cache($storage = $this->getCacheStorage(), 'Nette.FileTemplate'); if ($storage instanceof Caching\Storages\PhpFileStorage) { $storage->hint = str_replace(dirname(dirname($this->file)), '', $this->file); } $cached = $compiled = $cache->load($this->file); if ($compiled === NULL) { try { $compiled = "<?php\n\n// source file: {$this->file}\n\n?>" . $this->compile(); } catch (FilterException $e) { $e->setSourceFile($this->file); throw $e; } $cache->save($this->file, $compiled, array(Caching\Cache::FILES => $this->file, Caching\Cache::CONSTS => 'Nette\\Framework::REVISION')); $cached = $cache->load($this->file); } if ($cached !== NULL && $storage instanceof Caching\Storages\PhpFileStorage) { Nette\Utils\LimitedScope::load($cached['file'], $this->getParameters()); } else { Nette\Utils\LimitedScope::evaluate($compiled, $this->getParameters()); } }
/** * Renders template to output. * @return void */ public function render() { if ($this->file == NULL) { // intentionally == throw new Nette\InvalidStateException('Template file name was not specified.'); } if (!$this->getFilters()) { $this->onPrepareFilters($this); } if ($latte = $this->getLatte()) { return $latte->setLoader(new Latte\Loaders\FileLoader())->render($this->file, $this->getParameters()); } $cache = new Caching\Cache($storage = $this->getCacheStorage(), 'Nette.FileTemplate'); if ($storage instanceof Caching\Storages\PhpFileStorage) { $storage->hint = str_replace(dirname(dirname($this->file)), '', $this->file); } $cached = $compiled = $cache->load($this->file); if ($compiled === NULL) { try { $compiled = "<?php\n\n// source file: {$this->file}\n\n?>" . $this->compile(); } catch (FilterException $e) { throw $e->setSource(file_get_contents($this->file), $e->sourceLine, $this->file); } $cache->save($this->file, $compiled, array(Caching\Cache::FILES => $this->file, Caching\Cache::CONSTS => 'Nette\\Framework::REVISION')); $cached = $cache->load($this->file); } $isFile = $cached !== NULL && $storage instanceof Caching\Storages\PhpFileStorage; self::load($isFile ? $cached['file'] : $compiled, $this->getParameters(), $isFile); }
/** * @param $class * @param array $types * @return string * @throws \Nette\InvalidArgumentException */ public function prepareType($class, array $types = array()) { $class = trim($class, '\\'); $key = serialize(array('class' => $class, 'types' => $types)); if (!isset($this->loaded[$key])) { $newClass = $this->prepareClassName($class, $types); if ($this->storage) { $cache = new Cache($this->storage, 'Venne.Generics'); $data = $cache->load($key); if (!$data) { $data = $this->prepareClassTemplate($class, $newClass, $types); $cache->save($key, $data); $data = $cache->load($key); } if ($this->storage instanceof PhpFileStorage) { \Nette\Utils\LimitedScope::load($data['file']); } else { \Nette\Utils\LimitedScope::evaluate($data); } } else { $data = $this->prepareClassTemplate($class, $newClass, $types); \Nette\Utils\LimitedScope::evaluate($data); } $this->loaded[$key] = $newClass; } return $this->loaded[$key]; }
public function actionDefault() { $cache = new Cache($this->context->cacheStorage, 'Homepage'); $counts = $cache->load('homepagecounts'); if ($counts === NULL) { $subjectsCount = $this->context->createServicePlaces()->fetchVisible()->count(); $eventsCount = $this->context->createServiceTimes()->fetchPublic()->group('event_time.event_id')->count(); $categoriesCount = $this->context->createServiceCategories()->where('subject', '1')->count(); $counts = array('sc' => $subjectsCount, 'ec' => $eventsCount, 'cc' => $categoriesCount); $cache->save('homepagecounts', $counts, array(Cache::EXPIRE => '1 hour', Cache::SLIDING => true, Cache::TAGS => array('event', 'events', 'places', 'place'))); } $this->template->subjectsCount = $counts['sc']; $this->template->eventsCount = $counts['ec']; $this->template->categoriesCount = $counts['cc']; $def = $cache->load('homepagecities'); if ($def === NULL) { $res = Model\Subjects::fetchLocalitiesToCities(); $cnt = 0; foreach ($res as $r) { $cnt = $cnt + $r['cnt']; } $def = array($res, $cnt); $cache->save('homepagecities', $def, array('expire' => 100000, 'tags' => 'cities')); } $this->template->cities = $def[0]; $this->template->citiesCount = $def[1]; $this->template->circles = $this->context->createServiceCircles()->order('shift')->where('visible', 1)->limit(4); }
public function findEventsByType($logTypeID) { return $this->cache->load('logEvents-' . $logTypeID, function (&$dependencies) use($logTypeID) { return array_column($this->em->createQuery('SELECT e.id, e.name FROM ' . EventLog::class . ' e INDEX BY e.id WHERE e.logType = :typeID')->setParameter('typeID', $logTypeID)->getArrayResult(), 'name', 'id'); }); }
public function build(array $nodes = NULL) { if (!$this->built) { $key = sprintf('nodes-%d', $this->language->getCurrent()->id); if ($this->cache->load($key) === NULL) { $rows = $this->repository->findAll(); $nodes = []; $parents = []; $children = []; foreach ($rows as $node) { $this->onFetch($node); $nodes[$node->id] = json_encode($node); if ($node->parent === NULL) { $parents[$node->order] = $node->id; } else { $children[$node->parent->id][$node->order] = $node->id; } } $this->cache->save($key, [$nodes, $parents, $children], [Cache::TAGS => [get_called_class()]]); } list($nodes, $this->parents, $this->children) = $this->cache->load($key); foreach ($nodes as $node) { $node = $this->repository->createUnserializedEntity($node); $this->nodes[$node->id] = $node; $this->urls[$node->url] = $node->id; if ($node->root) { $this->root = $node; } } $this->onBuild(); $this->built = true; } }
/** * @param Translator $translator * @param MessageCatalogueInterface[] $availableCatalogues * @param string $locale * @throws InvalidArgumentException * @return MessageCatalogueInterface|NULL */ public function compile(Translator $translator, array &$availableCatalogues, $locale) { if (empty($locale)) { throw new InvalidArgumentException("Invalid locale."); } if (isset($availableCatalogues[$locale])) { return $availableCatalogues; } $cacheKey = array($locale, $translator->getFallbackLocales()); $storage = $this->cache->getStorage(); if (!$storage instanceof Kdyby\Translation\Caching\PhpFileStorage) { if (($messages = $this->cache->load($cacheKey)) !== NULL) { $availableCatalogues[$locale] = new MessageCatalogue($locale, $messages); return $availableCatalogues; } $this->catalogueFactory->createCatalogue($translator, $availableCatalogues, $locale); $this->cache->save($cacheKey, $availableCatalogues[$locale]->all()); return $availableCatalogues; } $storage->hint = $locale; $cached = $compiled = $this->cache->load($cacheKey); if ($compiled === NULL) { $this->catalogueFactory->createCatalogue($translator, $availableCatalogues, $locale); $this->cache->save($cacheKey, $compiled = $this->compilePhpCache($translator, $availableCatalogues, $locale)); $cached = $this->cache->load($cacheKey); } $availableCatalogues[$locale] = self::load($cached['file']); return $availableCatalogues; }
/** * Search configuration files. * @param mixed * @param mixed */ public function addFindConfig($dirs, $exclude = NULL) { $cache = new Caching\Cache(new Caching\Storages\FileStorage($this->getCacheDirectory()), self::Caching); // Search will be started only when the cache does not exist. if (!$cache->load(self::Caching)) { // Search configuration files. foreach (Utils\Finder::findFiles('*.neon')->from($dirs)->exclude($exclude) as $row) { $data[] = $row->getPathname(); } foreach ($data as $row) { $name[] = basename($row); } // Sort found files by number and put into the cache. array_multisort($name, SORT_NUMERIC, $data); if (isset($data)) { $cache->save(self::Caching, $data); } } // Loads the data from the cache. if ($cache->load(self::Caching)) { foreach ($cache->load(self::Caching) as $files) { $this->addConfig($files); } } }
public function load($key, $callback) { return $this->cache->load($key, function (&$dependencies) use($callback) { $dependencies = $this->dependencies; return call_user_func($callback); }); }
public function setConnection(Nette\Database\Connection $connection) { $this->connection = $connection; if ($this->cacheStorage) { $this->cache = new Nette\Caching\Cache($this->cacheStorage, 'Nette.Database.' . md5($connection->getDsn())); $this->structure = $this->cache->load('structure') ?: $this->structure; } }
/** * @param $namespace * @return array */ public function loadCachedNamespace($namespace) { if (isset($this->loadedData[$namespace])) { return $this->loadedData[$namespace]; } else { return $this->loadedData[$namespace] = $this->cache->load($namespace); } }
/** * @param string * @return mixed|NULL */ public function load($key) { $cached = $this->cache->load($key); if (!is_array($cached) || $cached[1] !== $this->expire) { return NULL; } return $cached[0]; }
public function handleLoadExceptions() { $this->importService->import(); $this->template->lastUpdate = $this->cache->load("lastUpdate", function () { return null; }); $this->redrawControl(); }
/** * @param string $oldAgencyId * @return string */ private function getAgencyId($oldAgencyId) { $newId = $this->agencyIdsCache->load($oldAgencyId); if ($newId === null) { throw new InvalidStateException('Unknown agency ID: ' . $oldAgencyId); } return $newId; }
/** * Create resources route list * @param string|null $module * @return ResourceRouteList */ public function create($module = NULL) { $routeList = $this->cache->load(self::CACHE_NAME); if ($routeList !== NULL) { return $routeList; } return $this->createCached($module); }
/** * @param string $oldStopId * @return string */ private function getStopId($oldStopId) { $newId = $this->stopsIdsCache->load($oldStopId); if ($newId === null) { throw new InvalidStateException('Unknown stop ID: ' . $oldStopId); } return $newId; }
/** * Create autodiscovery structure. */ public function __construct(Nette\Database\Connection $connection, Nette\Caching\IStorage $cacheStorage = NULL) { $this->connection = $connection; if ($cacheStorage) { $this->cache = new Nette\Caching\Cache($cacheStorage, 'Nette.Database.' . md5($connection->getDsn())); $this->structure = $this->loadedStructure = $this->cache->load('structure') ?: array(); } }
public function __invoke($url, string $directory = NULL, string $parameter = NULL) : string { $arguments = [$url, $directory ?: $this->directory, $parameter ?: $this->parameter]; return $this->cache ? $this->cache->load($arguments, function (&$dependencies) use($arguments) { $dependencies[Nette\Caching\Cache::EXPIRE] = $this->expire; $arguments[] =& $dependencies; return $this->process(...$arguments); }) : $this->process(...$arguments); }
/** * Save data into cache * * @param string $key * @param mixed $callback * @param array|NULL $params * @return mixed|NULL */ public function storeValue($key, $callback, array $params = NULL) { // load from cache $savedData = $this->cache->load($key); if (!empty($savedData)) { return $savedData; } return $this->cache->save($key, $callback, $params); }
/** * @return Collection */ public function getAll() { $collection = $this->cache->load(self::CACHE_COLLECTION, function (&$dependencies) { $dependencies[Cache::TAGS] = ['blog', 'posts']; $dependencies[Cache::EXPIRATION] = '+24 hour'; return $this->collector->collect(); }); return $collection; }
/** * {@inheritdoc} */ public function isAllowed($role, $resource, $privilege) { if (NULL === ($allowed = $this->cache->load([$role, $resource, $privilege]))) { $allowed = $this->cache->save([$role, $resource, $privilege], function () use($role, $resource, $privilege) { return $this->authorizator->isAllowed($role, $resource, $privilege); }, [Cache::TAGS => ['role/' . serialize($role), 'resource/' . serialize($resource), 'privilege/' . serialize($privilege)]]); } return $allowed; }
/** * @param Environment $oEnvironment * @param IStorage $oStorage * @throws BadApplicationException */ public function __construct(Environment $oEnvironment, IStorage $oStorage) { $this->oEnvironment = $oEnvironment; $this->oCache = new Cache($oStorage); $this->aApplications = $this->oCache->load(self::ALL_APPLICATION_CACHE_KEY); if (NULL === $this->aApplications) { $this->loadApplications(); } }
/** * @return mixed */ public function createMenu() { $neon = $this->cache->load('menu'); if ($neon === NULL) { $neon = Neon::decode(file_get_contents($this->filename)); $this->cache->save('menu', $neon, array(Cache::FILES => $this->filename)); } return $neon; }
/** * Create autodiscovery structure. */ public function __construct(Nette\Database\Connection $connection, Nette\Caching\IStorage $cacheStorage = NULL) { trigger_error(__CLASS__ . '() is deprecated; use Nette\\Database\\Conventions\\DiscoveredConventions instead.', E_USER_DEPRECATED); $this->connection = $connection; if ($cacheStorage) { $this->cache = new Nette\Caching\Cache($cacheStorage, 'Nette.Database.' . md5($connection->getDsn())); $this->structure = $this->loadedStructure = $this->cache->load('structure') ?: array(); } }
public function findAllLocales() { return $this->cache->load('locales', function () { $locales = $this->em->createQuery('SELECT l FROM ' . Locale::class . ' l INDEX BY l.name')->getArrayResult(); if (empty($locales)) { return []; } return $locales; }); }
/** * @param string $path * @return HashMap */ public function load($path) { if (!file_exists($path)) { throw new FileNotFoundException("File '{$path}' not found."); } return HashMap::from($this->cache->load($path, function (&$dependencies) use($path) { $dependencies[Cache::FILES] = $path; return Neon::decode(file_get_contents($path)); }), TRUE); }
/** * * @return ArrayObject[][]|\DIPcms\Scripter\CacheObject */ private function getCache() { $source = $this->cache->load('scripter'); $source = $source ? $source : array(); if (!isset($source[$this->cache_file_name])) { $source[$this->cache_file_name] = array(); } $this->cache->save('scripter', $source); return $source; }