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
|
|
|
/** @var mixed */ |
29
|
|
|
private $value; |
30
|
|
|
private ?int $expiry; |
31
|
|
|
private ?Dependency $dependency; |
32
|
|
|
private float $updated; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $key |
36
|
|
|
* @param mixed $value |
37
|
|
|
* @param int|null $expiry |
38
|
|
|
* @param Dependency|null $dependency |
39
|
|
|
*/ |
40
|
53 |
|
public function __construct(string $key, $value, ?int $expiry, ?Dependency $dependency) |
41
|
|
|
{ |
42
|
53 |
|
$this->key = $key; |
43
|
53 |
|
$this->value = $value; |
44
|
53 |
|
$this->expiry = $expiry; |
45
|
53 |
|
$this->dependency = $dependency; |
46
|
53 |
|
$this->updated = microtime(true); |
47
|
53 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param mixed $value |
51
|
|
|
* @param int|null $expiry |
52
|
|
|
* @param Dependency|null $dependency |
53
|
|
|
*/ |
54
|
3 |
|
public function update($value, ?int $expiry, ?Dependency $dependency): void |
55
|
|
|
{ |
56
|
3 |
|
$this->value = $value; |
57
|
3 |
|
$this->expiry = $expiry; |
58
|
3 |
|
$this->dependency = $dependency; |
59
|
3 |
|
$this->updated = microtime(true); |
60
|
3 |
|
} |
61
|
|
|
|
62
|
42 |
|
public function key(): string |
63
|
|
|
{ |
64
|
42 |
|
return $this->key; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return mixed |
69
|
|
|
*/ |
70
|
9 |
|
public function value() |
71
|
|
|
{ |
72
|
9 |
|
return $this->value; |
73
|
|
|
} |
74
|
|
|
|
75
|
13 |
|
public function expiry(): ?int |
76
|
|
|
{ |
77
|
13 |
|
return $this->expiry; |
78
|
|
|
} |
79
|
|
|
|
80
|
5 |
|
public function dependency(): ?Dependency |
81
|
|
|
{ |
82
|
5 |
|
return $this->dependency; |
83
|
|
|
} |
84
|
|
|
|
85
|
18 |
|
public function expired(float $beta, CacheInterface $cache): bool |
86
|
|
|
{ |
87
|
18 |
|
if ($beta < 0) { |
88
|
1 |
|
throw new InvalidArgumentException(sprintf( |
89
|
1 |
|
'Argument "$beta" must be a positive number, %f given.', |
90
|
1 |
|
$beta |
91
|
|
|
)); |
92
|
|
|
} |
93
|
|
|
|
94
|
17 |
|
if ($this->dependency !== null && $this->dependency->isChanged($cache)) { |
95
|
3 |
|
return true; |
96
|
|
|
} |
97
|
|
|
|
98
|
17 |
|
if ($this->expiry === null) { |
99
|
7 |
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
11 |
|
if ($this->expiry <= time()) { |
103
|
4 |
|
return true; |
104
|
|
|
} |
105
|
|
|
|
106
|
9 |
|
$now = microtime(true); |
107
|
9 |
|
$delta = ceil(1000 * ($now - $this->updated)) / 1000; |
108
|
9 |
|
$expired = $now - $delta * $beta * log(random_int(1, PHP_INT_MAX) / PHP_INT_MAX); |
109
|
|
|
|
110
|
9 |
|
return $this->expiry <= $expired; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|