|
|
@ -7,7 +7,11 @@ |
|
|
|
|
|
|
|
|
|
|
|
namespace yii\widgets; |
|
|
|
namespace yii\widgets; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use Yii; |
|
|
|
|
|
|
|
use yii\base\InvalidConfigException; |
|
|
|
use yii\base\Widget; |
|
|
|
use yii\base\Widget; |
|
|
|
|
|
|
|
use yii\caching\Cache; |
|
|
|
|
|
|
|
use yii\caching\Dependency; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @author Qiang Xue <qiang.xue@gmail.com> |
|
|
|
* @author Qiang Xue <qiang.xue@gmail.com> |
|
|
@ -16,81 +20,47 @@ use yii\base\Widget; |
|
|
|
class FragmentCache extends Widget |
|
|
|
class FragmentCache extends Widget |
|
|
|
{ |
|
|
|
{ |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Prefix to the keys for storing cached data |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
const CACHE_KEY_PREFIX = 'Yii.COutputCache.'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @var string the ID of the cache application component. Defaults to 'cache' (the primary cache application component.) |
|
|
|
* @var string the ID of the cache application component. Defaults to 'cache' (the primary cache application component.) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public $cacheID = 'cache'; |
|
|
|
public $cacheID = 'cache'; |
|
|
|
/** |
|
|
|
/** |
|
|
|
* @var integer number of seconds that the data can remain in cache. Defaults to 60 seconds. |
|
|
|
* @var integer number of seconds that the data can remain valid in cache. |
|
|
|
* If it is 0, existing cached content would be removed from the cache. |
|
|
|
* Use 0 to indicate that the cached data will never expire. |
|
|
|
* If it is a negative value, the cache will be disabled (any existing cached content will |
|
|
|
|
|
|
|
* remain in the cache.) |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* Note, if cache dependency changes or cache space is limited, |
|
|
|
|
|
|
|
* the data may be purged out of cache earlier. |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public $duration = 60; |
|
|
|
public $duration = 60; |
|
|
|
/** |
|
|
|
/** |
|
|
|
* @var mixed the dependency that the cached content depends on. |
|
|
|
* @var array|Dependency the dependency that the cached content depends on. |
|
|
|
* This can be either an object implementing {@link ICacheDependency} interface or an array |
|
|
|
* This can be either a [[Dependency]] object or a configuration array for creating the dependency object. |
|
|
|
* specifying the configuration of the dependency object. For example, |
|
|
|
* For example, |
|
|
|
* <pre> |
|
|
|
* |
|
|
|
|
|
|
|
* ~~~ |
|
|
|
* array( |
|
|
|
* array( |
|
|
|
* 'class'=>'CDbCacheDependency', |
|
|
|
* 'class' => 'yii\caching\DbDependency', |
|
|
|
* 'sql' => 'SELECT MAX(lastModified) FROM Post', |
|
|
|
* 'sql' => 'SELECT MAX(lastModified) FROM Post', |
|
|
|
* ) |
|
|
|
* ) |
|
|
|
* </pre> |
|
|
|
* ~~~ |
|
|
|
|
|
|
|
* |
|
|
|
* would make the output cache depends on the last modified time of all posts. |
|
|
|
* would make the output cache depends on the last modified time of all posts. |
|
|
|
* If any post has its modification time changed, the cached content would be invalidated. |
|
|
|
* If any post has its modification time changed, the cached content would be invalidated. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public $dependency; |
|
|
|
public $dependency; |
|
|
|
/** |
|
|
|
/** |
|
|
|
* @var boolean whether the content being cached should be differentiated according to route. |
|
|
|
* @var array list of factors that would cause the variation of the content being cached. |
|
|
|
* A route consists of the requested controller ID and action ID. |
|
|
|
* Each factor is a string representing a variation (e.g. the language, a GET parameter). |
|
|
|
* Defaults to true. |
|
|
|
* The following variation setting will cause the content to be cached in different versions |
|
|
|
*/ |
|
|
|
* according to the current application language: |
|
|
|
public $varyByRoute = true; |
|
|
|
* |
|
|
|
/** |
|
|
|
* ~~~ |
|
|
|
* @var boolean whether the content being cached should be differentiated according to user's language. |
|
|
|
* array( |
|
|
|
* A language is retrieved via Yii::app()->language. |
|
|
|
* Yii::$app->language, |
|
|
|
* Defaults to false. |
|
|
|
* ) |
|
|
|
* @since 1.1.14 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public $varyByLanguage = false; |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @var array list of GET parameters that should participate in cache key calculation. |
|
|
|
|
|
|
|
* By setting this property, the output cache will use different cached data |
|
|
|
|
|
|
|
* for each different set of GET parameter values. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public $varyByParam; |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @var string a PHP expression whose result is used in the cache key calculation. |
|
|
|
|
|
|
|
* By setting this property, the output cache will use different cached data |
|
|
|
|
|
|
|
* for each different expression result. |
|
|
|
|
|
|
|
* The expression can also be a valid PHP callback, |
|
|
|
|
|
|
|
* including class method name (array(ClassName/Object, MethodName)), |
|
|
|
|
|
|
|
* or anonymous function (PHP 5.3.0+). The function/method signature should be as follows: |
|
|
|
|
|
|
|
* <pre> |
|
|
|
|
|
|
|
* function foo($cache) { ... } |
|
|
|
|
|
|
|
* </pre> |
|
|
|
|
|
|
|
* where $cache refers to the output cache component. |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public $varyByExpression; |
|
|
|
public $variations; |
|
|
|
/** |
|
|
|
/** |
|
|
|
* @var array list of request types (e.g. GET, POST) for which the cache should be enabled only. |
|
|
|
* @var boolean whether to enable the fragment cache. You may use this property to turn on and off |
|
|
|
* Defaults to null, meaning all request types. |
|
|
|
* the fragment cache according to specific setting (e.g. enable fragment cache only for GET requests). |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public $requestTypes; |
|
|
|
public $enabled = true; |
|
|
|
|
|
|
|
|
|
|
|
private $_key; |
|
|
|
|
|
|
|
private $_cache; |
|
|
|
|
|
|
|
private $_contentCached; |
|
|
|
|
|
|
|
private $_content; |
|
|
|
|
|
|
|
private $_actions; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Marks the start of content to be cached. |
|
|
|
* Marks the start of content to be cached. |
|
|
@ -100,10 +70,7 @@ class FragmentCache extends Widget |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function init() |
|
|
|
public function init() |
|
|
|
{ |
|
|
|
{ |
|
|
|
if ($this->getIsContentCached()) { |
|
|
|
if ($this->getCachedContent() === false && $this->getCache() !== null) { |
|
|
|
$this->replayActions(); |
|
|
|
|
|
|
|
} elseif ($this->_cache !== null) { |
|
|
|
|
|
|
|
$this->getController()->getCachingStack()->push($this); |
|
|
|
|
|
|
|
ob_start(); |
|
|
|
ob_start(); |
|
|
|
ob_implicit_flush(false); |
|
|
|
ob_implicit_flush(false); |
|
|
|
} |
|
|
|
} |
|
|
@ -117,178 +84,89 @@ class FragmentCache extends Widget |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function run() |
|
|
|
public function run() |
|
|
|
{ |
|
|
|
{ |
|
|
|
if ($this->getIsContentCached()) { |
|
|
|
if (($content = $this->getCachedContent()) !== false) { |
|
|
|
if ($this->getController()->isCachingStackEmpty()) { |
|
|
|
echo $content; |
|
|
|
echo $this->getController()->processDynamicOutput($this->_content); |
|
|
|
} elseif (($cache = $this->getCache()) !== false) { |
|
|
|
} else { |
|
|
|
$content = ob_get_clean(); |
|
|
|
echo $this->_content; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} elseif ($this->_cache !== null) { |
|
|
|
|
|
|
|
$this->_content = ob_get_clean(); |
|
|
|
|
|
|
|
$this->getController()->getCachingStack()->pop(); |
|
|
|
|
|
|
|
$data = array($this->_content, $this->_actions); |
|
|
|
|
|
|
|
if (is_array($this->dependency)) { |
|
|
|
if (is_array($this->dependency)) { |
|
|
|
$this->dependency = Yii::createComponent($this->dependency); |
|
|
|
$this->dependency = Yii::createObject($this->dependency); |
|
|
|
} |
|
|
|
|
|
|
|
$this->_cache->set($this->getCacheKey(), $data, $this->duration, $this->dependency); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($this->getController()->isCachingStackEmpty()) { |
|
|
|
|
|
|
|
echo $this->getController()->processDynamicOutput($this->_content); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
echo $this->_content; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
$cache->set($this->calculateKey(), $content, $this->duration, $this->dependency); |
|
|
|
|
|
|
|
echo $content; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @return boolean whether the content can be found from cache |
|
|
|
* @var string|boolean the cached content. False if the content is not cached. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function getIsContentCached() |
|
|
|
private $_content; |
|
|
|
{ |
|
|
|
|
|
|
|
if ($this->_contentCached !== null) { |
|
|
|
|
|
|
|
return $this->_contentCached; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
return $this->_contentCached = $this->checkContentCache(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Looks for content in cache. |
|
|
|
* Returns the cached content if available. |
|
|
|
* @return boolean whether the content is found in cache. |
|
|
|
* @return string|boolean the cached content. False is returned if valid content is not found in the cache. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected function checkContentCache() |
|
|
|
public function getCachedContent() |
|
|
|
{ |
|
|
|
{ |
|
|
|
if ((empty($this->requestTypes) || in_array(Yii::app()->getRequest()->getRequestType(), $this->requestTypes)) |
|
|
|
if ($this->_content === null) { |
|
|
|
&& ($this->_cache = $this->getCache()) !== null |
|
|
|
if (($cache = $this->getCache()) !== null) { |
|
|
|
) { |
|
|
|
$key = $this->calculateKey(); |
|
|
|
if ($this->duration > 0 && ($data = $this->_cache->get($this->getCacheKey())) !== false) { |
|
|
|
$this->_content = $cache->get($key); |
|
|
|
$this->_content = $data[0]; |
|
|
|
} else { |
|
|
|
$this->_actions = $data[1]; |
|
|
|
$this->_content = false; |
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if ($this->duration == 0) { |
|
|
|
|
|
|
|
$this->_cache->delete($this->getCacheKey()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if ($this->duration <= 0) { |
|
|
|
|
|
|
|
$this->_cache = null; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return false; |
|
|
|
return $this->_content; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @return ICache the cache used for caching the content. |
|
|
|
* Generates a unique key used for storing the content in cache. |
|
|
|
|
|
|
|
* The key generated depends on both [[id]] and [[variations]]. |
|
|
|
|
|
|
|
* @return string a valid cache key |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected function getCache() |
|
|
|
protected function calculateKey() |
|
|
|
{ |
|
|
|
{ |
|
|
|
return Yii::app()->getComponent($this->cacheID); |
|
|
|
$factors = array(__CLASS__, $this->getId()); |
|
|
|
|
|
|
|
if (is_array($this->variations)) { |
|
|
|
|
|
|
|
foreach ($this->variations as $factor) { |
|
|
|
|
|
|
|
$factors[] = $factor; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return $this->getCache()->buildKey($factors); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Caclulates the base cache key. |
|
|
|
* @var Cache |
|
|
|
* The calculated key will be further variated in {@link getCacheKey}. |
|
|
|
|
|
|
|
* Derived classes may override this method if more variations are needed. |
|
|
|
|
|
|
|
* @return string basic cache key without variations |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected function getBaseCacheKey() |
|
|
|
private $_cache; |
|
|
|
{ |
|
|
|
|
|
|
|
return self::CACHE_KEY_PREFIX . $this->getId() . '.'; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Calculates the cache key. |
|
|
|
* Returns the cache instance used for storing content. |
|
|
|
* The key is calculated based on {@link getBaseCacheKey} and other factors, including |
|
|
|
* @return Cache the cache instance. Null is returned if the cache component is not available |
|
|
|
* {@link varyByRoute}, {@link varyByParam}, {@link varyBySession} and {@link varyByLanguage}. |
|
|
|
* or [[enabled]] is false. |
|
|
|
* @return string cache key |
|
|
|
* @throws InvalidConfigException if [[cacheID]] does not point to a valid application component. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected function getCacheKey() |
|
|
|
public function getCache() |
|
|
|
{ |
|
|
|
{ |
|
|
|
if ($this->_key !== null) { |
|
|
|
if (!$this->enabled) { |
|
|
|
return $this->_key; |
|
|
|
return null; |
|
|
|
} else { |
|
|
|
|
|
|
|
$key = $this->getBaseCacheKey() . '.'; |
|
|
|
|
|
|
|
if ($this->varyByRoute) { |
|
|
|
|
|
|
|
$controller = $this->getController(); |
|
|
|
|
|
|
|
$key .= $controller->getUniqueId() . '/'; |
|
|
|
|
|
|
|
if (($action = $controller->getAction()) !== null) { |
|
|
|
|
|
|
|
$key .= $action->getId(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
$key .= '.'; |
|
|
|
if ($this->_cache === null) { |
|
|
|
|
|
|
|
$cache = Yii::$app->getComponent($this->cacheID); |
|
|
|
if ($this->varyBySession) { |
|
|
|
if ($cache instanceof Cache) { |
|
|
|
$key .= Yii::app()->getSession()->getSessionID(); |
|
|
|
$this->_cache = $cache; |
|
|
|
} |
|
|
|
|
|
|
|
$key .= '.'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (is_array($this->varyByParam) && isset($this->varyByParam[0])) { |
|
|
|
|
|
|
|
$params = array(); |
|
|
|
|
|
|
|
foreach ($this->varyByParam as $name) { |
|
|
|
|
|
|
|
if (isset($_GET[$name])) { |
|
|
|
|
|
|
|
$params[$name] = $_GET[$name]; |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
$params[$name] = ''; |
|
|
|
throw new InvalidConfigException('FragmentCache::cacheID must refer to the ID of a cache application component.'); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
$key .= serialize($params); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
$key .= '.'; |
|
|
|
return $this->_cache; |
|
|
|
|
|
|
|
|
|
|
|
if ($this->varyByExpression !== null) { |
|
|
|
|
|
|
|
$key .= $this->evaluateExpression($this->varyByExpression); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
$key .= '.'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($this->varyByLanguage) { |
|
|
|
|
|
|
|
$key .= Yii::app()->language; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
$key .= '.'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return $this->_key = $key; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Records a method call when this output cache is in effect. |
|
|
|
|
|
|
|
* When the content is served from the output cache, the recorded |
|
|
|
|
|
|
|
* method will be re-invoked. |
|
|
|
|
|
|
|
* @param string $context a property name of the controller. The property should refer to an object |
|
|
|
|
|
|
|
* whose method is being recorded. If empty it means the controller itself. |
|
|
|
|
|
|
|
* @param string $method the method name |
|
|
|
|
|
|
|
* @param array $params parameters passed to the method |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function recordAction($context, $method, $params) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$this->_actions[] = array($context, $method, $params); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Replays the recorded method calls. |
|
|
|
* Sets the cache instance used by the session component. |
|
|
|
|
|
|
|
* @param Cache $value the cache instance |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected function replayActions() |
|
|
|
public function setCache($value) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (empty($this->_actions)) { |
|
|
|
$this->_cache = $value; |
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
$controller = $this->getController(); |
|
|
|
|
|
|
|
$cs = Yii::app()->getClientScript(); |
|
|
|
|
|
|
|
foreach ($this->_actions as $action) { |
|
|
|
|
|
|
|
if ($action[0] === 'clientScript') { |
|
|
|
|
|
|
|
$object = $cs; |
|
|
|
|
|
|
|
} elseif ($action[0] === '') { |
|
|
|
|
|
|
|
$object = $controller; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
$object = $controller->{$action[0]}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (method_exists($object, $action[1])) { |
|
|
|
|
|
|
|
call_user_func_array(array($object, $action[1]), $action[2]); |
|
|
|
|
|
|
|
} elseif ($action[0] === '' && function_exists($action[1])) { |
|
|
|
|
|
|
|
call_user_func_array($action[1], $action[2]); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
throw new CException(Yii::t('yii', 'Unable to replay the action "{object}.{method}". The method does not exist.', |
|
|
|
|
|
|
|
array('object' => $action[0], |
|
|
|
|
|
|
|
'method' => $action[1]))); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |