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\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 MemcacheSoftware $instance |
31
|
|
|
*/ |
32
|
|
|
class Driver implements ExtendedCacheItemPoolInterface |
33
|
|
|
{ |
34
|
|
|
use DriverBaseTrait, MemcacheDriverCollisionDetectorTrait; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
protected $memcacheFlags = 0; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Driver constructor. |
43
|
|
|
* @param array $config |
44
|
|
|
* @throws phpFastCacheDriverException |
45
|
|
|
*/ |
46
|
|
|
public function __construct(array $config = []) |
47
|
|
|
{ |
48
|
|
|
self::checkCollision('Memcache'); |
49
|
|
|
$this->setup($config); |
50
|
|
|
|
51
|
|
|
if (!$this->driverCheck()) { |
52
|
|
|
throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName())); |
53
|
|
|
} else { |
54
|
|
|
$this->driverConnect(); |
55
|
|
|
|
56
|
|
|
if (array_key_exists('compress_data', $config) && $config[ 'compress_data' ] === true) { |
57
|
|
|
$this->memcacheFlags = MEMCACHE_COMPRESSED; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
|
|
public function driverCheck() |
66
|
|
|
{ |
67
|
|
|
return class_exists('Memcache'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
72
|
|
|
* @return mixed |
73
|
|
|
* @throws phpFastCacheInvalidArgumentException |
74
|
|
|
*/ |
75
|
|
|
protected function driverWrite(CacheItemInterface $item) |
76
|
|
|
{ |
77
|
|
|
/** |
78
|
|
|
* Check for Cross-Driver type confusion |
79
|
|
|
*/ |
80
|
|
|
if ($item instanceof Item) { |
81
|
|
|
return $this->instance->set($item->getKey(), $this->driverPreWrap($item), $this->memcacheFlags, $item->getTtl()); |
82
|
|
|
} else { |
83
|
|
|
throw new phpFastCacheInvalidArgumentException('Cross-Driver type confusion detected'); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
89
|
|
|
* @return mixed |
90
|
|
|
*/ |
91
|
|
View Code Duplication |
protected function driverRead(CacheItemInterface $item) |
92
|
|
|
{ |
93
|
|
|
$val = $this->instance->get($item->getKey()); |
94
|
|
|
|
95
|
|
|
if ($val === false) { |
96
|
|
|
return null; |
97
|
|
|
} else { |
98
|
|
|
return $val; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
104
|
|
|
* @return bool |
105
|
|
|
* @throws phpFastCacheInvalidArgumentException |
106
|
|
|
*/ |
107
|
|
|
protected function driverDelete(CacheItemInterface $item) |
108
|
|
|
{ |
109
|
|
|
/** |
110
|
|
|
* Check for Cross-Driver type confusion |
111
|
|
|
*/ |
112
|
|
|
if ($item instanceof Item) { |
113
|
|
|
return $this->instance->delete($item->getKey()); |
114
|
|
|
} else { |
115
|
|
|
throw new phpFastCacheInvalidArgumentException('Cross-Driver type confusion detected'); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
|
|
protected function driverClear() |
123
|
|
|
{ |
124
|
|
|
return $this->instance->flush(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return bool |
129
|
|
|
*/ |
130
|
|
View Code Duplication |
protected function driverConnect() |
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
|
|
|
} |