1
|
|
|
<?php |
2
|
|
|
namespace Genkgo\Cache\Adapter; |
3
|
|
|
|
4
|
|
|
use Genkgo\Cache\CacheAdapterInterface; |
5
|
|
|
use Genkgo\Cache\SerializerInterface; |
6
|
|
|
use Predis\ClientInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class PredisAdapter |
10
|
|
|
* @package Genkgo\Cache\Adapter |
11
|
|
|
*/ |
12
|
|
|
class PredisAdapter implements CacheAdapterInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var ClientInterface |
16
|
|
|
*/ |
17
|
|
|
private $client; |
18
|
|
|
/** |
19
|
|
|
* @var SerializerInterface |
20
|
|
|
*/ |
21
|
|
|
private $serializer; |
22
|
|
|
/** |
23
|
|
|
* @var int|null |
24
|
|
|
*/ |
25
|
|
|
private $ttl; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param ClientInterface $client |
29
|
|
|
* @param SerializerInterface $serializer |
30
|
|
|
* @param null $ttl |
31
|
|
|
*/ |
32
|
6 |
|
public function __construct( |
33
|
|
|
ClientInterface $client, |
34
|
|
|
SerializerInterface $serializer, |
35
|
|
|
$ttl = null |
36
|
|
|
) { |
37
|
6 |
|
$this->client = $client; |
38
|
6 |
|
$this->serializer = $serializer; |
39
|
6 |
|
$this->ttl = $ttl; |
40
|
6 |
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Gets a cache entry |
45
|
|
|
* returning null if not in cache |
46
|
|
|
* |
47
|
|
|
* @param $key |
48
|
|
|
* @return null|mixed |
49
|
|
|
*/ |
50
|
2 |
|
public function get($key) |
51
|
|
|
{ |
52
|
2 |
|
return $this->serializer->deserialize($this->client->get($key)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Sets a cache entry |
57
|
|
|
* |
58
|
|
|
* @param $key |
59
|
|
|
* @param $value |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
5 |
|
public function set($key, $value) |
63
|
|
|
{ |
64
|
5 |
|
if ($this->ttl === null) { |
65
|
4 |
|
$this->client->set($key, $this->serializer->serialize($value)); |
66
|
4 |
|
} else { |
67
|
1 |
|
$this->client->set($key, $this->serializer->serialize($value), 'ex', $this->ttl); |
68
|
|
|
} |
69
|
5 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Deletes a cache entry |
73
|
|
|
* |
74
|
|
|
* @param $key |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
3 |
|
public function delete($key) |
78
|
|
|
{ |
79
|
3 |
|
if (strpos($key, '*') !== false) { |
80
|
2 |
|
$this->deleteGlob($key); |
81
|
2 |
|
} else { |
82
|
1 |
|
$this->client->del([$key]); |
83
|
|
|
} |
84
|
3 |
|
} |
85
|
|
|
|
86
|
2 |
|
private function deleteGlob ($pattern) { |
87
|
2 |
|
$keys = $this->client->keys($pattern); |
88
|
2 |
|
$options = $this->client->getOptions(); |
89
|
2 |
|
if (isset($options->prefix)) { |
|
|
|
|
90
|
1 |
|
$length = strlen($options->prefix->getPrefix()); |
|
|
|
|
91
|
|
|
|
92
|
1 |
|
$keys = array_map(function ($key) use ($length) { |
93
|
1 |
|
return substr($key, $length); |
94
|
1 |
|
}, $keys); |
95
|
1 |
|
} |
96
|
|
|
|
97
|
2 |
|
if (count($keys) === 0) { |
98
|
|
|
return; |
99
|
|
|
} |
100
|
|
|
|
101
|
2 |
|
$this->client->del($keys); |
102
|
2 |
|
} |
103
|
|
|
} |
104
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: