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\Util\MemcacheDriverCollisionDetectorTrait; |
24
|
|
|
use Psr\Cache\CacheItemInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class Driver |
28
|
|
|
* @package phpFastCache\Drivers |
29
|
|
|
*/ |
30
|
|
|
class Driver implements ExtendedCacheItemPoolInterface |
31
|
|
|
{ |
32
|
|
|
use DriverBaseTrait, MemcacheDriverCollisionDetectorTrait; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Driver constructor. |
36
|
|
|
* @param array $config |
37
|
|
|
* @throws phpFastCacheDriverException |
38
|
|
|
*/ |
39
|
|
View Code Duplication |
public function __construct(array $config = []) |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
self::checkCollision('Memcached'); |
42
|
|
|
$this->setup($config); |
43
|
|
|
|
44
|
|
|
if (!$this->driverCheck()) { |
45
|
|
|
throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName())); |
46
|
|
|
} else { |
47
|
|
|
$this->instance = new MemcachedSoftware(); |
48
|
|
|
$this->driverConnect(); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
|
|
public function driverCheck() |
56
|
|
|
{ |
57
|
|
|
return class_exists('Memcached'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
62
|
|
|
* @return mixed |
63
|
|
|
* @throws \InvalidArgumentException |
64
|
|
|
*/ |
65
|
|
|
protected function driverWrite(CacheItemInterface $item) |
66
|
|
|
{ |
67
|
|
|
/** |
68
|
|
|
* Check for Cross-Driver type confusion |
69
|
|
|
*/ |
70
|
|
|
if ($item instanceof Item) { |
71
|
|
|
$ttl = $item->getExpirationDate()->getTimestamp() - time(); |
72
|
|
|
|
73
|
|
|
// Memcache will only allow a expiration timer less than 2592000 seconds, |
74
|
|
|
// otherwise, it will assume you're giving it a UNIX timestamp. |
75
|
|
|
if ($ttl > 2592000) { |
76
|
|
|
$ttl = time() + $ttl; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this->instance->set($item->getKey(), $this->driverPreWrap($item), $ttl); |
80
|
|
|
} else { |
81
|
|
|
throw new \InvalidArgumentException('Cross-Driver type confusion detected'); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
87
|
|
|
* @return mixed |
88
|
|
|
*/ |
89
|
|
View Code Duplication |
protected function driverRead(CacheItemInterface $item) |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
$val = $this->instance->get($item->getKey()); |
92
|
|
|
|
93
|
|
|
if ($val === false) { |
94
|
|
|
return null; |
95
|
|
|
} else { |
96
|
|
|
return $val; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
102
|
|
|
* @return bool |
103
|
|
|
* @throws \InvalidArgumentException |
104
|
|
|
*/ |
105
|
|
|
protected function driverDelete(CacheItemInterface $item) |
106
|
|
|
{ |
107
|
|
|
/** |
108
|
|
|
* Check for Cross-Driver type confusion |
109
|
|
|
*/ |
110
|
|
|
if ($item instanceof Item) { |
111
|
|
|
return $this->instance->delete($item->getKey()); |
112
|
|
|
} else { |
113
|
|
|
throw new \InvalidArgumentException('Cross-Driver type confusion detected'); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
|
|
protected function driverClear() |
121
|
|
|
{ |
122
|
|
|
return $this->instance->flush(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
|
View Code Duplication |
protected function driverConnect() |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
$servers = (!empty($this->config[ 'memcache' ]) && is_array($this->config[ 'memcache' ]) ? $this->config[ 'memcache' ] : []); |
131
|
|
|
if (count($servers) < 1) { |
132
|
|
|
$servers = [ |
133
|
|
|
['127.0.0.1', 11211], |
134
|
|
|
]; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
foreach ($servers as $server) { |
138
|
|
|
try { |
139
|
|
|
if (!$this->instance->addServer($server[ 0 ], $server[ 1 ])) { |
140
|
|
|
$this->fallback = true; |
141
|
|
|
} |
142
|
|
|
if(!empty($server[ 'sasl_user' ]) && !empty($server[ 'sasl_password'])){ |
143
|
|
|
$this->instance->setSaslAuthData($server[ 'sasl_user' ], $server[ 'sasl_password']); |
144
|
|
|
} |
145
|
|
|
} catch (\Exception $e) { |
146
|
|
|
$this->fallback = true; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/******************** |
152
|
|
|
* |
153
|
|
|
* PSR-6 Extended Methods |
154
|
|
|
* |
155
|
|
|
*******************/ |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return driverStatistic |
159
|
|
|
*/ |
160
|
|
|
public function getStats() |
161
|
|
|
{ |
162
|
|
|
$stats = (array) $this->instance->getStats(); |
163
|
|
|
$date = (new \DateTime())->setTimestamp(time() - $stats[ 'uptime' ]); |
164
|
|
|
|
165
|
|
|
return (new driverStatistic()) |
166
|
|
|
->setData(implode(', ', array_keys($this->itemInstances))) |
167
|
|
|
->setInfo(sprintf("The memcache daemon v%s is up since %s.\n For more information see RawData.", $stats[ 'version' ], $date->format(DATE_RFC2822))) |
168
|
|
|
->setRawData($stats) |
169
|
|
|
->setSize($stats[ 'bytes' ]); |
170
|
|
|
} |
171
|
|
|
} |
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.