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
|
|
|
/** |
15
|
|
|
* ActionMultipleTrait. |
16
|
|
|
*/ |
17
|
|
|
trait ActionMultipleTrait |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Express Requirements by Abstract Methods. |
21
|
|
|
* |
22
|
|
|
* @param string $key The unique key of this item in the cache. |
23
|
|
|
* @param mixed $default Default value to return if the key does not exist. |
24
|
|
|
* |
25
|
|
|
* @return mixed The value of the item from the cache, or $default in case of cache miss. |
26
|
|
|
*/ |
27
|
|
|
abstract public function get(string $key, $default = null); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Express Requirements by Abstract Methods. |
31
|
|
|
* |
32
|
|
|
* @param string $key The key of the item to store. |
33
|
|
|
* @param mixed $value The value of the item to store, must be serializable. |
34
|
|
|
* @param int $ttl Optional. The TTL (time to live) value in seconds of this item. |
35
|
|
|
* If no value is sent and the driver supports TTL then the |
36
|
|
|
* library may set a default value for it or let the driver take care of that. |
37
|
|
|
* |
38
|
|
|
* @return bool True on success and false on failure. |
39
|
|
|
*/ |
40
|
|
|
abstract public function set(string $key, $value, int $ttl = 0): bool; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Express Requirements by Abstract Methods. |
44
|
|
|
* |
45
|
|
|
* @param string $key The cache item key. |
46
|
|
|
*/ |
47
|
|
|
abstract public function has(string $key): bool; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Express Requirements by Abstract Methods. |
51
|
|
|
* |
52
|
|
|
* @param string $key The unique cache key of the item to delete. |
53
|
|
|
*/ |
54
|
|
|
abstract public function delete(string $key): bool; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Obtains multiple cache items by their unique keys. |
58
|
|
|
* |
59
|
|
|
* @param array<mixed> $keys A list of keys that can obtained in a single operation. |
60
|
|
|
* @param mixed $default Default value to return for keys that do not exist. |
61
|
|
|
* |
62
|
|
|
* @return array<mixed> A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value. |
63
|
|
|
*/ |
64
|
6 |
|
public function getMultiple(array $keys, $default = null): array |
65
|
|
|
{ |
66
|
6 |
|
$arrayResult = []; |
67
|
|
|
|
68
|
6 |
|
foreach ($keys as $key) { |
69
|
6 |
|
$arrayResult[$key] = $this->get($key, $default); |
70
|
|
|
} |
71
|
|
|
|
72
|
4 |
|
return $arrayResult; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Persists a set of key => value pairs in the cache, with an optional TTL. |
77
|
|
|
* |
78
|
|
|
* @param array<mixed> $values A list of key => value pairs for a multiple-set operation. |
79
|
|
|
* @param int $ttl Optional. The TTL (time to live) value in seconds of this item. |
80
|
|
|
* If no value is sent and the driver supports TTL then the |
81
|
|
|
* library may set a default value for it or let the driver take care of that. |
82
|
|
|
* |
83
|
|
|
* @return bool True on success and false on failure. |
84
|
|
|
*/ |
85
|
8 |
|
public function setMultiple(array $values, int $ttl = 0): bool |
86
|
|
|
{ |
87
|
8 |
|
foreach ($values as $key => $value) { |
88
|
8 |
|
$this->set($key, $value, $ttl); |
89
|
|
|
} |
90
|
|
|
|
91
|
6 |
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Deletes multiple cache items in a single operation. |
96
|
|
|
* |
97
|
|
|
* @param array<mixed> $keys A list of string-based keys to be deleted. |
98
|
|
|
* |
99
|
|
|
* @return bool True if the items were successfully removed. False if there was an error. |
100
|
|
|
*/ |
101
|
4 |
|
public function deleteMultiple(array $keys): bool |
102
|
|
|
{ |
103
|
4 |
|
foreach ($keys as $key) { |
104
|
4 |
|
$this->delete($key); |
105
|
|
|
} |
106
|
|
|
|
107
|
2 |
|
return true; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|