FragmentCache
API reference › yii › widgets › FragmentCache
FragmentCache is used by View to provide caching of page fragments.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$autoIdPrefix static | string | The prefix to the automatically generated widget IDs. Default: 'w'. | yii\ |
$behaviors read-only | array<yii\<static>> | List of behaviors attached to this component. | yii\ |
$cache | yii\| | The cache object or the application component ID of the cache object. Default: 'cache'. | FragmentCache |
$cachedContent read-only | string| | The cached content. | FragmentCache |
$dependency | array|yii\ | The dependency that the cached content depends on. Default: null. | FragmentCache |
$duration | int | Number of seconds that the data can remain valid in cache. Default: 60. | FragmentCache |
$dynamicPlaceholders | array | A list of placeholders. | yii\ |
$enabled | bool | Whether to enable the fragment cache. Default: true. | FragmentCache |
$id | string | Id of the widget. | yii\ |
$variations | string[]| | List of factors that would cause the variation of the content being cached. Default: null. | FragmentCache |
$view | yii\ | The view object that can be used to render views or view files. | yii\ |
$viewPath read-only | string | The directory containing the view files for this widget. | yii\ |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__clone() | This method is called after the object is created by cloning an existing one. | yii\ |
__get() | Returns the value of a component property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets the value of a component property. | yii\ |
__unset() | Sets a component property to be null. | yii\ |
addDynamicPlaceholder() | Adds a placeholder for dynamic content. | yii\ |
afterRun() | This method is invoked right after a widget is executed. | yii\ |
attachBehavior() | Attaches a behavior to this component. | yii\ |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\ |
beforeRun() | This method is invoked right before the widget is executed. | yii\ |
begin() static | Begins a widget. | yii\ |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
detachBehavior() | Detaches a behavior from the component. | yii\ |
detachBehaviors() | Detaches all behaviors from the component. | yii\ |
end() static | Ends a widget. | yii\ |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\ |
getBehavior() | Returns the named behavior object. | yii\ |
getBehaviors() | Returns all behaviors attached to this component. | yii\ |
getCachedContent() | Returns the cached content if available. | FragmentCache |
getDynamicPlaceholders() | Returns a list of placeholders for dynamic content. This method is used internally to implement the content caching feature. | yii\ |
getId() | Returns the ID of the widget. | yii\ |
getView() | Returns the view object that can be used to render views or view files. | yii\ |
getViewPath() | Returns the directory containing the view files for this widget. | yii\ |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\ |
init() | Initializes the FragmentCache object. | FragmentCache |
off() | Detaches an existing event handler from this component. | yii\ |
on() | Attaches an event handler to an event. | yii\ |
render() | Renders a view. | yii\ |
renderFile() | Renders a view file. | yii\ |
run() | Marks the end of content to be cached. | FragmentCache |
setDynamicPlaceholders() | Sets a list of placeholders for dynamic content. This method is used internally to implement the content caching feature. | yii\ |
setId() | Sets the ID of the widget. | yii\ |
setView() | Sets the view object to be used by this widget. | yii\ |
trigger() | Triggers an event. | yii\ |
widget() static | Creates a widget instance and runs it. | yii\ |
calculateKey() protected | Generates a unique key used for storing the content in cache. | FragmentCache |
updateDynamicContent() protected | Replaces placeholders in $content with results of evaluated dynamic statements. | yii\ |
Events
Section titled “Events”| Event | Type | Description | Defined by |
|---|---|---|---|
EVENT_INIT | yii\ | An event that is triggered when the widget is initialized via init(). Event name: 'init'. Available since 2.0.11. | yii\ |
EVENT_BEFORE_RUN | yii\ | An event raised right before executing a widget. Event name: 'beforeRun'. Available since 2.0.11. | yii\ |
EVENT_AFTER_RUN | yii\ | An event raised right after executing a widget. Event name: 'afterRun'. Available since 2.0.11. | yii\ |
Property details
Section titled “Property details”$cache
Section titled “$cache ”public yii\caching\CacheInterface|array|string $cache = 'cache'The cache object or the application component ID of the cache object.
After the FragmentCache object is created, if you want to change this property, you should only assign it with a cache object. Starting from version 2.0.2, this can also be a configuration array for creating the object.
$cachedContent
Section titled “$cachedContent ”public string|false $cachedContentThe cached content.
False is returned if valid content is not found in the cache.
Access
Read-only (virtual property).
$dependency
Section titled “$dependency ”public array|yii\caching\Dependency $dependency = nullThe dependency that the cached content depends on.
This can be either a Dependency object or a configuration array for creating the dependency object.
For example,
[ 'class' => 'yii\caching\DbDependency', 'sql' => 'SELECT MAX(updated_at) 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.
$duration
Section titled “$duration ”public int $duration = 60Number of seconds that the data can remain valid in cache.
Use 0 to indicate that the cached data will never expire.
$enabled
Section titled “$enabled ”public bool $enabled = trueWhether to enable the fragment cache.
You may use this property to turn on and off the fragment cache according to specific setting (e.g. enable fragment cache only for GET requests).
$variations
Section titled “$variations ”public string[]|string $variations = nullList of factors that would cause the variation of the content being cached.
Each factor is a string representing a variation (e.g. the language, a GET parameter). The following variation setting will cause the content to be cached in different versions according to the current application language:
[ Yii::$app->language,]Method details
Section titled “Method details”getCachedContent()
Section titled “getCachedContent() ”public function getCachedContent(): string|falseReturns the cached content if available.
Returns
string|false: The cached content. False is returned if valid content is not found in the cache.
init()
Section titled “init() ”public function init(): mixedInitializes the FragmentCache object.
Returns
mixed
run()
Section titled “run() ”public function run(): mixedMarks the end of content to be cached.
Content displayed before this method call and after init()
will be captured and saved in cache.
This method does nothing if valid content is already found in cache.
Returns
mixed
calculateKey()
Section titled “calculateKey() ”protected function calculateKey(): mixedGenerates a unique key used for storing the content in cache.
The key generated depends on both $id and $variations.
Returns
mixed: A valid cache key