1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* This file is part of Phpfastcache. |
6
|
|
|
* |
7
|
|
|
* @license MIT License (MIT) |
8
|
|
|
* |
9
|
|
|
* For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files. |
10
|
|
|
* |
11
|
|
|
* @author Georges.L (Geolim4) <[email protected]> |
12
|
|
|
* @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types=1); |
16
|
|
|
|
17
|
|
|
namespace Phpfastcache\Drivers\Redis; |
18
|
|
|
|
19
|
|
|
use DateTime; |
20
|
|
|
use Phpfastcache\Cluster\AggregatablePoolInterface; |
21
|
|
|
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface; |
22
|
|
|
use Phpfastcache\Core\Pool\TaggableCacheItemPoolTrait; |
23
|
|
|
use Phpfastcache\Core\Item\ExtendedCacheItemInterface; |
24
|
|
|
use Phpfastcache\Entities\DriverStatistic; |
25
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException; |
26
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheLogicException; |
27
|
|
|
use Redis as RedisClient; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @property \Redis $instance |
31
|
|
|
* @method Config getConfig() |
32
|
|
|
*/ |
33
|
|
|
class Driver implements ExtendedCacheItemPoolInterface, AggregatablePoolInterface |
34
|
|
|
{ |
35
|
|
|
use TaggableCacheItemPoolTrait; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return bool |
39
|
|
|
*/ |
40
|
|
|
public function driverCheck(): bool |
41
|
|
|
{ |
42
|
|
|
return extension_loaded('Redis'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return DriverStatistic |
47
|
|
|
*/ |
48
|
|
|
public function getStats(): DriverStatistic |
49
|
|
|
{ |
50
|
|
|
// used_memory |
51
|
|
|
$info = $this->instance->info(); |
52
|
|
|
$date = (new DateTime())->setTimestamp(time() - $info['uptime_in_seconds']); |
53
|
|
|
|
54
|
|
|
return (new DriverStatistic()) |
55
|
|
|
->setData(implode(', ', array_keys($this->itemInstances))) |
56
|
|
|
->setRawData($info) |
57
|
|
|
->setSize((int)$info['used_memory']) |
58
|
|
|
->setInfo( |
59
|
|
|
sprintf( |
60
|
|
|
"The Redis daemon v%s is up since %s.\n For more information see RawData. \n Driver size includes the memory allocation size.", |
61
|
|
|
$info['redis_version'], |
62
|
|
|
$date->format(DATE_RFC2822) |
63
|
|
|
) |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return bool |
69
|
|
|
* @throws PhpfastcacheLogicException |
70
|
|
|
*/ |
71
|
|
|
protected function driverConnect(): bool |
72
|
|
|
{ |
73
|
|
|
if (isset($this->instance) && $this->instance instanceof RedisClient) { |
74
|
|
|
throw new PhpfastcacheLogicException('Already connected to Redis server'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* In case of an user-provided |
79
|
|
|
* Redis client just return here |
80
|
|
|
*/ |
81
|
|
|
if ($this->getConfig()->getRedisClient() instanceof RedisClient) { |
82
|
|
|
/** |
83
|
|
|
* Unlike Predis, we can't test if we're connected |
84
|
|
|
* or not, so let's just assume that we are |
85
|
|
|
*/ |
86
|
|
|
$this->instance = $this->getConfig()->getRedisClient(); |
87
|
|
|
return true; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->instance = $this->instance ?? new RedisClient(); |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* If path is provided we consider it as a UNIX Socket |
94
|
|
|
*/ |
95
|
|
|
if ($this->getConfig()->getPath()) { |
96
|
|
|
$isConnected = $this->instance->connect($this->getConfig()->getPath()); |
97
|
|
|
} else { |
98
|
|
|
$isConnected = $this->instance->connect($this->getConfig()->getHost(), $this->getConfig()->getPort(), $this->getConfig()->getTimeout()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (!$isConnected && $this->getConfig()->getPath()) { |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if ($this->getConfig()->getOptPrefix()) { |
106
|
|
|
$this->instance->setOption(RedisClient::OPT_PREFIX, $this->getConfig()->getOptPrefix()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if ($this->getConfig()->getPassword() && !$this->instance->auth($this->getConfig()->getPassword())) { |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if ($this->getConfig()->getDatabase() !== null) { |
|
|
|
|
114
|
|
|
$this->instance->select($this->getConfig()->getDatabase()); |
115
|
|
|
} |
116
|
|
|
return true; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param ExtendedCacheItemInterface $item |
121
|
|
|
* @return null|array |
122
|
|
|
*/ |
123
|
|
|
protected function driverRead(ExtendedCacheItemInterface $item): ?array |
124
|
|
|
{ |
125
|
|
|
$val = $this->instance->get($item->getKey()); |
126
|
|
|
if (!$val) { |
127
|
|
|
return null; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $this->decode($val); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param ExtendedCacheItemInterface $item |
135
|
|
|
* @return mixed |
136
|
|
|
* @throws PhpfastcacheInvalidArgumentException |
137
|
|
|
* @throws PhpfastcacheLogicException |
138
|
|
|
*/ |
139
|
|
|
protected function driverWrite(ExtendedCacheItemInterface $item): bool |
140
|
|
|
{ |
141
|
|
|
$this->assertCacheItemType($item, Item::class); |
142
|
|
|
|
143
|
|
|
$ttl = $item->getExpirationDate()->getTimestamp() - time(); |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @see https://redis.io/commands/setex |
147
|
|
|
* @see https://redis.io/commands/expire |
148
|
|
|
*/ |
149
|
|
|
if ($ttl <= 0) { |
150
|
|
|
return $this->instance->expire($item->getKey(), 0); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item))); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param ExtendedCacheItemInterface $item |
158
|
|
|
* @return bool |
159
|
|
|
* @throws PhpfastcacheInvalidArgumentException |
160
|
|
|
*/ |
161
|
|
|
protected function driverDelete(ExtendedCacheItemInterface $item): bool |
162
|
|
|
{ |
163
|
|
|
$this->assertCacheItemType($item, Item::class); |
164
|
|
|
|
165
|
|
|
return (bool) $this->instance->del($item->getKey()); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return bool |
170
|
|
|
*/ |
171
|
|
|
protected function driverClear(): bool |
172
|
|
|
{ |
173
|
|
|
return $this->instance->flushDB(); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|