|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Vectorface\Cache; |
|
4
|
|
|
|
|
5
|
|
|
use Vectorface\Cache\Common\PSR16Util; |
|
6
|
|
|
use Redis; |
|
7
|
|
|
use RedisClient\RedisClient; |
|
8
|
|
|
use Vectorface\Cache\Exception\InvalidArgumentException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* A cache implementation using phpredis/php-redis-client |
|
12
|
|
|
*/ |
|
13
|
|
|
class RedisCache implements Cache |
|
14
|
|
|
{ |
|
15
|
|
|
use PSR16Util { key as PSR16Key; } |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var Redis|RedisClient |
|
19
|
|
|
*/ |
|
20
|
|
|
private $redis; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
private $prefix; |
|
26
|
|
|
|
|
27
|
|
|
private function key($key) |
|
28
|
|
|
{ |
|
29
|
|
|
return $this->prefix . $this->PSR16Key($key); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function __construct($redis, string $prefix = '') |
|
33
|
|
|
{ |
|
34
|
|
|
if (!($redis instanceof Redis || $redis instanceof RedisClient)) { |
|
35
|
|
|
throw new InvalidArgumentException("Unsupported Redis implementation"); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$this->redis = $redis; |
|
39
|
|
|
$this->prefix = $prefix; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @inheritDoc Vectorface\Cache\Cache |
|
44
|
|
|
*/ |
|
45
|
|
|
public function get($key, $default = null) |
|
46
|
|
|
{ |
|
47
|
|
|
$result = $this->redis->get($this->key($key)); |
|
48
|
|
|
|
|
49
|
|
|
/* Not found: false in phpredis, null in php-redis-client */ |
|
50
|
|
|
$notFoundResult = ($this->redis instanceof Redis) ? false : null; |
|
51
|
|
|
|
|
52
|
|
|
return ($result !== $notFoundResult) ? $result : $default; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @inheritDoc Vectorface\Cache\Cache |
|
57
|
|
|
*/ |
|
58
|
|
|
public function set($key, $value, $ttl = null) |
|
59
|
|
|
{ |
|
60
|
|
|
$args = [$this->key($key), $this->ttl($ttl), $value]; |
|
61
|
|
|
|
|
62
|
|
|
/* Compatible signatures, different function case; probably shouldn't care. */ |
|
63
|
|
|
return ($this->redis instanceof Redis) ? $this->redis->setEx(...$args) : $this->redis->setex(...$args); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @inheritDoc Vectorface\Cache\Cache |
|
68
|
|
|
*/ |
|
69
|
|
|
public function delete($key) |
|
70
|
|
|
{ |
|
71
|
|
|
$key = $this->key($key); |
|
72
|
|
|
|
|
73
|
|
|
if ($this->redis instanceof Redis) { |
|
74
|
|
|
throw new InvalidArgumentException("write me!"); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return (bool)$this->redis->del([$key]); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @inheritDoc Vectorface\Cache\Cache |
|
82
|
|
|
*/ |
|
83
|
|
|
public function clean() |
|
84
|
|
|
{ |
|
85
|
|
|
return true; /* redis does this on its own */ |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @inheritDoc Vectorface\Cache\Cache |
|
90
|
|
|
*/ |
|
91
|
|
|
public function flush() |
|
92
|
|
|
{ |
|
93
|
|
|
if ($this->redis instanceof Redis) { |
|
94
|
|
|
throw new InvalidArgumentException("write me!"); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return (bool)$this->redis->flushdb(); // We probably don't actually want to do this |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @inheritDoc \Psr\SimpleCache\CacheInterface |
|
102
|
|
|
*/ |
|
103
|
|
|
public function clear() |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->flush(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @inheritDoc \Psr\SimpleCache\CacheInterface |
|
110
|
|
|
*/ |
|
111
|
|
|
public function has($key) |
|
112
|
|
|
{ |
|
113
|
|
|
$key = $this->key($key); |
|
114
|
|
|
|
|
115
|
|
|
if ($this->redis instanceof Redis) { |
|
116
|
|
|
throw new InvalidArgumentException("write me!"); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return (bool)$this->redis->exists($key); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @inheritDoc \Psr\SimpleCache\CacheInterface |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getMultiple($keys, $default = null) |
|
126
|
|
|
{ |
|
127
|
|
|
throw new InvalidArgumentException("write me!"); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @inheritDoc \Psr\SimpleCache\CacheInterface |
|
132
|
|
|
*/ |
|
133
|
|
|
public function setMultiple($values, $ttl = null) |
|
134
|
|
|
{ |
|
135
|
|
|
throw new InvalidArgumentException("write me!"); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @inheritDoc \Psr\SimpleCache\CacheInterface |
|
140
|
|
|
*/ |
|
141
|
|
|
public function deleteMultiple($keys) |
|
142
|
|
|
{ |
|
143
|
|
|
throw new InvalidArgumentException("write me!"); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|