Browse Source

fragment cache WIP

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
27f5c49bd5
  1. 19
      framework/base/View.php
  2. 33
      framework/widgets/FragmentCache.php

19
framework/base/View.php

@ -156,6 +156,25 @@ class View extends Component
return ob_get_clean(); return ob_get_clean();
} }
public function renderDynamic($statements)
{
if (!empty($this->cachingStack)) {
$n = count($this->_dynamicOutput);
$placeholder = "<![CDATA[YDP-$n]]>";
foreach ($this->cachingStack as $cache) {
$cache->dynamicPlaceholders[$placeholder] = $statements;
}
return $placeholder;
} else {
return $this->evaluateDynamicContent($statements);
}
}
public function evaluateDynamicContent($statements)
{
return eval($statements);
}
/** /**
* Finds the view file based on the given view name. * Finds the view file based on the given view name.
* *

33
framework/widgets/FragmentCache.php

@ -61,6 +61,16 @@ class FragmentCache extends Widget
* the fragment cache according to specific setting (e.g. enable fragment cache only for GET requests). * the fragment cache according to specific setting (e.g. enable fragment cache only for GET requests).
*/ */
public $enabled = true; public $enabled = true;
/**
* @var \yii\base\View the view object within which this widget is sued. If not set,
* the view registered with the application will be used. This is mainly used by dynamic content feature.
*/
public $view;
/**
* @var array
*/
public $dynamicPlaceholders;
/** /**
* Marks the start of content to be cached. * Marks the start of content to be cached.
@ -70,7 +80,11 @@ class FragmentCache extends Widget
*/ */
public function init() public function init()
{ {
if ($this->view === null) {
$this->view = Yii::$app->getView();
}
if ($this->getCachedContent() === false && $this->getCache() !== null) { if ($this->getCachedContent() === false && $this->getCache() !== null) {
array_push($this->view->cachingStack, $this);
ob_start(); ob_start();
ob_implicit_flush(false); ob_implicit_flush(false);
} }
@ -88,10 +102,12 @@ class FragmentCache extends Widget
echo $content; echo $content;
} elseif (($cache = $this->getCache()) !== false) { } elseif (($cache = $this->getCache()) !== false) {
$content = ob_get_clean(); $content = ob_get_clean();
array_pop($this->view->cachingStack);
if (is_array($this->dependency)) { if (is_array($this->dependency)) {
$this->dependency = Yii::createObject($this->dependency); $this->dependency = Yii::createObject($this->dependency);
} }
$cache->set($this->calculateKey(), $content, $this->duration, $this->dependency); $data = array($content, $this->dynamicPlaceholders);
$cache->set($this->calculateKey(), $data, $this->duration, $this->dependency);
echo $content; echo $content;
} }
} }
@ -108,11 +124,20 @@ class FragmentCache extends Widget
public function getCachedContent() public function getCachedContent()
{ {
if ($this->_content === null) { if ($this->_content === null) {
$this->_content = false;
if (($cache = $this->getCache()) !== null) { if (($cache = $this->getCache()) !== null) {
$key = $this->calculateKey(); $key = $this->calculateKey();
$this->_content = $cache->get($key); $data = $cache->get($key);
} else { if (is_array($data) && count($data) === 2) {
$this->_content = false; list ($content, $placeholders) = $data;
if (is_array($placeholders) && count($placeholders) > 0) {
foreach ($placeholders as $name => $statements) {
$placeholders[$name] = $this->view->evaluateDynamicContent($statements);
}
$content = strtr($content, $placeholders);
}
$this->_content = $content;
}
} }
} }
return $this->_content; return $this->_content;

Loading…
Cancel
Save