1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cmp\Cache\Backend; |
4
|
|
|
|
5
|
|
|
use Cmp\Cache\Cache; |
6
|
|
|
use Cmp\Cache\Exceptions\NotFoundException; |
7
|
|
|
use Cmp\Cache\Traits\MultiCacheTrait; |
8
|
|
|
use Redis; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class RedisCache |
12
|
|
|
* |
13
|
|
|
* A redis powered backend for caching |
14
|
|
|
* |
15
|
|
|
* @package Cmp\Cache\Infrastureture\Backend |
16
|
|
|
*/ |
17
|
|
|
class RedisCache extends TaggableCache |
18
|
|
|
{ |
19
|
|
|
use MultiCacheTrait { |
20
|
|
|
MultiCacheTrait::setItems as setItemsTrait; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
const DEFAULT_HOST = '127.0.0.1'; |
24
|
|
|
const DEFAULT_PORT = 6379; |
25
|
|
|
const DEFAULT_DB = 0; |
26
|
|
|
const DEFAULT_TIMEOUT = 0.0; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Redis |
30
|
|
|
*/ |
31
|
|
|
private $client; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* RedisCache constructor. |
35
|
|
|
* |
36
|
|
|
* @param Redis $client |
37
|
|
|
*/ |
38
|
1 |
|
public function __construct(Redis $client) |
39
|
|
|
{ |
40
|
1 |
|
$this->client = $client; |
41
|
1 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
1 |
|
public function set($key, $value, $timeToLive = null) |
47
|
|
|
{ |
48
|
1 |
|
if ($timeToLive > 0) { |
49
|
1 |
|
return $this->client->setex($key, $timeToLive, $value); |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
return $this->client->set($key, $value); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
1 |
|
public function setItems(array $items, $timeToLive = null) |
59
|
|
|
{ |
60
|
1 |
|
if (!$timeToLive) { |
61
|
1 |
|
return $this->client->mset($items); |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
return $this->setItemsTrait($items, $timeToLive); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
1 |
|
public function has($key) |
71
|
|
|
{ |
72
|
1 |
|
return $this->client->exists($key); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
1 |
|
public function demand($key) |
79
|
|
|
{ |
80
|
1 |
|
$value = $this->client->get($key); |
81
|
|
|
|
82
|
1 |
|
if (!$value) { |
83
|
1 |
|
throw new NotFoundException($key); |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
return $value; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
1 |
|
public function get($key, $default = null) |
93
|
|
|
{ |
94
|
|
|
try { |
95
|
1 |
|
return $this->demand($key); |
96
|
1 |
|
} catch (NotFoundException $exception) { |
97
|
1 |
|
return $default; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
1 |
|
public function getItems(array $keys) |
105
|
|
|
{ |
106
|
1 |
|
$items = []; |
107
|
|
|
|
108
|
1 |
|
$values = $this->client->mget($keys); |
109
|
1 |
|
foreach ($keys as $index => $key) { |
110
|
1 |
|
$items[$key] = $values[$index] === false ? null : $values[$index]; |
111
|
1 |
|
} |
112
|
|
|
|
113
|
1 |
|
return $items; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
1 |
|
public function delete($key) |
120
|
|
|
{ |
121
|
1 |
|
return (bool) $this->client->delete($key); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
1 |
|
public function deleteItems(array $keys) |
128
|
|
|
{ |
129
|
1 |
|
return call_user_func_array([$this->client, 'delete'], $keys) > 0; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritdoc} |
134
|
|
|
*/ |
135
|
1 |
|
public function flush() |
136
|
|
|
{ |
137
|
1 |
|
return $this->client->flushDB(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* {@inheritdoc} |
142
|
|
|
*/ |
143
|
1 |
|
public function getTimeToLive($key) |
144
|
|
|
{ |
145
|
1 |
|
$timeToLive = $this->client->ttl($key); |
146
|
|
|
|
147
|
1 |
|
return false === $timeToLive || $timeToLive <= 0 ? null : $timeToLive; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|