|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Koded package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Mihail Binev <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* Please view the LICENSE distributed with this source code |
|
9
|
|
|
* for the full copyright and license information. |
|
10
|
|
|
* |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Koded\Caching\Client; |
|
14
|
|
|
|
|
15
|
|
|
use Koded\Caching\Cache; |
|
16
|
|
|
use function Koded\Caching\verify_key; |
|
17
|
|
|
use function Koded\Stdlib\now; |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @property MemoryClient client |
|
22
|
|
|
*/ |
|
23
|
|
|
final class MemoryClient implements Cache |
|
24
|
|
|
{ |
|
25
|
|
|
use ClientTrait, MultiplesTrait; |
|
26
|
|
|
|
|
27
|
|
|
private $storage = []; |
|
28
|
|
|
private $expiration = []; |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
201 |
|
public function __construct(int $ttl = null) |
|
32
|
|
|
{ |
|
33
|
201 |
|
$this->ttl = $ttl; |
|
34
|
201 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
64 |
|
public function get($key, $default = null) |
|
38
|
|
|
{ |
|
39
|
64 |
|
return $this->has($key) ? $this->storage[$key] : $default; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
78 |
|
public function set($key, $value, $ttl = null) |
|
44
|
|
|
{ |
|
45
|
78 |
|
verify_key($key); |
|
46
|
|
|
|
|
47
|
61 |
|
if (1 > $expiration = $this->timestampWithGlobalTtl($ttl, Cache::DATE_FAR_FAR_AWAY)) { |
|
48
|
2 |
|
unset($this->storage[$key], $this->expiration[$key]); |
|
49
|
|
|
} else { |
|
50
|
|
|
// Loose the reference to the object |
|
51
|
50 |
|
$this->storage[$key] = is_object($value) ? clone $value : $value; |
|
52
|
50 |
|
$this->expiration[$key] = $expiration; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
51 |
|
return true; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
25 |
|
public function delete($key) |
|
60
|
|
|
{ |
|
61
|
25 |
|
if (false === $this->has($key)) { |
|
62
|
6 |
|
return true; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
6 |
|
unset($this->storage[$key], $this->expiration[$key]); |
|
66
|
|
|
|
|
67
|
6 |
|
return true; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
197 |
|
public function clear() |
|
72
|
|
|
{ |
|
73
|
197 |
|
$this->storage = []; |
|
74
|
197 |
|
$this->expiration = []; |
|
75
|
|
|
|
|
76
|
197 |
|
return true; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
103 |
|
public function has($key) |
|
81
|
|
|
{ |
|
82
|
103 |
|
verify_key($key); |
|
83
|
|
|
|
|
84
|
52 |
|
if (false === array_key_exists($key, $this->expiration)) { |
|
85
|
17 |
|
return false; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
46 |
|
if ($this->expiration[$key] <= now()->getTimestamp()) { |
|
89
|
2 |
|
unset($this->storage[$key], $this->expiration[$key]); |
|
90
|
|
|
|
|
91
|
2 |
|
return false; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
46 |
|
return true; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
2 |
|
public function getExpirationFor(string $key): ?int |
|
99
|
|
|
{ |
|
100
|
2 |
|
return $this->expiration[$key] ?? null; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|