/** * Stops and saves the cache. * @param array dependencies * @return void */ public function end(array $dp = NULL) { if ($this->cache === NULL) { throw new InvalidStateException('Output cache has already been saved.'); } $this->cache->save($this->key, ob_get_flush(), (array) $dp + (array) $this->dependencies); $this->cache = NULL; }
/** * Filters the given file. * @param string * @return string|NULL */ public function processFile($path) { $data = $this->cache->load($path); if ($data === NULL) { $data = $this->filter->processFile($path); if ($data !== NULL) { $this->cache->save($path, $data, array(Cache::CONSTS => 'FilterStream\\FilterStream::REVISION', Cache::FILES => array($path))); } } return $data; }
function render() { if ($this->getFile() == NULL) { throw new InvalidStateException("Template file name was not specified."); } $cache = new NCache($storage = $this->getCacheStorage(), 'wplatte'); if ($storage instanceof NPhpFileStorage) { $storage->hint = str_replace(dirname(dirname($this->getFile())), '', $this->getFile()); } $cached = $compiled = $cache->load($this->getFile()); if ($compiled === NULL) { try { $compiled = "<?php\n\n// source file: {$this->getFile()}\n\n?>" . $this->compile(); } catch (NTemplateException $e) { $e->setSourceFile($this->getFile()); throw $e; } $cache->save($this->getFile(), $compiled, array(NCache::FILES => $this->getFile(), NCache::CONSTS => array('NFramework::REVISION', 'WPLATTE_CACHE_VERSION'))); $cache->release(); $cached = $cache->load($this->getFile()); } if ($cached !== NULL && $storage instanceof NPhpFileStorage) { NLimitedScope::load($cached['file'], $this->getParams()); } else { NLimitedScope::evaluate($compiled, $this->getParams()); } }
/** * Renders template to output. * @return void */ public function render() { $cache = new NCache($storage = $this->getCacheStorage(), 'Nette.Template'); $cached = $compiled = $cache->load($this->source); if ($compiled === NULL) { $compiled = $this->compile(); $cache->save($this->source, $compiled, array(NCache::CONSTS => 'NFramework::REVISION')); $cached = $cache->load($this->source); } if ($cached !== NULL && $storage instanceof NPhpFileStorage) { NLimitedScope::load($cached['file'], $this->getParameters()); } else { NLimitedScope::evaluate($compiled, $this->getParameters()); } }
function render() { if ($this->file == NULL) { throw new InvalidStateException("Template file name was not specified."); } $this->__set('template', $this); $shortName = str_replace(NEnvironment::getVariable('appDir'), '', $this->file); $cache = new NCache($this->getCacheStorage(), 'Nette.Template'); $key = trim(strtr($shortName, '\\/@', '.._'), '.') . '-' . md5($this->file); $cached = $content = $cache[$key]; if ($content === NULL) { if (!$this->getFilters()) { $this->onPrepareFilters($this); } if (!$this->getFilters()) { NLimitedScope::load($this->file, $this->getParams()); return; } $content = $this->compile(file_get_contents($this->file), "file …{$shortName}"); $cache->save($key, $content, array(NCache::FILES => $this->file, NCache::EXPIRE => self::$cacheExpire)); $cache->release(); $cached = $cache[$key]; } if ($cached !== NULL && self::$cacheStorage instanceof NTemplateCacheStorage) { NLimitedScope::load($cached['file'], $this->getParams()); fclose($cached['handle']); } else { NLimitedScope::evaluate($content, $this->getParams()); } }
function render() { if ($this->file == NULL) { throw new InvalidStateException("Template file name was not specified."); } $this->__set('template', $this); $cache = new NCache($storage = $this->getCacheStorage(), 'Nette.FileTemplate'); if ($storage instanceof NTemplateCacheStorage) { $storage->hint = str_replace(dirname(dirname($this->file)), '', $this->file); } $cached = $content = $cache[$this->file]; if ($content === NULL) { try { $content = $this->compile(file_get_contents($this->file)); $content = "<?php\n\n// source file: {$this->file}\n\n?>{$content}"; } catch (NTemplateException $e) { $e->setSourceFile($this->file); throw $e; } $cache->save($this->file, $content, array(NCache::FILES => $this->file, NCache::EXPIRATION => self::$cacheExpire, NCache::CONSTS => 'NFramework::REVISION')); $cache->release(); $cached = $cache[$this->file]; } if ($cached !== NULL && $storage instanceof NTemplateCacheStorage) { NLimitedScope::load($cached['file'], $this->getParams()); fclose($cached['handle']); } else { NLimitedScope::evaluate($content, $this->getParams()); } }
/** * Read from cache. * @param string key * @return mixed|NULL */ public function read($key) { $key = $this->prefix . $key; $meta = $this->memcache->get($key); if (!$meta) { return NULL; } // meta structure: // array( // data => stored data // delta => relative (sliding) expiration // callbacks => array of callbacks (function, args) // ) // verify dependencies if (!empty($meta[self::META_CALLBACKS]) && !NCache::checkCallbacks($meta[self::META_CALLBACKS])) { $this->memcache->delete($key, 0); return NULL; } if (!empty($meta[self::META_DELTA])) { $this->memcache->replace($key, $meta, 0, $meta[self::META_DELTA] + time()); } return $meta[self::META_DATA]; }
public function __destruct() { if ($this->cache && $this->structure !== $this->loadedStructure) { $this->cache->save('structure', $this->structure); } }
/** * Starts the output cache. Returns NCachingHelper object if buffering was started. * @param ICacheStorage * @param string * @param NCachingHelper[] * @param array * @return NCachingHelper */ public static function createCache(ICacheStorage $cacheStorage, $key, & $parents, array $args = NULL) { if ($args) { if (array_key_exists('if', $args) && !$args['if']) { return $parents[] = new stdClass; } $key = array_merge(array($key), array_intersect_key($args, range(0, count($args)))); } if ($parents) { end($parents)->dependencies[NCache::ITEMS][] = $key; } $cache = new NCache($cacheStorage, 'Nette.Templating.Cache'); if ($helper = $cache->start($key)) { if (isset($args['expire'])) { $args['expiration'] = $args['expire']; // back compatibility } $helper->dependencies = array( NCache::TAGS => isset($args['tags']) ? $args['tags'] : NULL, NCache::EXPIRATION => isset($args['expiration']) ? $args['expiration'] : '+ 7 days', ); $parents[] = $helper; } return $helper; }
/** * Returns annotations. * @param ReflectionClass|ReflectionMethod|ReflectionProperty * @return array */ public static function getAll(Reflector $r) { if ($r instanceof ReflectionClass) { $type = $r->getName(); $member = ''; } elseif ($r instanceof ReflectionMethod) { $type = $r->getDeclaringClass()->getName(); $member = $r->getName(); } else { $type = $r->getDeclaringClass()->getName(); $member = '$' . $r->getName(); } if (!self::$useReflection) { // auto-expire cache $file = $r instanceof ReflectionClass ? $r->getFileName() : $r->getDeclaringClass()->getFileName(); // will be used later if ($file && isset(self::$timestamps[$file]) && self::$timestamps[$file] !== filemtime($file)) { unset(self::$cache[$type]); } unset(self::$timestamps[$file]); } if (isset(self::$cache[$type][$member])) { // is value cached? return self::$cache[$type][$member]; } if (self::$useReflection === NULL) { // detects whether is reflection available self::$useReflection = (bool) NClassReflection::from(__CLASS__)->getDocComment(); } if (self::$useReflection) { $annotations = self::parseComment($r->getDocComment()); } else { if (!self::$cacheStorage) { // trigger_error('Set a cache storage for annotations parser via NAnnotationParser::setCacheStorage().', E_USER_WARNING); self::$cacheStorage = new NDevNullStorage; } $outerCache = new NCache(self::$cacheStorage, 'Nette.Reflection.Annotations'); if (self::$cache === NULL) { self::$cache = (array) $outerCache->offsetGet('list'); self::$timestamps = isset(self::$cache['*']) ? self::$cache['*'] : array(); } if (!isset(self::$cache[$type]) && $file) { self::$cache['*'][$file] = filemtime($file); self::parseScript($file); $outerCache->save('list', self::$cache); } if (isset(self::$cache[$type][$member])) { $annotations = self::$cache[$type][$member]; } else { $annotations = array(); } } if ($r instanceof ReflectionMethod && !$r->isPrivate() && (!$r->isConstructor() || !empty($annotations['inheritdoc'][0]))) { try { $inherited = self::getAll(new ReflectionMethod(get_parent_class($type), $member)); } catch (ReflectionException $e) { try { $inherited = self::getAll($r->getPrototype()); } catch (ReflectionException $e) { $inherited = array(); } } $annotations += array_intersect_key($inherited, array_flip(self::$inherited)); } return self::$cache[$type][$member] = $annotations; }
/** * Verifies dependencies. * @param array * @return bool */ private function verify($meta) { do { if (!empty($meta[self::META_DELTA])) { // meta[file] was added by readMetaAndLock() if (filemtime($meta[self::FILE]) + $meta[self::META_DELTA] < time()) { break; } touch($meta[self::FILE]); } elseif (!empty($meta[self::META_EXPIRE]) && $meta[self::META_EXPIRE] < time()) { break; } if (!empty($meta[self::META_CALLBACKS]) && !NCache::checkCallbacks($meta[self::META_CALLBACKS])) { break; } if (!empty($meta[self::META_ITEMS])) { foreach ($meta[self::META_ITEMS] as $depFile => $time) { $m = $this->readMetaAndLock($depFile, LOCK_SH); if ($m[self::META_TIME] !== $time || ($m && !$this->verify($m))) { break 2; } } } return TRUE; } while (FALSE); $this->delete($meta[self::FILE], $meta[self::HANDLE]); // meta[handle] & meta[file] was added by readMetaAndLock() return FALSE; }
function removeCache() { return NCache::removeMenu(); }
/** * Returns system DI container. * @return SystemContainer */ public function createContainer() { if ($cacheDir = $this->getCacheDirectory()) { $cache = new NCache(new NPhpFileStorage($cacheDir), 'Nette.Configurator'); $cacheKey = array($this->params, $this->files); $cached = $cache->load($cacheKey); if (!$cached) { $code = $this->buildContainer($dependencies); $cache->save($cacheKey, $code, array( NCache::FILES => $this->params['productionMode'] ? NULL : $dependencies, )); $cached = $cache->load($cacheKey); } NLimitedScope::load($cached['file'], TRUE); } elseif ($this->files) { throw new InvalidStateException("Set path to temporary directory using setTempDirectory()."); } else { NLimitedScope::evaluate($this->buildContainer()); // back compatibility with Environment } $container = new $this->params['container']['class']; $container->initialize(); NEnvironment::setContext($container); // back compatibility return $container; }
function &getTreeAsSelect($name, $label) { include_once 'n_cache.php'; include_once 'n_quickform.php'; if (!($options = NCache::getTreeAsSelect())) { $options = $this->getOptions(false); if ($options) { NCache::createTreeAsSelect($options); } } return NQuickForm::createElement('select', $name, $label, $options); }
public function __destruct() { if ($this->cache) { $this->cache->save('structure', $this->structure); } }
protected function saveCacheState() { if ($this->observeCache === $this && $this->cache && !$this->sqlBuilder->getSelect() && $this->accessedColumns != $this->previousAccessedColumns) { $this->cache->save($this->getCacheKey(), $this->accessedColumns); } }
/** * Renders template to output. * @return void */ public function render() { if ($this->file == NULL) { // intentionally == throw new InvalidStateException("Template file name was not specified."); } $cache = new NCache($storage = $this->getCacheStorage(), 'Nette.FileTemplate'); if ($storage instanceof NPhpFileStorage) { $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 (NTemplateException $e) { $e->setSourceFile($this->file); throw $e; } $cache->save($this->file, $compiled, array( NCache::FILES => $this->file, NCache::CONSTS => 'NFramework::REVISION', )); $cached = $cache->load($this->file); } if ($cached !== NULL && $storage instanceof NPhpFileStorage) { NLimitedScope::load($cached['file'], $this->getParameters()); } else { NLimitedScope::evaluate($compiled, $this->getParameters()); } }