1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ICanBoogie package. |
5
|
|
|
* |
6
|
|
|
* (c) Olivier Laviale <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ICanBoogie\Storage; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* A storage using Redis. |
16
|
|
|
*/ |
17
|
|
|
class RedisStorage implements Storage, \ArrayAccess |
18
|
|
|
{ |
19
|
|
|
use Storage\ArrayAccess; |
20
|
|
|
use Storage\ClearWithIterator; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var \Redis |
24
|
|
|
*/ |
25
|
|
|
private $redis; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $prefix; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param \Redis|mixed $redis |
34
|
|
|
*/ |
35
|
|
|
public function __construct($redis, string $prefix) |
36
|
|
|
{ |
37
|
|
|
$this->redis = $redis; |
38
|
|
|
$this->prefix = $prefix; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
*/ |
44
|
|
|
public function retrieve(string $key) |
45
|
|
|
{ |
46
|
|
|
$value = $this->redis->get($this->prefix . $key); |
47
|
|
|
|
48
|
|
|
if ($value === false) |
49
|
|
|
{ |
50
|
|
|
return null; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return unserialize($value); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @inheritdoc |
58
|
|
|
*/ |
59
|
|
|
public function exists(string $key): bool |
60
|
|
|
{ |
61
|
|
|
return (bool) $this->redis->exists($this->prefix . $key); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @inheritdoc |
66
|
|
|
*/ |
67
|
|
|
public function store(string $key, $value, int $ttl = null): void |
68
|
|
|
{ |
69
|
|
|
$key = $this->prefix . $key; |
70
|
|
|
|
71
|
|
|
if ($ttl) |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$this->redis->set($key, serialize($value), $ttl); |
74
|
|
|
|
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->redis->set($key, serialize($value)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @inheritdoc |
83
|
|
|
*/ |
84
|
|
|
public function eliminate(string $key): void |
85
|
|
|
{ |
86
|
|
|
$this->redis->del($this->prefix . $key); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @inheritdoc |
91
|
|
|
*/ |
92
|
|
|
public function getIterator(): iterable |
93
|
|
|
{ |
94
|
|
|
$redis = $this->redis; |
95
|
|
|
$prefix = $this->prefix; |
96
|
|
|
$prefix_length = strlen($prefix); |
97
|
|
|
$it = null; |
98
|
|
|
|
99
|
|
|
while(($keys = $redis->scan($it))) |
100
|
|
|
{ |
101
|
|
|
foreach ($keys as $internal_key) |
102
|
|
|
{ |
103
|
|
|
if (strpos($internal_key, $prefix) !== 0) |
104
|
|
|
{ |
105
|
|
|
continue; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
yield substr($internal_key, $prefix_length); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: