public function render($context) { try { $expire_time = $this->expire_time_var->resolve($context); } catch (VariableDoesNotExist $e) { throw new TemplateSyntaxError('"cache" tag got an unknown variable: ' . $this->expire_time_var->var); } if (!is_numeric($expire_time)) { throw new TemplateSyntaxError('"cache" tag got a non-integer timeout value: ' . print_r($expire_time, true)); } $expire_time = (int) $expire_time; // Build a unicode key for this fragment and all vary-on's. $vs_ = array(); foreach ($this->vary_on as $var) { $v_ = new Variable($var); $vs_[] = urlencode($v_->resolve($context)); } $args = join(':', $vs_); unset($vs_); $cache_key = 'template.cache.' . $this->fragment_name . '.' . md5($args); $manager = Dja::getCacheManager(); $value = $manager->get($cache_key); if ($value === null) { $value = $this->nodelist->render($context); $manager->set($cache_key, $value, $expire_time); } return $value; }
/** * Returns a compiled Template object for the given template name, * handling template inheritance recursively. * * @param $template_name * * @return Template */ public static function getTemplate($template_name) { $self = get_called_class(); $get_template = function () use($template_name, $self) { list($template, $origin) = $self::findTemplate($template_name); if (!py_hasattr($template, 'render')) { // template needs to be compiled $template = $self::getTemplateFromString($template, $origin, $template_name); } return $template; }; $use_cache = Dja::getSetting('TEMPLATE_CACHE'); if (!$use_cache) { return $get_template(); } $cacher = Dja::getCacheManager(); if (!($template = $cacher->get($template_name))) { $template = $get_template(); $cacher->set($template_name, $template); } return $template; }