1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* This file is part of phpFastCache. |
5
|
|
|
* |
6
|
|
|
* @license MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* For full copyright and license information, please see the docs/CREDITS.txt file. |
9
|
|
|
* |
10
|
|
|
* @author Khoa Bui (khoaofgod) <[email protected]> http://www.phpfastcache.com |
11
|
|
|
* @author Georges.L (Geolim4) <[email protected]> |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace phpFastCache\Drivers\Predis; |
16
|
|
|
|
17
|
|
|
use phpFastCache\Core\DriverAbstract; |
18
|
|
|
use phpFastCache\Core\StandardPsr6StructureTrait; |
19
|
|
|
use phpFastCache\Entities\driverStatistic; |
20
|
|
|
use phpFastCache\Exceptions\phpFastCacheDriverCheckException; |
21
|
|
|
use phpFastCache\Exceptions\phpFastCacheDriverException; |
22
|
|
|
use Predis\Client as PredisClient; |
23
|
|
|
use Psr\Cache\CacheItemInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class Driver |
27
|
|
|
* @package phpFastCache\Drivers |
28
|
|
|
*/ |
29
|
|
|
class Driver extends DriverAbstract |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var PredisClient Instance of driver service |
33
|
|
|
*/ |
34
|
|
|
public $instance; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Driver constructor. |
38
|
|
|
* @param array $config |
39
|
|
|
* @throws phpFastCacheDriverException |
40
|
|
|
*/ |
41
|
|
|
public function __construct(array $config = []) |
42
|
|
|
{ |
43
|
|
|
$this->setup($config); |
44
|
|
|
|
45
|
|
|
if (!$this->driverCheck()) { |
46
|
|
|
throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName())); |
47
|
|
|
} else { |
48
|
|
|
$this->driverConnect(); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
|
|
public function driverCheck() |
56
|
|
|
{ |
57
|
|
|
if (extension_loaded('Redis')) { |
58
|
|
|
trigger_error('The native Redis extension is installed, you should use Redis instead of Predis to increase performances', E_USER_NOTICE); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return class_exists('Predis\Client'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
66
|
|
|
* @return mixed |
67
|
|
|
* @throws \InvalidArgumentException |
68
|
|
|
*/ |
69
|
|
View Code Duplication |
protected function driverWrite(CacheItemInterface $item) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
/** |
72
|
|
|
* Check for Cross-Driver type confusion |
73
|
|
|
*/ |
74
|
|
|
if ($item instanceof Item) { |
75
|
|
|
$ttl = $item->getExpirationDate()->getTimestamp() - time(); |
76
|
|
|
|
77
|
|
|
return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item))); |
78
|
|
|
} else { |
79
|
|
|
throw new \InvalidArgumentException('Cross-Driver type confusion detected'); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
85
|
|
|
* @return mixed |
86
|
|
|
*/ |
87
|
|
View Code Duplication |
protected function driverRead(CacheItemInterface $item) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$val = $this->instance->get($item->getKey()); |
90
|
|
|
if ($val == false) { |
91
|
|
|
return null; |
92
|
|
|
} else { |
93
|
|
|
return $this->decode($val); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
99
|
|
|
* @return bool |
100
|
|
|
* @throws \InvalidArgumentException |
101
|
|
|
*/ |
102
|
|
|
protected function driverDelete(CacheItemInterface $item) |
103
|
|
|
{ |
104
|
|
|
/** |
105
|
|
|
* Check for Cross-Driver type confusion |
106
|
|
|
*/ |
107
|
|
|
if ($item instanceof Item) { |
108
|
|
|
return $this->instance->del($item->getKey()); |
109
|
|
|
} else { |
110
|
|
|
throw new \InvalidArgumentException('Cross-Driver type confusion detected'); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
|
|
protected function driverClear() |
118
|
|
|
{ |
119
|
|
|
return $this->instance->flushDB(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
|
protected function driverConnect() |
126
|
|
|
{ |
127
|
|
|
$server = isset($this->config[ 'redis' ]) ? $this->config[ 'redis' ] : [ |
128
|
|
|
'host' => '127.0.0.1', |
129
|
|
|
'port' => '6379', |
130
|
|
|
'password' => '', |
131
|
|
|
'database' => '', |
132
|
|
|
]; |
133
|
|
|
|
134
|
|
|
$config = [ |
135
|
|
|
'host' => $server[ 'host' ], |
136
|
|
|
]; |
137
|
|
|
|
138
|
|
|
$port = isset($server[ 'port' ]) ? $server[ 'port' ] : ''; |
139
|
|
|
if ($port != '') { |
140
|
|
|
$config[ 'port' ] = $port; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$password = isset($server[ 'password' ]) ? $server[ 'password' ] : ''; |
144
|
|
|
if ($password != '') { |
145
|
|
|
$config[ 'password' ] = $password; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$database = isset($server[ 'database' ]) ? $server[ 'database' ] : ''; |
149
|
|
|
if ($database != '') { |
150
|
|
|
$config[ 'database' ] = $database; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$timeout = isset($server[ 'timeout' ]) ? $server[ 'timeout' ] : ''; |
154
|
|
|
if ($timeout != '') { |
155
|
|
|
$config[ 'timeout' ] = $timeout; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$read_write_timeout = isset($server[ 'read_write_timeout' ]) ? $server[ 'read_write_timeout' ] : ''; |
159
|
|
|
if ($read_write_timeout != '') { |
160
|
|
|
$config[ 'read_write_timeout' ] = $read_write_timeout; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$this->instance = new PredisClient($config); |
164
|
|
|
|
165
|
|
|
return true; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/******************** |
169
|
|
|
* |
170
|
|
|
* PSR-6 Extended Methods |
171
|
|
|
* |
172
|
|
|
*******************/ |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return driverStatistic |
176
|
|
|
*/ |
177
|
|
|
public function getStats() |
178
|
|
|
{ |
179
|
|
|
return (new driverStatistic()) |
180
|
|
|
->setRawData($this->instance->info()) |
181
|
|
|
->setSize(0) |
182
|
|
|
->setInfo(''); |
183
|
|
|
} |
184
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.