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