Final Class Yiisoft\Db\Cache\SchemaCache
| Inheritance | Yiisoft\Db\Cache\SchemaCache |
|---|
Implements a cache for the database schema information.
The {@see \Yiisoft\Db\Schema\AbstractSchema} retrieves information about the database schema from the database server and stores it in the cache for faster access. When the {@see \Yiisoft\Db\Schema\AbstractSchema} needs to retrieve information about the database schema, it first checks the cache using {@see \Yiisoft\Db\Cache\SchemaCache}. If the information is not in the cache, the Schema retrieves it from the database server and stores it in the cache using the {@see \Yiisoft\Db\Cache\SchemaCache}.
{@see \Yiisoft\Db\Schema\AbstractSchema} uses this implementation to cache table metadata.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Db\Cache\SchemaCache | |
| get() | Retrieve value from cache. | Yiisoft\Db\Cache\SchemaCache |
| getDuration() | Yiisoft\Db\Cache\SchemaCache | |
| invalidate() | Invalidates all the cached values associated with any of the specified tags. | Yiisoft\Db\Cache\SchemaCache |
| isEnabled() | Return true if SchemaCache is active. | Yiisoft\Db\Cache\SchemaCache |
| isExcluded() | Yiisoft\Db\Cache\SchemaCache | |
| remove() | Remove a value with the specified key from cache. | Yiisoft\Db\Cache\SchemaCache |
| set() | Persists data in the cache, uniquely referenced by a key with an optional tag. | Yiisoft\Db\Cache\SchemaCache |
| setDuration() | Number of seconds that table metadata can remain valid in cache. Use 'null' to indicate that the cached data will never expire. | Yiisoft\Db\Cache\SchemaCache |
| setEnabled() | Whether to enable schema caching. | Yiisoft\Db\Cache\SchemaCache |
| setExclude() | List of tables not to cache metadata for. | Yiisoft\Db\Cache\SchemaCache |
Method Details
| public mixed __construct ( \Psr\SimpleCache\CacheInterface $psrCache ) | ||
| $psrCache | \Psr\SimpleCache\CacheInterface |
PSR-16 cache implementation to use. |
public function __construct(private CacheInterface $psrCache) {}
Retrieve value from cache.
| public mixed get ( mixed $key ) | ||
| $key | mixed |
The key identifying the value to cache. |
| return | mixed |
Cache value. |
|---|---|---|
| throws | \Psr\SimpleCache\InvalidArgumentException | |
public function get(mixed $key): mixed
{
$stringKey = $this->normalize($key);
return $this->psrCache->get($stringKey);
}
| public DateInterval|integer|null getDuration ( ) | ||
| return | DateInterval|integer|null |
The number of seconds that table metadata can remain valid in cache. |
|---|---|---|
public function getDuration(): int|DateInterval|null
{
return $this->duration;
}
Invalidates all the cached values associated with any of the specified tags.
| public void invalidate ( string $cacheTag ) | ||
| $cacheTag | string |
The cache tag used to identify the values to invalidate. |
| throws | \Psr\SimpleCache\InvalidArgumentException | |
|---|---|---|
public function invalidate(string $cacheTag): void
{
if (empty($cacheTag)) {
return;
}
/** @var string[] $data */
$data = $this->psrCache->get($cacheTag, []);
foreach ($data as $key) {
$this->psrCache->delete($key);
}
}
Return true if SchemaCache is active.
| public boolean isEnabled ( ) |
public function isEnabled(): bool
{
return $this->enabled;
}
| public boolean isExcluded ( string $value ) | ||
| $value | string |
The table name. |
| return | boolean |
Whether to exclude the table from caching. |
|---|---|---|
public function isExcluded(string $value): bool
{
return in_array($value, $this->exclude, true);
}
Remove a value with the specified key from cache.
| public void remove ( mixed $key ) | ||
| $key | mixed |
A key identifying the value to delete from cache. |
| throws | \Psr\SimpleCache\InvalidArgumentException | |
|---|---|---|
public function remove(mixed $key): void
{
$stringKey = $this->normalize($key);
$this->psrCache->delete($stringKey);
}
Persists data in the cache, uniquely referenced by a key with an optional tag.
| public void set ( mixed $key, mixed $value, string|null $tag = null ) | ||
| $key | mixed |
The key of the item to store. |
| $value | mixed |
The value of the item to store. |
| $tag | string|null |
Cache tag. |
| throws | \Psr\SimpleCache\InvalidArgumentException |
If the $key string isn't a legal value. |
|---|---|---|
| throws | RuntimeException |
If cache value isn't set. |
public function set(mixed $key, mixed $value, ?string $tag = null): void
{
$stringKey = $this->normalize($key);
if ($this->psrCache->set($stringKey, $value, $this->duration)) {
$this->addToTag($stringKey, $tag);
return;
}
throw new RuntimeException('Cache value not set.');
}
Number of seconds that table metadata can remain valid in cache. Use 'null' to indicate that the cached data will never expire.
See also setEnabled().
| public void setDuration ( DateInterval|integer|null $value ) | ||
| $value | DateInterval|integer|null |
The number of seconds that table metadata can remain valid in cache. |
public function setDuration(int|DateInterval|null $value): void
{
$this->duration = $value;
}
| public void setEnabled ( boolean $value ) | ||
| $value | boolean |
Whether to enable schema caching. |
public function setEnabled(bool $value): void
{
$this->enabled = $value;
}
List of tables not to cache metadata for.
Defaults to an empty array. The table names may contain schema prefix, if any. Don't quote the table names.
See also setEnabled().
| public void setExclude ( array $value ) | ||
| $value | array |
The table names. |
public function setExclude(array $value): void
{
$this->exclude = $value;
}
Signup or Login in order to comment.