|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sugarcrm\UpgradeSpec\Cache\Adapter; |
|
4
|
|
|
|
|
5
|
|
|
use Sugarcrm\UpgradeSpec\Cache\Exception\InvalidArgumentException; |
|
6
|
|
|
|
|
7
|
|
|
trait AdapterTrait |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Default TTL. |
|
11
|
|
|
* |
|
12
|
|
|
* @var int |
|
13
|
|
|
*/ |
|
14
|
|
|
private $ttl = 0; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Obtains multiple cache items by their unique keys. |
|
18
|
|
|
* |
|
19
|
|
|
* @param iterable $keys a list of keys that can obtained in a single operation |
|
20
|
|
|
* @param mixed $default default value to return for keys that do not exist |
|
21
|
|
|
* |
|
22
|
|
|
* @return \Generator A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value. |
|
23
|
|
|
*/ |
|
24
|
|
|
public function getMultiple($keys, $default = null) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->validateIterableKey($keys); |
|
27
|
|
|
|
|
28
|
|
|
foreach ($keys as $key) { |
|
29
|
|
|
yield $key => $this->get($key, $default); |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Persists a set of key => value pairs in the cache, with an optional TTL. |
|
35
|
|
|
* |
|
36
|
|
|
* @param iterable $values a list of key => value pairs for a multiple-set operation |
|
37
|
|
|
* @param null|int|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and |
|
38
|
|
|
* the driver supports TTL then the library may set a default value |
|
39
|
|
|
* for it or let the driver take care of that. |
|
40
|
|
|
* |
|
41
|
|
|
* @return bool true on success and false on failure |
|
42
|
|
|
*/ |
|
43
|
|
|
public function setMultiple($values, $ttl = null) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->validateIterableKey($values); |
|
46
|
|
|
|
|
47
|
|
|
$result = true; |
|
48
|
|
|
foreach ($values as $key => $value) { |
|
49
|
|
|
if (!$this->set($key, $value, $ttl)) { |
|
50
|
|
|
$result = false; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $result; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Deletes multiple cache items in a single operation. |
|
59
|
|
|
* |
|
60
|
|
|
* @param iterable $keys a list of string-based keys to be deleted |
|
61
|
|
|
* |
|
62
|
|
|
* @return bool True if the items were successfully removed. False if there was an error. |
|
63
|
|
|
*/ |
|
64
|
|
|
public function deleteMultiple($keys) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->validateIterableKey($keys); |
|
67
|
|
|
|
|
68
|
|
|
$result = true; |
|
69
|
|
|
foreach ($keys as $key) { |
|
70
|
|
|
if (!$this->delete($key)) { |
|
71
|
|
|
$result = false; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $result; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Fetches a value from the cache. |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $key The unique key of this item in the cache. |
|
82
|
|
|
* @param mixed $default Default value to return if the key does not exist. |
|
83
|
|
|
* |
|
84
|
|
|
* @return mixed The value of the item from the cache, or $default in case of cache miss. |
|
85
|
|
|
* |
|
86
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
|
87
|
|
|
* MUST be thrown if the $key string is not a legal value. |
|
88
|
|
|
*/ |
|
89
|
|
|
public abstract function get($key, $default = null); |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $key The key of the item to store. |
|
95
|
|
|
* @param mixed $value The value of the item to store, must be serializable. |
|
96
|
|
|
* @param null|int|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and |
|
97
|
|
|
* the driver supports TTL then the library may set a default value |
|
98
|
|
|
* for it or let the driver take care of that. |
|
99
|
|
|
* |
|
100
|
|
|
* @return bool True on success and false on failure. |
|
101
|
|
|
* |
|
102
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
|
103
|
|
|
* MUST be thrown if the $key string is not a legal value. |
|
104
|
|
|
*/ |
|
105
|
|
|
public abstract function set($key, $value, $ttl = null); |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Delete an item from the cache by its unique key. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $key The unique cache key of the item to delete. |
|
111
|
|
|
* |
|
112
|
|
|
* @return bool True if the item was successfully removed. False if there was an error. |
|
113
|
|
|
* |
|
114
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
|
115
|
|
|
* MUST be thrown if the $key string is not a legal value. |
|
116
|
|
|
*/ |
|
117
|
|
|
public abstract function delete($key); |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param $key |
|
121
|
|
|
* |
|
122
|
|
|
* @throws InvalidArgumentException |
|
123
|
|
|
*/ |
|
124
|
|
|
private function validateIterableKey($key) |
|
125
|
|
|
{ |
|
126
|
|
|
if (!is_array($key) && !$key instanceof \Traversable) { |
|
127
|
|
|
throw new InvalidArgumentException('Argument is not traversable'); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param string $key |
|
133
|
|
|
* |
|
134
|
|
|
* @throws InvalidArgumentException |
|
135
|
|
|
*/ |
|
136
|
|
|
private function validateKey($key) |
|
137
|
|
|
{ |
|
138
|
|
|
if (!is_string($key) || empty($key) || mb_strlen($key) > 64) { |
|
139
|
|
|
throw new InvalidArgumentException(sprintf('Invalid cache key: %s', $key)); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
if (preg_match('/\{|\}|\(|\)|\/|\\\\|\@|\:/u', $key)) { |
|
143
|
|
|
throw new InvalidArgumentException('"{}()/\@:" characters are reserved by PSR-16'); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param $ttl |
|
149
|
|
|
* |
|
150
|
|
|
* @return int |
|
151
|
|
|
* |
|
152
|
|
|
* @throws InvalidArgumentException |
|
153
|
|
|
*/ |
|
154
|
|
|
private function getExpire($ttl) |
|
155
|
|
|
{ |
|
156
|
|
|
$expire = time() + $this->ttl; |
|
157
|
|
|
if ($ttl instanceof \DateInterval) { |
|
158
|
|
|
$expire = (new \DateTime('now'))->add($ttl)->getTimeStamp(); |
|
159
|
|
|
} elseif (is_int($ttl) || ctype_digit($ttl)) { |
|
160
|
|
|
$expire = time() + $ttl; |
|
161
|
|
|
} elseif ($ttl !== null) { |
|
162
|
|
|
throw new InvalidArgumentException(sprintf('Invalid TTL: %s', print_r($ttl, true))); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return $expire; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|