Class COutputCache
COutputCache enables caching the output generated by an action or a view fragment.
If the output to be displayed is found valid in cache, the cached version will be displayed instead, which saves the time for generating the original output.
Since COutputCache extends from CFilterWidget
, it can be used as
either a filter (for action caching) or a widget (for fragment caching). For the
latter, the shortcuts CBaseController::beginCache()
and CBaseController::endCache()
are often used instead, like the following in a
view file:
if($this->beginCache('cacheName',array('property1'=>'value1',...)) { // ... display the content to be cached here $this->endCache(); }
COutputCache must work with a cache application component specified via
COutputCache::$cacheID
. If the cache application component is not available,
COutputCache will be disabled.
The validity of the cached content is determined based on two factors: the
COutputCache::$duration
and the cache COutputCache::$dependency
. The former specifies the
number of seconds that the data can remain valid in cache (defaults to 60s),
while the latter specifies conditions that the cached data depends on. If a
dependency changes, (e.g. relevant data in DB are updated), the cached data will
be invalidated. For more details about cache dependency, see CCacheDependency
.
Sometimes, it is necessary to turn off output caching only for certain
request types. For example, we only want to cache a form when it is initially
requested; any subsequent display of the form should not be cached because it
contains user input. We can set COutputCache::$requestTypes
to be <span
class="php-keyword1">array</span>(<span
class="php-quote">'GET'</span>)
to accomplish this task.
COutputCache::$varyByRoute
: this specifies whether the cached content should be varied with the requested route (controller and action)COutputCache::$varyByParam
: this specifies a list of GET parameter names and uses the corresponding values to determine the version of the cached content.COutputCache::$varyBySession
: this specifies whether the cached content should be varied with the user session.COutputCache::$varyByExpression
: this specifies whether the cached content should be varied with the result of the specified PHP expression.COutputCache::$varyByLanguage
: this specifies whether the cached content should by varied with the user's language. Available since 1.1.14.
COutputCache::getBaseCacheKey()
method.
- CComponent
- CBaseController
- CWidget
- CFilterWidget implements IFilter
- COutputCache
Copyright: 2008-2013 Yii Software LLC
License: http://www.yiiframework.com/license/
Author: Qiang Xue <qiang.xue@gmail.com>
Since: 1.0
Located at x2engine/framework/web/widgets/COutputCache.php
public
boolean
|
#
filter(
Performs filtering before the action is executed. This method is meant to be overridden by child classes if begin-filtering is needed. |
public
|
#
init( )
Marks the start of content to be cached. Content displayed after this method
call and before |
public
|
#
run( )
Marks the end of content to be cached. Content displayed before this method
call and after |
public
boolean
|
|
protected
boolean
|
|
protected
|
|
protected
string
|
#
getBaseCacheKey( )
Caclulates the base cache key. The calculated key will be further variated in
|
protected
string
|
#
getCacheKey( )
Calculates the cache key. The key is calculated based on |
public
|
#
recordAction( string $context, string $method, array $params )
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. |
protected
|
__construct(),
getIsFilter()
|
actions(),
getController(),
getId(),
getOwner(),
getViewFile(),
getViewPath(),
render(),
setId()
|
beginCache(),
beginClip(),
beginContent(),
beginWidget(),
createWidget(),
endCache(),
endClip(),
endContent(),
endWidget(),
renderFile(),
renderInternal(),
widget()
|
string |
CACHE_KEY_PREFIX
|
'Yii.COutputCache.' |
#
Prefix to the keys for storing cached data |
public
integer
|
$duration | 60 |
#
number of seconds that the data can remain in cache. Defaults to 60 seconds. If it is 0, existing cached content would be removed from the cache. 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
boolean
|
$varyByRoute | true |
#
whether the content being cached should be differentiated according to route. A route consists of the requested controller ID and action ID. Defaults to true. |
public
boolean
|
$varyBySession | false |
#
whether the content being cached should be differentiated according to user sessions. Defaults to false. |
public
array
|
$varyByParam |
|
#
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
string
|
$varyByExpression |
|
#
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: function foo($cache) { ... } where $cache refers to the output cache component. The PHP expression will be evaluated using A PHP expression can be any PHP code that has a value. To learn more about what an expression is, please refer to the php manual. |
public
boolean
|
$varyByLanguage | false |
#
whether the content being cached should be differentiated according to user's language. A language is retrieved via Yii::app()->language. Defaults to false. |
public
array
|
$requestTypes |
|
#
list of request types (e.g. GET, POST) for which the cache should be enabled only. Defaults to null, meaning all request types. |
public
string
|
$cacheID | 'cache' |
#
the ID of the cache application component. Defaults to 'cache' (the primary cache application component.) |
public
mixed
|
$dependency |
|
#
the dependency that the cached content depends on. This can be either an
object implementing array( 'class'=>'CDbCacheDependency', 'sql'=>'SELECT MAX(lastModified) FROM Post', ) 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. |
$stopAction
|
$actionPrefix,
$skin
|
public
boolean
|
$isContentCached |
#
Whether the content can be found from cache. |
$isFilter
|
$controller,
$id,
$owner,
$viewPath
|