1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_cache_psr\Adapters\SimpleCache; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use DateInterval; |
7
|
|
|
use kalanis\kw_cache\CacheException; |
8
|
|
|
use kalanis\kw_cache\Interfaces\IFormat; |
9
|
|
|
use kalanis\kw_cache_psr\InvalidArgumentException; |
10
|
|
|
use kalanis\kw_cache_psr\Traits\TCheckKey; |
11
|
|
|
use kalanis\kw_cache_psr\Traits\TTtl; |
12
|
|
|
use kalanis\kw_storage\Interfaces\IStorage; |
13
|
|
|
use kalanis\kw_storage\StorageException; |
14
|
|
|
use Psr\SimpleCache\CacheInterface; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class StorageAdapter |
19
|
|
|
* @package kalanis\kw_cache_psr\Adapters\SimpleCache |
20
|
|
|
* Storage adapter for PSR Cache Interface |
21
|
|
|
*/ |
22
|
|
|
class StorageAdapter implements CacheInterface |
23
|
|
|
{ |
24
|
|
|
use TCheckKey; |
25
|
|
|
use TTtl; |
26
|
|
|
|
27
|
|
|
protected IStorage $storage; |
28
|
|
|
protected IFormat $format; |
29
|
|
|
|
30
|
9 |
|
public function __construct(IStorage $storage, IFormat $format) |
31
|
|
|
{ |
32
|
9 |
|
$this->storage = $storage; |
33
|
9 |
|
$this->format = $format; |
34
|
9 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $key |
38
|
|
|
* @param mixed $default |
39
|
|
|
* @throws InvalidArgumentException |
40
|
|
|
* @return mixed|null |
41
|
|
|
*/ |
42
|
4 |
|
public function get($key, $default = null) |
43
|
|
|
{ |
44
|
|
|
try { |
45
|
4 |
|
if ($this->has($key)) { |
46
|
3 |
|
return $this->format->unpack($this->storage->read($this->checkKey($key))); |
47
|
|
|
} |
48
|
2 |
|
return $default; |
49
|
1 |
|
} catch (CacheException | StorageException $ex) { |
50
|
1 |
|
return $default; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $key |
56
|
|
|
* @param mixed $value |
57
|
|
|
* @param DateInterval|int|null $ttl |
58
|
|
|
* @throws InvalidArgumentException |
59
|
|
|
* @throws \kalanis\kw_cache_psr\CacheException |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
5 |
|
public function set($key, $value, $ttl = null): bool |
63
|
|
|
{ |
64
|
|
|
try { |
65
|
5 |
|
return $this->storage->write($this->checkKey($key), strval($this->format->pack($value)), $this->timeToInt($ttl)); |
66
|
1 |
|
} catch (CacheException | StorageException $ex) { |
67
|
1 |
|
return false; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $key |
73
|
|
|
* @throws InvalidArgumentException |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
3 |
|
public function delete($key): bool |
77
|
|
|
{ |
78
|
|
|
try { |
79
|
3 |
|
return $this->storage->remove($this->checkKey($key)); |
80
|
1 |
|
} catch (StorageException $ex) { |
81
|
1 |
|
return false; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
3 |
|
public function clear(): bool |
86
|
|
|
{ |
87
|
|
|
try { |
88
|
3 |
|
$keys = []; |
89
|
3 |
|
foreach ($this->storage->lookup('') as $item) { |
90
|
2 |
|
$keys[] = $item; |
91
|
|
|
} |
92
|
2 |
|
$result = true; |
93
|
2 |
|
foreach ($this->storage->removeMulti($keys) as $key => $status) { |
94
|
2 |
|
$result = $result && $status; |
95
|
|
|
} |
96
|
2 |
|
return $result; |
97
|
1 |
|
} catch (StorageException $ex) { |
98
|
1 |
|
return false; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param iterable<string|int, string> $keys |
104
|
|
|
* @param mixed $default |
105
|
|
|
* @throws InvalidArgumentException |
106
|
|
|
* @return iterable<string, mixed> |
107
|
|
|
*/ |
108
|
1 |
|
public function getMultiple($keys, $default = null): iterable |
109
|
|
|
{ |
110
|
1 |
|
$result = []; |
111
|
1 |
|
foreach ($keys as $key) { |
112
|
1 |
|
$result[$key] = $this->get($key, $default); |
113
|
|
|
} |
114
|
1 |
|
return $result; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param iterable<string, mixed> $values |
119
|
|
|
* @param null|int|DateInterval $ttl |
120
|
|
|
* @throws InvalidArgumentException |
121
|
|
|
* @throws \kalanis\kw_cache_psr\CacheException |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
1 |
|
public function setMultiple($values, $ttl = null): bool |
125
|
|
|
{ |
126
|
1 |
|
$result = true; |
127
|
1 |
|
foreach ($values as $key => $value) { |
128
|
1 |
|
$result = $result && $this->set($key, $value, $ttl); |
129
|
|
|
} |
130
|
1 |
|
return $result; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param iterable<string|int, string> $keys |
135
|
|
|
* @throws InvalidArgumentException |
136
|
|
|
* @return bool |
137
|
|
|
*/ |
138
|
1 |
|
public function deleteMultiple($keys): bool |
139
|
|
|
{ |
140
|
1 |
|
$result = true; |
141
|
1 |
|
foreach ($keys as $key => $item) { |
142
|
1 |
|
$result = $result && $this->delete($item); |
143
|
|
|
} |
144
|
1 |
|
return $result; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param string $key |
149
|
|
|
* @throws InvalidArgumentException |
150
|
|
|
* @return bool |
151
|
|
|
*/ |
152
|
6 |
|
public function has($key): bool |
153
|
|
|
{ |
154
|
|
|
try { |
155
|
6 |
|
return $this->storage->exists($this->checkKey($key)); |
156
|
1 |
|
} catch (StorageException $ex) { |
157
|
1 |
|
return false; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|