Final Class Yiisoft\Yii\Widgets\FragmentCache
| Inheritance | Yiisoft\Yii\Widgets\FragmentCache » Yiisoft\Widget\Widget |
|---|
FragmentCache caches a fragment of content.
See also:
- \Yiisoft\View\Cache\CachedContent
- \Yiisoft\View\Cache\DynamicContent Example of use:
`php $dynamicContent = new Yiisoft\View\Cache\DynamicContent('dynamic-id', static function (array $parameters): string { return strtoupper("{$parameters['a']} - {$parameters['b']}"); }, ['a' => 'string-a', 'b' => 'string-b']); FragmentCache::widget() ->id('cache-id') ->ttl(30) ->dynamicContents($dynamicContent) ->begin(); echo 'Content to be cached ...'; echo $dynamicContent->placeholder(); echo 'Content to be cached ...'; FragmentCache::end();`
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Yii\Widgets\FragmentCache | |
| begin() | Starts recording a fragment cache. | Yiisoft\Yii\Widgets\FragmentCache |
| dependency() | Returns a new instance with the specified dependency. | Yiisoft\Yii\Widgets\FragmentCache |
| dynamicContents() | Returns a new instance with the specified dynamic contents. | Yiisoft\Yii\Widgets\FragmentCache |
| id() | Returns a new instance with the specified Widget ID. | Yiisoft\Yii\Widgets\FragmentCache |
| render() | Marks the end of content to be cached. | Yiisoft\Yii\Widgets\FragmentCache |
| ttl() | Returns a new instance with the specified TTL. | Yiisoft\Yii\Widgets\FragmentCache |
| variations() | Returns a new instance with the specified variations. | Yiisoft\Yii\Widgets\FragmentCache |
Method Details
| public __construct( \Yiisoft\Cache\CacheInterface $cache ): mixed | ||
| $cache | \Yiisoft\Cache\CacheInterface | |
public function __construct(private CacheInterface $cache) {}
Starts recording a fragment cache.
| public begin( ): string|null |
public function begin(): ?string
{
parent::begin();
ob_start();
return null;
}
Returns a new instance with the specified dependency.
| public dependency( \Yiisoft\Cache\Dependency\Dependency $value ): self | ||
| $value | \Yiisoft\Cache\Dependency\Dependency |
The dependency that the cached content depends on. This can be either a \Yiisoft\Cache\Dependency\Dependency object or a configuration array for creating the dependency object. 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. |
public function dependency(Dependency $value): self
{
$new = clone $this;
$new->dependency = $value;
return $new;
}
Returns a new instance with the specified dynamic contents.
| public dynamicContents( \Yiisoft\View\Cache\DynamicContent $value ): self | ||
| $value | \Yiisoft\View\Cache\DynamicContent |
The dynamic content instances. |
public function dynamicContents(DynamicContent ...$value): self
{
$new = clone $this;
foreach ($value as $dynamicContent) {
$new->dynamicContents[$dynamicContent->id()] = $dynamicContent;
}
return $new;
}
Returns a new instance with the specified Widget ID.
| public id( string $value ): self | ||
| $value | string |
The unique identifier of the cache fragment. |
public function id(string $value): self
{
$new = clone $this;
$new->id = $value;
return $new;
}
Marks the end of content to be cached.
Content displayed before this method call and after begin() will be captured and saved in cache.
This method does nothing if valid content is already found in cache.
| public render( ): string | ||
| return | string |
The result of widget execution to be outputted. |
|---|---|---|
public function render(): string
{
if ($this->id === '') {
ob_end_clean();
throw new RuntimeException('You must assign the "id" using the "id()" setter.');
}
$cachedContent = new CachedContent($this->id, $this->cache, $this->dynamicContents, $this->variations);
$content = $cachedContent->get();
if ($content !== null) {
ob_end_clean();
return $content;
}
$content = ob_get_clean();
if ($content === false || $content === '') {
return '';
}
return $cachedContent->cache($content, $this->ttl, $this->dependency);
}
Returns a new instance with the specified TTL.
| public ttl( integer $value ): self | ||
| $value | integer |
The number of seconds that the data can remain valid in cache. |
public function ttl(int $value): self
{
$new = clone $this;
$new->ttl = $value;
return $new;
}
Returns a new instance with the specified variations.
| public variations( string $value ): self | ||
| $value | string |
The 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:
|
public function variations(string ...$value): self
{
$new = clone $this;
$new->variations = $value;
return $new;
}
Signup or Login in order to comment.