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\Memcached; |
16
|
|
|
|
17
|
|
|
use Memcached as MemcachedSoftware; |
18
|
|
|
use phpFastCache\Core\Pool\DriverBaseTrait; |
19
|
|
|
use phpFastCache\Core\Pool\ExtendedCacheItemPoolInterface; |
20
|
|
|
use phpFastCache\Entities\DriverStatistic; |
21
|
|
|
use phpFastCache\Exceptions\phpFastCacheDriverCheckException; |
22
|
|
|
use phpFastCache\Exceptions\phpFastCacheDriverException; |
23
|
|
|
use phpFastCache\Exceptions\phpFastCacheInvalidArgumentException; |
24
|
|
|
use phpFastCache\Util\MemcacheDriverCollisionDetectorTrait; |
25
|
|
|
use Psr\Cache\CacheItemInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class Driver |
29
|
|
|
* @package phpFastCache\Drivers |
30
|
|
|
* @property MemcachedSoftware $instance |
31
|
|
|
*/ |
32
|
|
|
class Driver implements ExtendedCacheItemPoolInterface |
33
|
|
|
{ |
34
|
|
|
use DriverBaseTrait, MemcacheDriverCollisionDetectorTrait; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Driver constructor. |
38
|
|
|
* @param array $config |
39
|
|
|
* @throws phpFastCacheDriverException |
40
|
|
|
*/ |
41
|
|
View Code Duplication |
public function __construct(array $config = []) |
42
|
|
|
{ |
43
|
|
|
self::checkCollision('Memcached'); |
44
|
|
|
$this->setup($config); |
45
|
|
|
|
46
|
|
|
if (!$this->driverCheck()) { |
47
|
|
|
throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName())); |
48
|
|
|
} else { |
49
|
|
|
$this->driverConnect(); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
public function driverCheck() |
57
|
|
|
{ |
58
|
|
|
return class_exists('Memcached'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
63
|
|
|
* @return mixed |
64
|
|
|
* @throws phpFastCacheInvalidArgumentException |
65
|
|
|
*/ |
66
|
|
View Code Duplication |
protected function driverWrite(CacheItemInterface $item) |
67
|
|
|
{ |
68
|
|
|
/** |
69
|
|
|
* Check for Cross-Driver type confusion |
70
|
|
|
*/ |
71
|
|
|
if ($item instanceof Item) { |
72
|
|
|
$ttl = $item->getTtl(); |
73
|
|
|
|
74
|
|
|
// Memcache will only allow a expiration timer less than 2592000 seconds, |
75
|
|
|
// otherwise, it will assume you're giving it a UNIX timestamp. |
76
|
|
|
if ($ttl >= 2592000) { |
77
|
|
|
$ttl = $item->getExpirationDate()->getTimestamp(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this->instance->set($item->getKey(), $this->driverPreWrap($item), $ttl); |
81
|
|
|
} else { |
82
|
|
|
throw new phpFastCacheInvalidArgumentException('Cross-Driver type confusion detected'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return true; |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
90
|
|
|
* @return null|array |
91
|
|
|
*/ |
92
|
|
View Code Duplication |
protected function driverRead(CacheItemInterface $item) |
93
|
|
|
{ |
94
|
|
|
$val = $this->instance->get($item->getKey()); |
95
|
|
|
|
96
|
|
|
if ($val === false) { |
97
|
|
|
return null; |
98
|
|
|
} else { |
99
|
|
|
return $val; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
105
|
|
|
* @return bool |
106
|
|
|
* @throws phpFastCacheInvalidArgumentException |
107
|
|
|
*/ |
108
|
|
View Code Duplication |
protected function driverDelete(CacheItemInterface $item) |
109
|
|
|
{ |
110
|
|
|
/** |
111
|
|
|
* Check for Cross-Driver type confusion |
112
|
|
|
*/ |
113
|
|
|
if ($item instanceof Item) { |
114
|
|
|
return $this->instance->delete($item->getKey()); |
115
|
|
|
} else { |
116
|
|
|
throw new phpFastCacheInvalidArgumentException('Cross-Driver type confusion detected'); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
|
|
protected function driverClear() |
124
|
|
|
{ |
125
|
|
|
return $this->instance->flush(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
|
|
protected function driverConnect() |
132
|
|
|
{ |
133
|
|
|
$this->instance = new MemcachedSoftware(); |
134
|
|
|
$this->instance->setOption(\Memcached::OPT_BINARY_PROTOCOL, true); |
135
|
|
|
$servers = (!empty($this->config[ 'servers' ]) && is_array($this->config[ 'servers' ]) ? $this->config[ 'servers' ] : []); |
136
|
|
View Code Duplication |
if (count($servers) < 1) { |
137
|
|
|
$servers = [ |
138
|
|
|
[ |
139
|
|
|
'host' => '127.0.0.1', |
140
|
|
|
'path' => false, |
141
|
|
|
'port' => 11211, |
142
|
|
|
'sasl_user' => false, |
143
|
|
|
'sasl_password' => false, |
144
|
|
|
], |
145
|
|
|
]; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
foreach ($servers as $server) { |
149
|
|
|
try { |
150
|
|
|
/** |
151
|
|
|
* If path is provided we consider it as an UNIX Socket |
152
|
|
|
*/ |
153
|
|
View Code Duplication |
if(!empty($server[ 'path' ]) && !$this->instance->addServer($server[ 'path' ], 0)){ |
154
|
|
|
$this->fallback = true; |
155
|
|
|
}else if (!empty($server[ 'host' ]) && !$this->instance->addServer($server[ 'host' ], $server[ 'port' ])) { |
156
|
|
|
$this->fallback = true; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if (!empty($server[ 'sasl_user' ]) && !empty($server[ 'sasl_password' ])) { |
160
|
|
|
$this->instance->setSaslAuthData($server[ 'sasl_user' ], $server[ 'sasl_password' ]); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
} catch (\Exception $e) { |
164
|
|
|
$this->fallback = true; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Since Memcached does not throw |
170
|
|
|
* any error if not connected ... |
171
|
|
|
*/ |
172
|
|
|
$version = $this->instance->getVersion(); |
173
|
|
|
if(!$version || $this->instance->getResultCode() !== MemcachedSoftware::RES_SUCCESS){ |
|
|
|
|
174
|
|
|
throw new phpFastCacheDriverException('Memcached seems to not be connected'); |
175
|
|
|
} |
176
|
|
|
return true; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/******************** |
180
|
|
|
* |
181
|
|
|
* PSR-6 Extended Methods |
182
|
|
|
* |
183
|
|
|
*******************/ |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return DriverStatistic |
187
|
|
|
*/ |
188
|
|
View Code Duplication |
public function getStats() |
189
|
|
|
{ |
190
|
|
|
$stats = current($this->instance->getStats()); |
191
|
|
|
$stats[ 'uptime' ] = (isset($stats[ 'uptime' ]) ? $stats[ 'uptime' ] : 0); |
192
|
|
|
$stats[ 'version' ] = (isset($stats[ 'version' ]) ? $stats[ 'version' ] : $this->instance->getVersion()); |
193
|
|
|
$stats[ 'bytes' ] = (isset($stats[ 'bytes' ]) ? $stats[ 'version' ] : 0); |
194
|
|
|
|
195
|
|
|
$date = (new \DateTime())->setTimestamp(time() - $stats[ 'uptime' ]); |
196
|
|
|
|
197
|
|
|
return (new DriverStatistic()) |
198
|
|
|
->setData(implode(', ', array_keys($this->itemInstances))) |
199
|
|
|
->setInfo(sprintf("The memcache daemon v%s is up since %s.\n For more information see RawData.", $stats[ 'version' ], $date->format(DATE_RFC2822))) |
200
|
|
|
->setRawData($stats) |
201
|
|
|
->setSize($stats[ 'bytes' ]); |
202
|
|
|
} |
203
|
|
|
} |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.