mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 14:26:54 +08:00
FragmentCache WIP
This commit is contained in:
@ -11,8 +11,18 @@ namespace yii\base;
|
|||||||
* @author Qiang Xue <qiang.xue@gmail.com>
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||||||
* @since 2.0
|
* @since 2.0
|
||||||
*/
|
*/
|
||||||
class Filter extends Behavior
|
class ActionFilter extends Behavior
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var array list of action IDs that this filter should apply to. If this property is not set,
|
||||||
|
* then the filter applies to all actions, unless they are listed in [[except]].
|
||||||
|
*/
|
||||||
|
public $only;
|
||||||
|
/**
|
||||||
|
* @var array list of action IDs that this filter should not apply to.
|
||||||
|
*/
|
||||||
|
public $except = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Declares event handlers for the [[owner]]'s events.
|
* Declares event handlers for the [[owner]]'s events.
|
||||||
* @return array events (array keys) and the corresponding event handler methods (array values).
|
* @return array events (array keys) and the corresponding event handler methods (array values).
|
||||||
|
@ -337,15 +337,16 @@ class View extends Component
|
|||||||
* ~~~
|
* ~~~
|
||||||
*
|
*
|
||||||
* @param string $id a unique ID identifying the fragment to be cached.
|
* @param string $id a unique ID identifying the fragment to be cached.
|
||||||
* @param array $properties initial property values for [[\yii\widgets\OutputCache]]
|
* @param array $properties initial property values for [[\yii\widgets\FragmentCache]]
|
||||||
* @return boolean whether you should generate the content for caching.
|
* @return boolean whether you should generate the content for caching.
|
||||||
* False if the cached version is available.
|
* False if the cached version is available.
|
||||||
*/
|
*/
|
||||||
public function beginCache($id, $properties = array())
|
public function beginCache($id, $properties = array())
|
||||||
{
|
{
|
||||||
$properties['id'] = $id;
|
$properties['id'] = $id;
|
||||||
|
/** @var $cache \yii\widgets\FragmentCache */
|
||||||
$cache = $this->beginWidget('yii\widgets\OutputCache', $properties);
|
$cache = $this->beginWidget('yii\widgets\OutputCache', $properties);
|
||||||
if ($cache->getIsContentCached()) {
|
if ($cache->getCachedContent() !== false) {
|
||||||
$this->endCache();
|
$this->endCache();
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
namespace yii\caching;
|
namespace yii\caching;
|
||||||
|
|
||||||
use yii\base\Component;
|
use yii\base\Component;
|
||||||
|
use yii\helpers\StringHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cache is the base class for cache classes supporting different cache storage implementation.
|
* Cache is the base class for cache classes supporting different cache storage implementation.
|
||||||
@ -70,13 +71,13 @@ abstract class Cache extends Component implements \ArrayAccess
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds a normalized cache key from one or multiple parameters.
|
* Builds a normalized cache key from a given key.
|
||||||
*
|
*
|
||||||
* The generated key contains letters and digits only, and its length is no more than 32.
|
* The generated key contains letters and digits only, and its length is no more than 32.
|
||||||
*
|
*
|
||||||
* If only one parameter is given and it is already a normalized key, then
|
* If the given key is a string containing alphanumeric characters only and no more than 32 characters,
|
||||||
* it will be returned back without change. Otherwise, a normalized key
|
* then the key will be returned back without change. Otherwise, a normalized key
|
||||||
* is generated by serializing all given parameters and applying MD5 hashing.
|
* is generated by serializing the given key and applying MD5 hashing.
|
||||||
*
|
*
|
||||||
* The following example builds a cache key using three parameters:
|
* The following example builds a cache key using three parameters:
|
||||||
*
|
*
|
||||||
@ -84,16 +85,15 @@ abstract class Cache extends Component implements \ArrayAccess
|
|||||||
* $key = $cache->buildKey($className, $method, $id);
|
* $key = $cache->buildKey($className, $method, $id);
|
||||||
* ~~~
|
* ~~~
|
||||||
*
|
*
|
||||||
* @param string $key the first parameter
|
* @param array|string $key the key to be normalized
|
||||||
* @return string the generated cache key
|
* @return string the generated cache key
|
||||||
*/
|
*/
|
||||||
public function buildKey($key)
|
public function buildKey($key)
|
||||||
{
|
{
|
||||||
if (func_num_args() === 1 && ctype_alnum($key) && strlen($key) <= 32) {
|
if (is_string($key)) {
|
||||||
return (string)$key;
|
return ctype_alnum($key) && StringHelper::strlen($key) <= 32 ? $key : md5($key);
|
||||||
} else {
|
} else {
|
||||||
$params = func_get_args();
|
return md5(json_encode($key));
|
||||||
return md5(json_encode($params));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,15 +7,15 @@
|
|||||||
|
|
||||||
namespace yii\caching;
|
namespace yii\caching;
|
||||||
|
|
||||||
|
use Yii;
|
||||||
use yii\base\InvalidConfigException;
|
use yii\base\InvalidConfigException;
|
||||||
use yii\db\Connection;
|
use yii\db\Connection;
|
||||||
use yii\db\Query;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DbDependency represents a dependency based on the query result of a SQL statement.
|
* DbDependency represents a dependency based on the query result of a SQL statement.
|
||||||
*
|
*
|
||||||
* If the query result changes, the dependency is considered as changed.
|
* If the query result changes, the dependency is considered as changed.
|
||||||
* The query is specified via the [[query]] property.
|
* The query is specified via the [[sql]] property.
|
||||||
*
|
*
|
||||||
* @author Qiang Xue <qiang.xue@gmail.com>
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||||||
* @since 2.0
|
* @since 2.0
|
||||||
@ -27,23 +27,25 @@ class DbDependency extends Dependency
|
|||||||
*/
|
*/
|
||||||
public $connectionID = 'db';
|
public $connectionID = 'db';
|
||||||
/**
|
/**
|
||||||
* @var Query the SQL query whose result is used to determine if the dependency has been changed.
|
* @var string the SQL query whose result is used to determine if the dependency has been changed.
|
||||||
* Only the first row of the query result will be used.
|
* Only the first row of the query result will be used.
|
||||||
*/
|
*/
|
||||||
public $query;
|
public $sql;
|
||||||
/**
|
/**
|
||||||
* @var Connection the DB connection instance
|
* @var array the parameters (name=>value) to be bound to the SQL statement specified by [[sql]].
|
||||||
*/
|
*/
|
||||||
private $_db;
|
public $params;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
* @param Query $query the SQL query whose result is used to determine if the dependency has been changed.
|
* @param string $sql the SQL query whose result is used to determine if the dependency has been changed.
|
||||||
|
* @param array $params the parameters (name=>value) to be bound to the SQL statement specified by [[sql]].
|
||||||
* @param array $config name-value pairs that will be used to initialize the object properties
|
* @param array $config name-value pairs that will be used to initialize the object properties
|
||||||
*/
|
*/
|
||||||
public function __construct($query = null, $config = array())
|
public function __construct($sql, $params = array(), $config = array())
|
||||||
{
|
{
|
||||||
$this->query = $query;
|
$this->sql = $sql;
|
||||||
|
$this->params = $params;
|
||||||
parent::__construct($config);
|
parent::__construct($config);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,21 +68,22 @@ class DbDependency extends Dependency
|
|||||||
protected function generateDependencyData()
|
protected function generateDependencyData()
|
||||||
{
|
{
|
||||||
$db = $this->getDb();
|
$db = $this->getDb();
|
||||||
/**
|
|
||||||
* @var \yii\db\Command $command
|
|
||||||
*/
|
|
||||||
$command = $this->query->createCommand($db);
|
|
||||||
if ($db->enableQueryCache) {
|
if ($db->enableQueryCache) {
|
||||||
// temporarily disable and re-enable query caching
|
// temporarily disable and re-enable query caching
|
||||||
$db->enableQueryCache = false;
|
$db->enableQueryCache = false;
|
||||||
$result = $command->queryRow();
|
$result = $db->createCommand($this->sql, $this->params)->queryRow();
|
||||||
$db->enableQueryCache = true;
|
$db->enableQueryCache = true;
|
||||||
} else {
|
} else {
|
||||||
$result = $command->queryRow();
|
$result = $db->createCommand($this->sql, $this->params)->queryRow();
|
||||||
}
|
}
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Connection the DB connection instance
|
||||||
|
*/
|
||||||
|
private $_db;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the DB connection instance used for caching purpose.
|
* Returns the DB connection instance used for caching purpose.
|
||||||
* @return Connection the DB connection instance
|
* @return Connection the DB connection instance
|
||||||
@ -89,11 +92,11 @@ class DbDependency extends Dependency
|
|||||||
public function getDb()
|
public function getDb()
|
||||||
{
|
{
|
||||||
if ($this->_db === null) {
|
if ($this->_db === null) {
|
||||||
$db = \Yii::$app->getComponent($this->connectionID);
|
$db = Yii::$app->getComponent($this->connectionID);
|
||||||
if ($db instanceof Connection) {
|
if ($db instanceof Connection) {
|
||||||
$this->_db = $db;
|
$this->_db = $db;
|
||||||
} else {
|
} else {
|
||||||
throw new InvalidConfigException("DbCache::connectionID must refer to the ID of a DB application component.");
|
throw new InvalidConfigException("DbCacheDependency::connectionID must refer to the ID of a DB application component.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $this->_db;
|
return $this->_db;
|
||||||
|
@ -389,7 +389,13 @@ class Command extends \yii\base\Component
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isset($cache)) {
|
if (isset($cache)) {
|
||||||
$cacheKey = $cache->buildKey(__CLASS__, $db->dsn, $db->username, $sql, $paramLog);
|
$cacheKey = $cache->buildKey(array(
|
||||||
|
__CLASS__,
|
||||||
|
$db->dsn,
|
||||||
|
$db->username,
|
||||||
|
$sql,
|
||||||
|
$paramLog,
|
||||||
|
));
|
||||||
if (($result = $cache->get($cacheKey)) !== false) {
|
if (($result = $cache->get($cacheKey)) !== false) {
|
||||||
\Yii::trace('Query result found in cache', __CLASS__);
|
\Yii::trace('Query result found in cache', __CLASS__);
|
||||||
return $result;
|
return $result;
|
||||||
|
@ -109,7 +109,12 @@ abstract class Schema extends \yii\base\Object
|
|||||||
*/
|
*/
|
||||||
public function getCacheKey($cache, $name)
|
public function getCacheKey($cache, $name)
|
||||||
{
|
{
|
||||||
return $cache->buildKey(__CLASS__, $this->db->dsn, $this->db->username, $name);
|
return $cache->buildKey(array(
|
||||||
|
__CLASS__,
|
||||||
|
$this->db->dsn,
|
||||||
|
$this->db->username,
|
||||||
|
$name,
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -7,15 +7,7 @@
|
|||||||
|
|
||||||
namespace yii\helpers;
|
namespace yii\helpers;
|
||||||
|
|
||||||
// todo define how subclassing will work
|
|
||||||
// todo add a run() or render() method
|
|
||||||
// todo test this on all kinds of terminals, especially windows (check out lib ncurses)
|
// todo test this on all kinds of terminals, especially windows (check out lib ncurses)
|
||||||
// todo not sure if all methods should be static
|
|
||||||
|
|
||||||
// todo subclass DetailView
|
|
||||||
// todo subclass GridView
|
|
||||||
// todo more subclasses
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Console View is the base class for console view components
|
* Console View is the base class for console view components
|
||||||
|
@ -45,6 +45,7 @@ class CacheSession extends Session
|
|||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the cache instance used for storing session data.
|
* Returns the cache instance used for storing session data.
|
||||||
* @return Cache the cache instance
|
* @return Cache the cache instance
|
||||||
@ -114,6 +115,6 @@ class CacheSession extends Session
|
|||||||
*/
|
*/
|
||||||
protected function calculateKey($id)
|
protected function calculateKey($id)
|
||||||
{
|
{
|
||||||
return $this->getCache()->buildKey(__CLASS__, $id);
|
return $this->getCache()->buildKey(array(__CLASS__, $id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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>
|
||||||
@ -15,82 +19,48 @@ 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:
|
||||||
|
*
|
||||||
|
* ~~~
|
||||||
|
* array(
|
||||||
|
* Yii::$app->language,
|
||||||
|
* )
|
||||||
*/
|
*/
|
||||||
public $varyByRoute = true;
|
public $variations;
|
||||||
/**
|
/**
|
||||||
* @var boolean whether the content being cached should be differentiated according to user's language.
|
* @var boolean whether to enable the fragment cache. You may use this property to turn on and off
|
||||||
* A language is retrieved via Yii::app()->language.
|
* the fragment cache according to specific setting (e.g. enable fragment cache only for GET requests).
|
||||||
* Defaults to false.
|
|
||||||
* @since 1.1.14
|
|
||||||
*/
|
*/
|
||||||
public $varyByLanguage = false;
|
public $enabled = true;
|
||||||
/**
|
|
||||||
* @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;
|
|
||||||
/**
|
|
||||||
* @var array list of request types (e.g. GET, POST) for which the cache should be enabled only.
|
|
||||||
* Defaults to null, meaning all request types.
|
|
||||||
*/
|
|
||||||
public $requestTypes;
|
|
||||||
|
|
||||||
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);
|
$cache->set($this->calculateKey(), $content, $this->duration, $this->dependency);
|
||||||
|
echo $content;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ($this->getController()->isCachingStackEmpty()) {
|
/**
|
||||||
echo $this->getController()->processDynamicOutput($this->_content);
|
* @var string|boolean the cached content. False if the content is not cached.
|
||||||
|
*/
|
||||||
|
private $_content;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the cached content if available.
|
||||||
|
* @return string|boolean the cached content. False is returned if valid content is not found in the cache.
|
||||||
|
*/
|
||||||
|
public function getCachedContent()
|
||||||
|
{
|
||||||
|
if ($this->_content === null) {
|
||||||
|
if (($cache = $this->getCache()) !== null) {
|
||||||
|
$key = $this->calculateKey();
|
||||||
|
$this->_content = $cache->get($key);
|
||||||
} else {
|
} else {
|
||||||
echo $this->_content;
|
$this->_content = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return $this->_content;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return boolean whether the content can be found from cache
|
* 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
|
||||||
*/
|
*/
|
||||||
public function getIsContentCached()
|
protected function calculateKey()
|
||||||
{
|
{
|
||||||
if ($this->_contentCached !== null) {
|
$factors = array(__CLASS__, $this->getId());
|
||||||
return $this->_contentCached;
|
if (is_array($this->variations)) {
|
||||||
} else {
|
foreach ($this->variations as $factor) {
|
||||||
return $this->_contentCached = $this->checkContentCache();
|
$factors[] = $factor;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Looks for content in cache.
|
|
||||||
* @return boolean whether the content is found in cache.
|
|
||||||
*/
|
|
||||||
protected function checkContentCache()
|
|
||||||
{
|
|
||||||
if ((empty($this->requestTypes) || in_array(Yii::app()->getRequest()->getRequestType(), $this->requestTypes))
|
|
||||||
&& ($this->_cache = $this->getCache()) !== null
|
|
||||||
) {
|
|
||||||
if ($this->duration > 0 && ($data = $this->_cache->get($this->getCacheKey())) !== false) {
|
|
||||||
$this->_content = $data[0];
|
|
||||||
$this->_actions = $data[1];
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if ($this->duration == 0) {
|
|
||||||
$this->_cache->delete($this->getCacheKey());
|
|
||||||
}
|
|
||||||
if ($this->duration <= 0) {
|
|
||||||
$this->_cache = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return $this->getCache()->buildKey($factors);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return ICache the cache used for caching the content.
|
* @var Cache
|
||||||
*/
|
*/
|
||||||
protected function getCache()
|
private $_cache;
|
||||||
{
|
|
||||||
return Yii::app()->getComponent($this->cacheID);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Caclulates the base cache key.
|
* Returns the cache instance used for storing content.
|
||||||
* The calculated key will be further variated in {@link getCacheKey}.
|
* @return Cache the cache instance. Null is returned if the cache component is not available
|
||||||
* Derived classes may override this method if more variations are needed.
|
* or [[enabled]] is false.
|
||||||
* @return string basic cache key without variations
|
* @throws InvalidConfigException if [[cacheID]] does not point to a valid application component.
|
||||||
*/
|
*/
|
||||||
protected function getBaseCacheKey()
|
public function getCache()
|
||||||
{
|
{
|
||||||
return self::CACHE_KEY_PREFIX . $this->getId() . '.';
|
if (!$this->enabled) {
|
||||||
}
|
return null;
|
||||||
|
|
||||||
/**
|
|
||||||
* Calculates the cache key.
|
|
||||||
* The key is calculated based on {@link getBaseCacheKey} and other factors, including
|
|
||||||
* {@link varyByRoute}, {@link varyByParam}, {@link varyBySession} and {@link varyByLanguage}.
|
|
||||||
* @return string cache key
|
|
||||||
*/
|
|
||||||
protected function getCacheKey()
|
|
||||||
{
|
|
||||||
if ($this->_key !== null) {
|
|
||||||
return $this->_key;
|
|
||||||
} else {
|
|
||||||
$key = $this->getBaseCacheKey() . '.';
|
|
||||||
if ($this->varyByRoute) {
|
|
||||||
$controller = $this->getController();
|
|
||||||
$key .= $controller->getUniqueId() . '/';
|
|
||||||
if (($action = $controller->getAction()) !== null) {
|
|
||||||
$key .= $action->getId();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$key .= '.';
|
|
||||||
|
|
||||||
if ($this->varyBySession) {
|
|
||||||
$key .= Yii::app()->getSession()->getSessionID();
|
|
||||||
}
|
|
||||||
$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 {
|
|
||||||
$params[$name] = '';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$key .= serialize($params);
|
|
||||||
}
|
|
||||||
$key .= '.';
|
|
||||||
|
|
||||||
if ($this->varyByExpression !== null) {
|
|
||||||
$key .= $this->evaluateExpression($this->varyByExpression);
|
|
||||||
}
|
|
||||||
$key .= '.';
|
|
||||||
|
|
||||||
if ($this->varyByLanguage) {
|
|
||||||
$key .= Yii::app()->language;
|
|
||||||
}
|
|
||||||
$key .= '.';
|
|
||||||
|
|
||||||
return $this->_key = $key;
|
|
||||||
}
|
}
|
||||||
}
|
if ($this->_cache === null) {
|
||||||
|
$cache = Yii::$app->getComponent($this->cacheID);
|
||||||
/**
|
if ($cache instanceof Cache) {
|
||||||
* Records a method call when this output cache is in effect.
|
$this->_cache = $cache;
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
protected function replayActions()
|
|
||||||
{
|
|
||||||
if (empty($this->_actions)) {
|
|
||||||
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 {
|
} else {
|
||||||
$object = $controller->{$action[0]};
|
throw new InvalidConfigException('FragmentCache::cacheID must refer to the ID of a cache application component.');
|
||||||
}
|
|
||||||
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])));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return $this->_cache;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the cache instance used by the session component.
|
||||||
|
* @param Cache $value the cache instance
|
||||||
|
*/
|
||||||
|
public function setCache($value)
|
||||||
|
{
|
||||||
|
$this->_cache = $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user