PhpRedis::deleteMultiple()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php namespace Comodojo\Cache\Drivers;
2
3
use \Comodojo\Cache\Traits\InstanceTrait;
4
use \Comodojo\Foundation\Utils\UniqueId;
5
use \RedisException;
6
use \Redis;
7
use \Exception;
8
9
/**
10
 * @package     Comodojo Cache
11
 * @author      Marco Giovinazzi <[email protected]>
12
 * @license     MIT
13
 *
14
 * LICENSE:
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
25
class PhpRedis extends AbstractDriver {
26
27
    use InstanceTrait;
28
29
    const DRIVER_NAME = "php-redis";
30
31
    /**
32
     * Connection parameters
33
     *
34
     * @param array
35
     */
36
    private $connection_parameters;
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 47
    public function __construct(array $configuration) {
42
43 47
        if ( class_exists('Redis') === false ) throw new Exception("ext-redis not available");
44
45 47
        $instance = new Redis();
46
47 47
        $this->connection_parameters = [$configuration['server'], $configuration['port'], $configuration['timeout']];
48
49 47
        $instance->connect(...$this->connection_parameters);
0 ignored issues
show
Bug introduced by
$this->connection_parameters is expanded, but the parameter $host of Redis::connect() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
        $instance->connect(/** @scrutinizer ignore-type */ ...$this->connection_parameters);
Loading history...
50
51 47
        if ( !empty($configuration['password']) ) {
52
            $instance->auth($configuration['password']);
53
        }
54
55 47
        $this->setInstance($instance);
56
57 47
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 47
    public function test() {
63
64 47
        $instance = $this->getInstance();
65
66
        try {
67
68 47
            $instance->ping();
69 47
            return true;
70
71
        } catch (RedisException $re) {
72
73
            // may be a connection error, try to reconnect first
74
            if ( $instance->connect(...$this->connection_parameters) === false ) {
75
                return false;
76
            } else {
77
                return true;
78
            }
79
80
        }
81
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 35
    public function get($key, $namespace) {
88
89
        try {
90
91 35
            $scope = $this->getNamespaceKey($namespace);
92
93 35
            if ( $scope === false ) return null;
0 ignored issues
show
introduced by
The condition $scope === false is always false.
Loading history...
94
95 27
            $shadowName = "$scope-$key";
96
97 27
            $instance = $this->getInstance();
98
99 27
            $item = $instance->get($shadowName);
100
101
        } catch (RedisException $re) {
102
103
            throw $re;
104
105
        }
106
107 27
        return $item === false ? null : $item;
108
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 28
    public function set($key, $namespace, $value, $ttl = null) {
115
116
        try {
117
118 28
            if ( $ttl == null ) $ttl = 0;
119
120 28
            $scope = $this->getNamespaceKey($namespace);
121
122 28
            if ( $scope === false ) $scope = $this->setNamespaceKey($namespace);
0 ignored issues
show
introduced by
The condition $scope === false is always false.
Loading history...
123
124 28
            if ( $scope === false ) return false;
0 ignored issues
show
introduced by
The condition $scope === false is always false.
Loading history...
125
126 28
            $shadowName = "$scope-$key";
127
128 28
            $instance = $this->getInstance();
129
130 28
            if ( $ttl == 0 ) {
131 25
                $return = $instance->set($shadowName, $value);
132
            } else {
133 28
                $return = $instance->setex($shadowName, $ttl, $value);
134
            }
135
136
        } catch (RedisException $re) {
137
138
            throw $re;
139
140
        }
141
142 28
        return (bool) $return;
143
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149 6
    public function delete($key, $namespace) {
150
151
        try {
152
153 6
            $scope = $this->getNamespaceKey($namespace);
154
155 6
            if ( $scope === false ) return false;
0 ignored issues
show
introduced by
The condition $scope === false is always false.
Loading history...
156
157 6
            $shadowName = "$scope-$key";
158
159 6
            return (bool) $this->getInstance()->delete($shadowName);
160
161
        } catch (RedisException $re) {
162
163
            throw $re;
164
165
        }
166
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172 11
    public function clear($namespace = null) {
173
174
        try {
175
176 11
            if ( $namespace == null ) {
177
178 9
                return (bool) $this->getInstance()->flushDB();
179
180
            } else {
181
182 2
                $scope = $this->getNamespaceKey($namespace);
183
184 2
                if ( $scope === false ) return false;
0 ignored issues
show
introduced by
The condition $scope === false is always false.
Loading history...
185
186 2
                return (bool) $this->getInstance()->delete($namespace);
187
188
            }
189
190
        } catch (RedisException $re) {
191
192
            throw $re;
193
194
        }
195
196
    }
197
198
    // TODO: write a better getMultiple using mGet
199
    /**
200
     * {@inheritdoc}
201
     */
202 2
    public function getMultiple(array $keys, $namespace) {
203
204 2
        $result = [];
205
206 2
        foreach ( $keys as $key ) {
207 2
            $result[$key] = $this->get($key, $namespace);
208
        }
209
210 2
        return $result;
211
212
    }
213
214
    // TODO: write a better setMultiple using mSet
215
    /**
216
     * {@inheritdoc}
217
     */
218 1
    public function setMultiple(array $key_values, $namespace, $ttl = null) {
219
220 1
        $result = [];
221
222 1
        foreach ( $key_values as $key => $value ) {
223 1
            $result[] = $this->set($key, $namespace, $value, $ttl);
224
        }
225
226 1
        return !in_array(false, $result);
227
228
    }
229
230
    // TODO: write a better deleteMultiple using delete([])
231
    /**
232
     * {@inheritdoc}
233
     */
234 3
    public function deleteMultiple(array $keys, $namespace) {
235
236 3
        $result = [];
237
238 3
        foreach ( $keys as $key ) {
239 3
            $result[] = $this->delete($key, $namespace);
240
        }
241
242 3
        return !in_array(false, $result);
243
244
    }
245
246
    /**
247
     * {@inheritdoc}
248
     */
249 5
    public function has($key, $namespace) {
250
251
        try {
252
253 5
            $scope = $this->getNamespaceKey($namespace);
254
255 5
            if ( $scope === false ) $scope = $this->setNamespaceKey($namespace);
0 ignored issues
show
introduced by
The condition $scope === false is always false.
Loading history...
256
257 5
            if ( $scope === false ) return false;
0 ignored issues
show
introduced by
The condition $scope === false is always false.
Loading history...
258
259 5
            $shadowName = "$scope-$key";
260
261 5
            return (bool) $this->getInstance()->exists($shadowName);
262
263
        } catch (RedisException $re) {
264
265
            throw $re;
266
267
        }
268
269
    }
270
271
    /**
272
     * {@inheritdoc}
273
     */
274 2
    public function stats() {
275
276 2
        $instance = $this->getInstance();
277
278
        try {
279
280 2
            $objects = $instance->dbSize();
281 2
            $stats = $instance->info();
282
283
        } catch (RedisException $re) {
284
285
            throw $re;
286
287
        }
288
289 2
        return ['objects' => $objects, 'stats' => $stats];
290
291
    }
292
293
    /**
294
     * Set namespace key
295
     *
296
     * @return  mixed
297
     */
298 5
    private function setNamespaceKey($namespace) {
299
300 5
        $uId = UniqueId::generate(64);
301
302 5
        $return = $this->getInstance()->set($namespace, $uId);
303
304 5
        return $return === false ? false : $uId;
305
306
    }
307
308
    /**
309
     * Get namespace key
310
     *
311
     * @return  string
312
     */
313 37
    private function getNamespaceKey($namespace) {
314
315 37
        return $this->getInstance()->get($namespace);
316
317
    }
318
319
}
320