0 follower

Final Class Yiisoft\Cache\Cache

InheritanceYiisoft\Cache\Cache
ImplementsYiisoft\Cache\CacheInterface

Cache provides support for the data caching, including cache key composition and dependencies, and uses "Probably early expiration" for cache stampede prevention. The actual data caching is performed via PSR-16 {@see \Psr\SimpleCache\CacheInterface} instance passed to constructor.

You can use PSR-16 methods via {@see \Yiisoft\Cache\Cache::psr()}.

See also Yiisoft\Cache\CacheInterface.

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( \Psr\SimpleCache\CacheInterface $handler, DateInterval|integer|Yiisoft\Cache\Ttl|null $defaultTtl null )
$handler \Psr\SimpleCache\CacheInterface

The actual cache handler.

$defaultTtl DateInterval|integer|Yiisoft\Cache\Ttl|null

The default TTL for a cache entry. null meaning infinity, negative or zero results in the cache key deletion. This value is used by {@see \Yiisoft\Cache\getOrSet()}, if the duration is not explicitly given.

                public function __construct(\Psr\SimpleCache\CacheInterface $handler, Ttl|DateInterval|int|null $defaultTtl = null)
{
    $this->psr = new DependencyAwareCache($this, $handler);
    $this->items = new CacheItems();
    $this->defaultTtl = Ttl::from($defaultTtl);
}

            
getOrSet() public method

public mixed getOrSet ( mixed $key, callable $callable, DateInterval|integer|null $ttl null, Yiisoft\Cache\Dependency\Dependency|null $dependency null, float $beta 1.0 )
$key mixed
$callable callable
$ttl DateInterval|integer|null
$dependency Yiisoft\Cache\Dependency\Dependency|null
$beta float

                public function getOrSet(
    mixed $key,
    callable $callable,
    DateInterval|int|null $ttl = null,
    Dependency|null $dependency = null,
    float $beta = 1.0
) {
    $ttlObj = Ttl::from($ttl ?? $this->defaultTtl);
    $key = CacheKeyNormalizer::normalize($key);
    $value = $this->getValue($key, $beta);
    return $value ?? $this->setAndGet($key, $callable, $ttlObj, $dependency);
}

            
psr() public method

public \Psr\SimpleCache\CacheInterface psr ( )

                public function psr(): \Psr\SimpleCache\CacheInterface
{
    return $this->psr;
}

            
remove() public method

public void remove ( mixed $key )
$key mixed

                public function remove(mixed $key): void
{
    $key = CacheKeyNormalizer::normalize($key);
    if (!$this->psr->delete($key)) {
        throw new RemoveCacheException($key);
    }
    $this->items->remove($key);
}