|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Linna Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Sebastian Rapetti <[email protected]> |
|
7
|
|
|
* @copyright (c) 2018, Sebastian Rapetti |
|
8
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
9
|
|
|
*/ |
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Linna\Cache; |
|
13
|
|
|
|
|
14
|
|
|
use DateInterval; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* ActionMultipleTrait. |
|
18
|
|
|
*/ |
|
19
|
|
|
trait ActionMultipleTrait |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Fetches a value from the cache. |
|
23
|
|
|
* |
|
24
|
|
|
* @param string $key The unique key of this item in the cache. |
|
25
|
|
|
* @param mixed $default Default value to return if the key does not exist. |
|
26
|
|
|
* |
|
27
|
|
|
* @return mixed The value of the item from the cache, or $default in case of cache miss. |
|
28
|
|
|
* |
|
29
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
|
30
|
|
|
* MUST be thrown if the $key string is not a legal value. |
|
31
|
|
|
*/ |
|
32
|
|
|
abstract public function get(string $key, mixed $default = null): mixed; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time. |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $key The key of the item to store. |
|
38
|
|
|
* @param mixed $value The value of the item to store, must be serializable. |
|
39
|
|
|
* @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and |
|
40
|
|
|
* the driver supports TTL then the library may set a default value |
|
41
|
|
|
* for it or let the driver take care of that. |
|
42
|
|
|
* |
|
43
|
|
|
* @return bool True on success and false on failure. |
|
44
|
|
|
* |
|
45
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
|
46
|
|
|
* MUST be thrown if the $key string is not a legal value. |
|
47
|
|
|
*/ |
|
48
|
|
|
abstract public function set(string $key, mixed $value, DateInterval|int|null $ttl = null): bool; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Determines whether an item is present in the cache. |
|
52
|
|
|
* |
|
53
|
|
|
* NOTE: It is recommended that has() is only to be used for cache warming type purposes |
|
54
|
|
|
* and not to be used within your live applications operations for get/set, as this method |
|
55
|
|
|
* is subject to a race condition where your has() will return true and immediately after, |
|
56
|
|
|
* another script can remove it making the state of your app out of date. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $key The cache item key. |
|
59
|
|
|
* |
|
60
|
|
|
* @return bool |
|
61
|
|
|
* |
|
62
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
|
63
|
|
|
* MUST be thrown if the $key string is not a legal value. |
|
64
|
6 |
|
*/ |
|
65
|
|
|
abstract public function has(string $key): bool; |
|
66
|
6 |
|
|
|
67
|
|
|
/** |
|
68
|
6 |
|
* Delete an item from the cache by its unique key. |
|
69
|
6 |
|
* |
|
70
|
|
|
* @param string $key The unique cache key of the item to delete. |
|
71
|
|
|
* |
|
72
|
4 |
|
* @return bool True if the item was successfully removed. False if there was an error. |
|
73
|
|
|
* |
|
74
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
|
75
|
|
|
* MUST be thrown if the $key string is not a legal value. |
|
76
|
|
|
*/ |
|
77
|
|
|
abstract public function delete(string $key): bool; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Obtains multiple cache items by their unique keys. |
|
81
|
|
|
* |
|
82
|
|
|
* @param iterable<string> $keys A list of keys that can be obtained in a single operation. |
|
83
|
|
|
* @param mixed $default Default value to return for keys that do not exist. |
|
84
|
|
|
* |
|
85
|
8 |
|
* @return iterable<string, mixed> A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value. |
|
86
|
|
|
* |
|
87
|
8 |
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
|
88
|
8 |
|
* MUST be thrown if $keys is neither an array nor a Traversable, |
|
89
|
|
|
* or if any of the $keys are not a legal value. |
|
90
|
|
|
*/ |
|
91
|
6 |
|
public function getMultiple(iterable $keys, mixed $default = null): iterable |
|
92
|
|
|
{ |
|
93
|
|
|
$arrayResult = []; |
|
94
|
|
|
|
|
95
|
|
|
foreach ($keys as $key) { |
|
96
|
|
|
$arrayResult[$key] = $this->get($key, $default); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $arrayResult; |
|
100
|
|
|
} |
|
101
|
4 |
|
|
|
102
|
|
|
/** |
|
103
|
4 |
|
* Persists a set of key => value pairs in the cache, with an optional TTL. |
|
104
|
4 |
|
* |
|
105
|
|
|
* @param iterable $values A list of key => value pairs for a multiple-set operation. |
|
106
|
|
|
* @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and |
|
107
|
2 |
|
* the driver supports TTL then the library may set a default value |
|
108
|
|
|
* for it or let the driver take care of that. |
|
109
|
|
|
* |
|
110
|
|
|
* @return bool True on success and false on failure. |
|
111
|
|
|
* |
|
112
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
|
113
|
|
|
* MUST be thrown if $values is neither an array nor a Traversable, |
|
114
|
|
|
* or if any of the $values are not a legal value. |
|
115
|
|
|
*/ |
|
116
|
|
|
public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool |
|
117
|
|
|
{ |
|
118
|
|
|
foreach ($values as $key => $value) { |
|
119
|
|
|
$this->set($key, $value, $ttl); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return true; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Deletes multiple cache items in a single operation. |
|
127
|
|
|
* |
|
128
|
|
|
* @param iterable<string> $keys A list of string-based keys to be deleted. |
|
129
|
|
|
* |
|
130
|
|
|
* @return bool True if the items were successfully removed. False if there was an error. |
|
131
|
|
|
* |
|
132
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
|
133
|
|
|
* MUST be thrown if $keys is neither an array nor a Traversable, |
|
134
|
|
|
* or if any of the $keys are not a legal value. |
|
135
|
|
|
*/ |
|
136
|
|
|
public function deleteMultiple(iterable $keys): bool |
|
137
|
|
|
{ |
|
138
|
|
|
foreach ($keys as $key) { |
|
139
|
|
|
$this->delete($key); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return true; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|