|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Shopware\Core\Test\Stub\Redis; |
|
4
|
|
|
|
|
5
|
|
|
class RedisStub extends \Redis |
|
6
|
|
|
{ |
|
7
|
|
|
use RedisCompatibility; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @var array<string, mixed> |
|
11
|
|
|
*/ |
|
12
|
|
|
private array $data = []; |
|
13
|
|
|
|
|
14
|
|
|
public function __construct() |
|
15
|
|
|
{ |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
private function doGet(string $key): mixed |
|
|
|
|
|
|
19
|
|
|
{ |
|
20
|
|
|
if (\array_key_exists($key, $this->data)) { |
|
21
|
|
|
$value = $this->data[$key]; |
|
22
|
|
|
|
|
23
|
|
|
if ($value['expire'] !== 0 && $value['expire'] < time()) { |
|
24
|
|
|
unset($this->data[$key]); |
|
25
|
|
|
|
|
26
|
|
|
return false; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
return $value['value']; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
return false; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
private function doSet(string $key, mixed $value, mixed $options = null): bool |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
|
|
$expire = 0; |
|
38
|
|
|
|
|
39
|
|
|
if (\is_array($options)) { |
|
40
|
|
|
if (isset($options['ex'])) { |
|
41
|
|
|
$expire = time() + $options['ex']; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if (isset($options['EX'])) { |
|
45
|
|
|
$expire = time() + $options['EX']; |
|
46
|
|
|
} |
|
47
|
|
|
} elseif (\is_int($options)) { |
|
48
|
|
|
$expire = time() + $options; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$this->data[$key] = ['value' => $value, 'expire' => $expire]; |
|
52
|
|
|
|
|
53
|
|
|
return true; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
private function doDel(string $key, string ...$other_keys): int |
|
|
|
|
|
|
57
|
|
|
{ |
|
58
|
|
|
$deletions = 0; |
|
59
|
|
|
|
|
60
|
|
|
$other_keys[] = $key; |
|
61
|
|
|
|
|
62
|
|
|
foreach ($other_keys as $otherKey) { |
|
63
|
|
|
if (\array_key_exists($otherKey, $this->data)) { |
|
64
|
|
|
unset($this->data[$otherKey]); |
|
65
|
|
|
++$deletions; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $deletions; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
private function doExists(mixed $key, mixed ...$other_keys): int|bool |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
if ($other_keys === []) { |
|
75
|
|
|
return \array_key_exists($key, $this->data); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$keys = array_merge([$key], $other_keys); |
|
79
|
|
|
|
|
80
|
|
|
$found = 0; |
|
81
|
|
|
|
|
82
|
|
|
foreach ($keys as $keyLoop) { |
|
83
|
|
|
if (\array_key_exists($keyLoop, $this->data)) { |
|
84
|
|
|
++$found; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $found; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
private function doSAdd(string $key, mixed $value, mixed ...$other_values): int |
|
|
|
|
|
|
92
|
|
|
{ |
|
93
|
|
|
$current = $this->get($key); |
|
94
|
|
|
|
|
95
|
|
|
if ($current === false) { |
|
96
|
|
|
$current = []; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if (!\is_array($current)) { |
|
100
|
|
|
throw new \RedisException('sAdd can be only called on a set'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$current = array_merge($current, [$value], $other_values); |
|
104
|
|
|
$current = array_unique($current); |
|
105
|
|
|
|
|
106
|
|
|
sort($current); |
|
107
|
|
|
|
|
108
|
|
|
$this->data[$key] = ['value' => $current, 'expire' => $current]; |
|
109
|
|
|
|
|
110
|
|
|
return 1; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return list<string> |
|
|
|
|
|
|
115
|
|
|
*/ |
|
116
|
|
|
private function doSMembers(string $key): array |
|
|
|
|
|
|
117
|
|
|
{ |
|
118
|
|
|
/** @var list<string>|false|string $value */ |
|
119
|
|
|
$value = $this->get($key); |
|
120
|
|
|
|
|
121
|
|
|
if ($value === false) { |
|
|
|
|
|
|
122
|
|
|
return []; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
if (!\is_array($value)) { |
|
|
|
|
|
|
126
|
|
|
throw new \RedisException('sMembers can be only called on a set'); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return $value; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
private function doTtl(string $key): int|false |
|
|
|
|
|
|
133
|
|
|
{ |
|
134
|
|
|
if (\array_key_exists($key, $this->data)) { |
|
135
|
|
|
$value = $this->data[$key]; |
|
136
|
|
|
|
|
137
|
|
|
// If the expiry is 0, the key will never expire |
|
138
|
|
|
if ($value['expire'] === 0) { |
|
139
|
|
|
return -1; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return $value['expire'] - time(); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return false; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.