Final Class Yiisoft\Yii\Widgets\FragmentCache
| Inheritance | Yiisoft\ |
|---|
FragmentCache caches a fragment of content.
See also:
- \
Yiisoft\ View\ Cache\ CachedContent - \
Yiisoft\ Example of use:View\ Cache\ DynamicContent `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\ |
|
| begin() | Starts recording a fragment cache. | Yiisoft\ |
| dependency() | Returns a new instance with the specified dependency. | Yiisoft\ |
| dynamicContents() | Returns a new instance with the specified dynamic contents. | Yiisoft\ |
| id() | Returns a new instance with the specified Widget ID. | Yiisoft\ |
| render() | Marks the end of content to be cached. | Yiisoft\ |
| ttl() | Returns a new instance with the specified TTL. | Yiisoft\ |
| variations() | Returns a new instance with the specified variations. | Yiisoft\ |
Method Details
| public mixed __construct ( \ | ||
| $cache | \ |
|
public function __construct(private readonly CacheInterface $cache) {}
Starts recording a fragment cache.
| public ?string begin ( ) |
public function begin(): ?string
{
parent::begin();
ob_start();
return null;
}
Returns a new instance with the specified dependency.
| public self dependency ( \ | ||
| $value | \ |
The dependency that the cached content depends on. This can be either a {@see \ 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 self dynamicContents ( \ | ||
| $value | \ |
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 self id ( string $value ) | ||
| $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 {@see \
This method does nothing if valid content is already found in cache.
| public string render ( ) | ||
| 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 self ttl ( integer $value ) | ||
| $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 self variations ( string $value ) | ||
| $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;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.