0 follower

Final Class Yiisoft\Cache\Metadata\CacheItem

InheritanceYiisoft\Cache\Metadata\CacheItem

CacheItem store the metadata of cache item.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Cache\Metadata\CacheItem
dependency() Returns a cache dependency or null if there is none. Yiisoft\Cache\Metadata\CacheItem
expired() Checks whether the dependency has been changed or whether the cache expired. Yiisoft\Cache\Metadata\CacheItem
expiry() Returns a cache expiry or null that means infinity. Yiisoft\Cache\Metadata\CacheItem
key() Returns a key that identifies the cache item. Yiisoft\Cache\Metadata\CacheItem
update() Updates the metadata of the cache item. Yiisoft\Cache\Metadata\CacheItem

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( string $key, integer|Yiisoft\Cache\Ttl|null $ttl, Yiisoft\Cache\Dependency\Dependency|null $dependency )
$key string

The key that identifies the cache item.

$ttl integer|Yiisoft\Cache\Ttl|null

The TTL value of this item. null means infinity.

$dependency Yiisoft\Cache\Dependency\Dependency|null

The cache invalidation dependency or null for none.

                public function __construct(
    private readonly string $key,
    Ttl|int|null $ttl,
    private ?Dependency $dependency
) {
    $ttl = Ttl::from($ttl)->toSeconds();
    $this->expiry = ($ttl > 0) ? time() + $ttl : $ttl;
    $this->updated = microtime(true);
}

            
dependency() public method

Returns a cache dependency or null if there is none.

public Yiisoft\Cache\Dependency\Dependency|null dependency ( )
return Yiisoft\Cache\Dependency\Dependency|null

The cache dependency or null if there is none.

                public function dependency(): ?Dependency
{
    return $this->dependency;
}

            
expired() public method

Checks whether the dependency has been changed or whether the cache expired.

public boolean expired ( float $beta, Yiisoft\Cache\CacheInterface $cache )
$beta float

The value for calculating the range that is used for "Probably early expiration" algorithm.

$cache Yiisoft\Cache\CacheInterface

The actual cache handler.

return boolean

Whether the dependency has been changed or whether the cache expired.

                public function expired(float $beta, CacheInterface $cache): bool
{
    if ($beta < 0) {
        throw new InvalidArgumentException(sprintf(
            'Argument "$beta" must be a positive number, %f given.',
            $beta
        ));
    }
    if ($this->dependency !== null && $this->dependency->isChanged($cache)) {
        return true;
    }
    if ($this->expiry === null) {
        return false;
    }
    if ($this->expiry <= time()) {
        return true;
    }
    $now = microtime(true);
    $delta = ceil(1000 * ($now - $this->updated)) / 1000;
    $expired = $now - $delta * $beta * log(random_int(1, PHP_INT_MAX) / PHP_INT_MAX);
    return $this->expiry <= $expired;
}

            
expiry() public method

Returns a cache expiry or null that means infinity.

public integer|null expiry ( )
return integer|null

The cache expiry timestamp or null that means infinity.

                public function expiry(): ?int
{
    return $this->expiry;
}

            
key() public method

Returns a key that identifies the cache item.

public string key ( )
return string

The key that identifies the cache item.

                public function key(): string
{
    return $this->key;
}

            
update() public method

Updates the metadata of the cache item.

public void update ( integer|null $expiry, Yiisoft\Cache\Dependency\Dependency|null $dependency )
$expiry integer|null

The cache expiry. null means infinity.

$dependency Yiisoft\Cache\Dependency\Dependency|null

The cache invalidation dependency or null for none.

                public function update(?int $expiry, ?Dependency $dependency): void
{
    $this->expiry = $expiry;
    $this->dependency = $dependency;
    $this->updated = microtime(true);
}