0 follower

Final Class Yiisoft\Db\Cache\SchemaCache

InheritanceYiisoft\Db\Cache\SchemaCache

Implements a cache for the database schema information.

The 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 Yiisoft\Db\Schema\AbstractSchema needs to retrieve information about the database schema, it first checks the cache using 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 Yiisoft\Db\Cache\SchemaCache.

Yiisoft\Db\Schema\AbstractSchema uses this implementation to cache table metadata.

Public Methods

Hide inherited 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

Hide inherited methods

__construct() public method

public __construct( \Psr\SimpleCache\CacheInterface $psrCache ): mixed
$psrCache \Psr\SimpleCache\CacheInterface

PSR-16 cache implementation to use.

                public function __construct(
    private readonly CacheInterface $psrCache,
) {}

            
get() public method

Retrieve value from cache.

public get( mixed $key ): mixed
$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);
}

            
getDuration() public method

public getDuration( ): DateInterval|integer|null
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;
}

            
invalidate() public method

Invalidates all the cached values associated with any of the specified tags.

public invalidate( string $cacheTag ): void
$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);
    }
}

            
isEnabled() public method

Return true if SchemaCache is active.

public isEnabled( ): boolean

                public function isEnabled(): bool
{
    return $this->enabled;
}

            
isExcluded() public method

public isExcluded( string $value ): boolean
$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() public method

Remove a value with the specified key from cache.

public remove( mixed $key ): void
$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);
}

            
set() public method

Persists data in the cache, uniquely referenced by a key with an optional tag.

public set( mixed $key, mixed $value, string|null $tag null ): void
$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.');
}

            
setDuration() public method

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 setDuration( DateInterval|integer|null $value ): void
$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;
}

            
setEnabled() public method

Whether to enable schema caching.

See also:

public setEnabled( boolean $value ): void
$value boolean

Whether to enable schema caching.

                public function setEnabled(bool $value): void
{
    $this->enabled = $value;
}

            
setExclude() public method

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 setExclude( array $value ): void
$value array

The table names.

                public function setExclude(array $value): void
{
    $this->exclude = $value;
}