CacheItem   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 12
eloc 24
c 1
b 0
f 1
dl 0
loc 96
ccs 27
cts 27
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A dependency() 0 3 1
A expiry() 0 3 1
A update() 0 5 1
A __construct() 0 7 2
A key() 0 3 1
A expired() 0 26 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Cache\Metadata;
6
7
use Yiisoft\Cache\CacheInterface;
8
use Yiisoft\Cache\Dependency\Dependency;
9
use Yiisoft\Cache\Exception\InvalidArgumentException;
10
11
use function ceil;
12
use function log;
13
use function microtime;
14
use function random_int;
15
use function sprintf;
16
use function time;
17
18
use const PHP_INT_MAX;
19
20
/**
21
 * CacheItem store the metadata of cache item.
22
 *
23
 * @internal
24
 */
25
final class CacheItem
26
{
27
    private ?int $expiry;
28
    private float $updated;
29
30
    /**
31
     * @param string $key The key that identifies the cache item.
32
     * @param int|null $ttl The TTL value of this item. null means infinity.
33
     * @param Dependency|null $dependency The cache invalidation dependency or null for none.
34
     */
35
    public function __construct(
36
        private string $key,
37 57
        ?int $ttl,
38
        private ?Dependency $dependency
39 57
    ) {
40 57
        $this->expiry = ($ttl > 0) ? time() + $ttl : $ttl;
41 57
        $this->updated = microtime(true);
42 57
    }
43
44
    /**
45
     * Updates the metadata of the cache item.
46
     *
47
     * @param int|null $expiry The cache expiry. null means infinity.
48
     * @param Dependency|null $dependency The cache invalidation dependency or null for none.
49
     */
50
    public function update(?int $expiry, ?Dependency $dependency): void
51 16
    {
52
        $this->expiry = $expiry;
53 16
        $this->dependency = $dependency;
54 16
        $this->updated = microtime(true);
55 16
    }
56
57
    /**
58
     * Returns a key that identifies the cache item.
59
     *
60
     * @return string The key that identifies the cache item.
61
     */
62
    public function key(): string
63 46
    {
64
        return $this->key;
65 46
    }
66
67
    /**
68
     * Returns a cache expiry or null that means infinity.
69
     *
70
     * @return int|null The cache expiry timestamp or null that means infinity.
71
     */
72
    public function expiry(): ?int
73 20
    {
74
        return $this->expiry;
75 20
    }
76
77
    /**
78
     * Returns a cache dependency or null if there is none.
79
     *
80
     * @return Dependency|null The cache dependency or null if there is none.
81
     */
82
    public function dependency(): ?Dependency
83 21
    {
84
        return $this->dependency;
85 21
    }
86
87
    /**
88
     * Checks whether the dependency has been changed or whether the cache expired.
89
     *
90
     * @param float $beta The value for calculating the range that is used for "Probably early expiration" algorithm.
91
     * @param CacheInterface $cache The actual cache handler.
92
     *
93
     * @return bool Whether the dependency has been changed or whether the cache expired.
94
     */
95
    public function expired(float $beta, CacheInterface $cache): bool
96 35
    {
97
        if ($beta < 0) {
98 35
            throw new InvalidArgumentException(sprintf(
99 1
                'Argument "$beta" must be a positive number, %f given.',
100
                $beta
101
            ));
102
        }
103
104
        if ($this->dependency !== null && $this->dependency->isChanged($cache)) {
105 34
            return true;
106 6
        }
107
108
        if ($this->expiry === null) {
109 33
            return false;
110 16
        }
111
112
        if ($this->expiry <= time()) {
113 18
            return true;
114 6
        }
115
116
        $now = microtime(true);
117 15
        $delta = ceil(1000 * ($now - $this->updated)) / 1000;
118 15
        $expired = $now - $delta * $beta * log(random_int(1, PHP_INT_MAX) / PHP_INT_MAX);
119 15
120
        return $this->expiry <= $expired;
121 15
    }
122
}
123