1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Test\Support\SimpleCache; |
6
|
|
|
|
7
|
|
|
use Psr\SimpleCache\CacheInterface; |
8
|
|
|
use Traversable; |
9
|
|
|
use Yiisoft\Test\Support\SimpleCache\Exception\InvalidArgumentException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @template TCacheService as CacheInterface |
13
|
|
|
*/ |
14
|
|
|
final class SimpleCacheActionLogger implements CacheInterface |
15
|
|
|
{ |
16
|
|
|
/** @var Action[] */ |
17
|
|
|
private array $actions = []; |
18
|
|
|
/** @psalm-var TCacheService */ |
19
|
|
|
private CacheInterface $cacheService; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* SimpleCacheActionLogger constructor. |
23
|
|
|
* |
24
|
|
|
* @param array $cacheData |
25
|
|
|
* @param CacheInterface $cacheService |
26
|
|
|
* @psalm-param TCacheService $cacheService |
27
|
|
|
*/ |
28
|
93 |
|
public function __construct(CacheInterface $cacheService, array $cacheData = []) |
29
|
|
|
{ |
30
|
93 |
|
$this->cacheService = $cacheService; |
31
|
93 |
|
$this->cacheService->setMultiple($cacheData); |
32
|
93 |
|
$this->actions = []; |
33
|
93 |
|
} |
34
|
|
|
|
35
|
66 |
|
public function get($key, $default = null) |
36
|
|
|
{ |
37
|
66 |
|
$this->actions[] = Action::createGetAction($key); |
38
|
66 |
|
return $this->cacheService->get($key, $default); |
39
|
|
|
} |
40
|
|
|
|
41
|
13 |
|
public function delete($key): bool |
42
|
|
|
{ |
43
|
13 |
|
$this->actions[] = Action::createDeleteAction($key); |
44
|
13 |
|
return $this->cacheService->delete($key); |
45
|
|
|
} |
46
|
|
|
|
47
|
14 |
|
public function has($key): bool |
48
|
|
|
{ |
49
|
14 |
|
$this->actions[] = Action::createHasAction($key); |
50
|
14 |
|
return $this->cacheService->has($key); |
51
|
|
|
} |
52
|
|
|
|
53
|
83 |
|
public function clear(): bool |
54
|
|
|
{ |
55
|
83 |
|
$this->actions[] = Action::createClearAction(); |
56
|
83 |
|
return $this->cacheService->clear(); |
57
|
|
|
} |
58
|
|
|
|
59
|
75 |
|
public function set($key, $value, $ttl = null): bool |
60
|
|
|
{ |
61
|
75 |
|
$this->actions[] = Action::createSetAction($key, $value, $ttl); |
62
|
75 |
|
return $this->cacheService->set($key, $value, $ttl); |
63
|
|
|
} |
64
|
|
|
|
65
|
9 |
|
public function getMultiple($keys, $default = null): iterable |
66
|
|
|
{ |
67
|
9 |
|
$keys = $this->iterableToArray($keys); |
68
|
|
|
/** @psalm-var mixed $key */ |
69
|
8 |
|
foreach ($keys as $key) { |
70
|
8 |
|
$this->actions[] = Action::createGetAction($key); |
71
|
|
|
} |
72
|
8 |
|
return $this->cacheService->getMultiple($keys, $default); |
73
|
|
|
} |
74
|
|
|
|
75
|
11 |
|
public function setMultiple($values, $ttl = null): bool |
76
|
|
|
{ |
77
|
11 |
|
$values = $this->iterableToArray($values); |
78
|
|
|
/** @psalm-var mixed $value */ |
79
|
10 |
|
foreach ($values as $key => $value) { |
80
|
10 |
|
$this->actions[] = Action::createSetAction($key, $value, $ttl); |
81
|
|
|
} |
82
|
10 |
|
return $this->cacheService->setMultiple($values, $ttl); |
83
|
|
|
} |
84
|
|
|
|
85
|
3 |
|
public function deleteMultiple($keys): bool |
86
|
|
|
{ |
87
|
3 |
|
$keys = $this->iterableToArray($keys); |
88
|
|
|
/** @psalm-var mixed $key */ |
89
|
2 |
|
foreach ($keys as $key) { |
90
|
2 |
|
$this->actions[] = Action::createDeleteAction($key); |
91
|
|
|
} |
92
|
2 |
|
return $this->cacheService->deleteMultiple($keys); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getActions(): array |
96
|
|
|
{ |
97
|
|
|
return $this->actions; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return array<int, array{0: string, 1: mixed}> |
102
|
|
|
*/ |
103
|
|
|
public function getShortActions(): array |
104
|
|
|
{ |
105
|
|
|
$result = []; |
106
|
|
|
foreach ($this->actions as $action) { |
107
|
|
|
$result[] = [$action->getAction(), $action->getKey()]; |
108
|
|
|
} |
109
|
|
|
return $result; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return CacheInterface |
114
|
|
|
* @psalm-return TCacheService |
115
|
|
|
*/ |
116
|
|
|
public function getCacheService(): CacheInterface |
117
|
|
|
{ |
118
|
|
|
return $this->cacheService; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param mixed $iterable |
123
|
|
|
* |
124
|
|
|
* Converts iterable to array. If provided value is not iterable it throws an InvalidArgumentException |
125
|
|
|
*/ |
126
|
15 |
|
private function iterableToArray($iterable): array |
127
|
|
|
{ |
128
|
15 |
|
if (!is_iterable($iterable)) { |
129
|
3 |
|
throw new InvalidArgumentException(sprintf("Iterable is expected, got %s.", gettype($iterable))); |
130
|
|
|
} |
131
|
12 |
|
return $iterable instanceof Traversable ? iterator_to_array($iterable) : $iterable; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|