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\Memcache; |
16
|
|
|
|
17
|
|
|
use Memcache as MemcacheSoftware; |
18
|
|
|
use phpFastCache\Core\DriverAbstract; |
19
|
|
|
use phpFastCache\Core\MemcacheDriverCollisionDetectorTrait; |
20
|
|
|
use phpFastCache\Entities\driverStatistic; |
21
|
|
|
use phpFastCache\Exceptions\phpFastCacheDriverCheckException; |
22
|
|
|
use phpFastCache\Exceptions\phpFastCacheDriverException; |
23
|
|
|
use Psr\Cache\CacheItemInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class Driver |
27
|
|
|
* @package phpFastCache\Drivers |
28
|
|
|
* @property MemcacheSoftware $instance |
29
|
|
|
*/ |
30
|
|
|
class Driver extends DriverAbstract |
31
|
|
|
{ |
32
|
|
|
use MemcacheDriverCollisionDetectorTrait; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
protected $memcacheFlags = 0; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Driver constructor. |
41
|
|
|
* @param array $config |
42
|
|
|
* @throws phpFastCacheDriverException |
43
|
|
|
*/ |
44
|
|
|
public function __construct(array $config = []) |
45
|
|
|
{ |
46
|
|
|
self::checkCollision('Memcache'); |
47
|
|
|
$this->setup($config); |
48
|
|
|
|
49
|
|
|
if (!$this->driverCheck()) { |
50
|
|
|
throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName())); |
51
|
|
|
} else { |
52
|
|
|
$this->driverConnect(); |
53
|
|
|
|
54
|
|
|
if (array_key_exists('compress_data', $config) && $config[ 'compress_data' ] === true) { |
55
|
|
|
$this->memcacheFlags = MEMCACHE_COMPRESSED; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
public function driverCheck() |
64
|
|
|
{ |
65
|
|
|
return class_exists('Memcache'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
70
|
|
|
* @return mixed |
71
|
|
|
* @throws \InvalidArgumentException |
72
|
|
|
*/ |
73
|
|
|
protected function driverWrite(CacheItemInterface $item) |
74
|
|
|
{ |
75
|
|
|
/** |
76
|
|
|
* Check for Cross-Driver type confusion |
77
|
|
|
*/ |
78
|
|
|
if ($item instanceof Item) { |
79
|
|
|
return $this->instance->set($item->getKey(), $this->driverPreWrap($item), $this->memcacheFlags, $item->getTtl()); |
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
|
|
|
$this->instance = new MemcacheSoftware(); |
131
|
|
|
|
132
|
|
|
$servers = (!empty($this->config[ 'servers' ]) && is_array($this->config[ 'servers' ]) ? $this->config[ 'servers' ] : []); |
133
|
|
|
if (count($servers) < 1) { |
134
|
|
|
$servers = [ |
135
|
|
|
[ |
136
|
|
|
'host' =>'127.0.0.1', |
137
|
|
|
'port' => 11211, |
138
|
|
|
'sasl_user' => false, |
139
|
|
|
'sasl_password' => false |
140
|
|
|
], |
141
|
|
|
]; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
foreach ($servers as $server) { |
145
|
|
|
try { |
146
|
|
|
if (!$this->instance->addServer($server['host'], $server['port'])) { |
147
|
|
|
$this->fallback = true; |
148
|
|
|
} |
149
|
|
|
if(!empty($server[ 'sasl_user' ]) && !empty($server[ 'sasl_password'])){ |
150
|
|
|
$this->instance->setSaslAuthData($server[ 'sasl_user' ], $server[ 'sasl_password']); |
151
|
|
|
} |
152
|
|
|
} catch (\Exception $e) { |
153
|
|
|
$this->fallback = true; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/******************** |
159
|
|
|
* |
160
|
|
|
* PSR-6 Extended Methods |
161
|
|
|
* |
162
|
|
|
*******************/ |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return driverStatistic |
166
|
|
|
*/ |
167
|
|
View Code Duplication |
public function getStats() |
168
|
|
|
{ |
169
|
|
|
$stats = (array) $this->instance->getstats(); |
170
|
|
|
$stats[ 'uptime' ] = (isset($stats[ 'uptime' ]) ? $stats[ 'uptime' ] : 0); |
171
|
|
|
$stats[ 'version' ] = (isset($stats[ 'version' ]) ? $stats[ 'version' ] : 'UnknownVersion'); |
172
|
|
|
$stats[ 'bytes' ] = (isset($stats[ 'bytes' ]) ? $stats[ 'version' ] : 0); |
173
|
|
|
|
174
|
|
|
$date = (new \DateTime())->setTimestamp(time() - $stats[ 'uptime' ]); |
175
|
|
|
|
176
|
|
|
return (new driverStatistic()) |
177
|
|
|
->setData(implode(', ', array_keys($this->itemInstances))) |
178
|
|
|
->setInfo(sprintf("The memcache daemon v%s is up since %s.\n For more information see RawData.", $stats[ 'version' ], $date->format(DATE_RFC2822))) |
179
|
|
|
->setRawData($stats) |
180
|
|
|
->setSize($stats[ 'bytes' ]); |
181
|
|
|
} |
182
|
|
|
} |