0 follower

Interface Yiisoft\Cache\CacheInterface

Implemented byYiisoft\Cache\Cache

CacheInterface is a wrapper over PSR-16 {@see \Psr\SimpleCache\CacheInterface} that allows you to implement the cache stampede prevention using the "Probably early expiration" algorithm.

Public Methods

Hide inherited methods

Method Description Defined By
getOrSet() The method combines retrieving and setting the value identified by the $key. Yiisoft\Cache\CacheInterface
psr() Returns the actual handler of the cache. Yiisoft\Cache\CacheInterface
remove() Removes a value with the specified key from cache. Yiisoft\Cache\CacheInterface

Method Details

Hide inherited methods

getOrSet() public abstract method

The method combines retrieving and setting the value identified by the $key.

It will save the result of $callable execution if there is no cache available for the $key. This method allows you to implement "Probably early expiration".

Usage example:

public function getTopProducts(int $count = 10) {
    $key = ['top-products', $count];
    return $this->cache->getOrSet($key, function (\Psr\SimpleCache\CacheInterface $cache) use ($count) {
        return $this->getTopNProductsFromDatabase($count);
    }, 1000);
}
public abstract mixed getOrSet ( mixed $key, callable $callable, DateInterval|integer|null $ttl null, Yiisoft\Cache\Dependency\Dependency|null $dependency null, float $beta 1.0 )
$key mixed

The key identifying the value to be cached.

$callable callable

The callable or closure that will be used to generate a value to be cached.

$ttl DateInterval|integer|null

The TTL of this value. If not set, default value is used.

$dependency Yiisoft\Cache\Dependency\Dependency|null

The dependency of the cache value. If the dependency changes, the corresponding value in the cache will be invalidated when it is fetched.

$beta float

The value for calculating the range that is used for "Probably early expiration". The larger the value, the larger the range. The default value is 1.0, which is sufficient in most cases.

return mixed

Result of $callable execution.

throws Yiisoft\Cache\Exception\InvalidArgumentException

Must be thrown if the $key or $ttl is not a legal value.

throws Yiisoft\Cache\Exception\SetCacheException

Must be thrown if the data could not be set in the cache.

                public function getOrSet(
    mixed $key,
    callable $callable,
    DateInterval|int|null $ttl = null,
    Dependency|null $dependency = null,
    float $beta = 1.0
);

            
psr() public abstract method

Returns the actual handler of the cache.

public abstract \Psr\SimpleCache\CacheInterface psr ( )
return \Psr\SimpleCache\CacheInterface

The actual cache handler.

                public function psr(): \Psr\SimpleCache\CacheInterface;

            
remove() public abstract method

Removes a value with the specified key from cache.

public abstract void remove ( mixed $key )
$key mixed

The key identifying the value to be removed from cache.

throws Yiisoft\Cache\Exception\InvalidArgumentException

MUST be thrown if the $key is not a legal value.

throws Yiisoft\Cache\Exception\RemoveCacheException

Must be thrown if the data could not be removed from the cache.

                public function remove(mixed $key): void;