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\Mongodb; |
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 Psr\Cache\CacheItemInterface; |
23
|
|
|
use MongoClient as MongodbClient; |
24
|
|
|
use MongoBinData; |
25
|
|
|
use MongoCollection; |
26
|
|
|
use MongoCursorException; |
27
|
|
|
use MongoDate; |
28
|
|
|
use MongoConnectionException; |
29
|
|
|
use LogicException; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class Driver |
33
|
|
|
* @package phpFastCache\Drivers |
34
|
|
|
*/ |
35
|
|
|
class Driver extends DriverAbstract |
36
|
|
|
{ |
37
|
|
|
use StandardPsr6StructureTrait; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var MongodbClient |
41
|
|
|
*/ |
42
|
|
|
public $instance; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Driver constructor. |
46
|
|
|
* @param array $config |
47
|
|
|
* @throws phpFastCacheDriverException |
48
|
|
|
*/ |
49
|
|
|
public function __construct(array $config = []) |
50
|
|
|
{ |
51
|
|
|
$this->setup($config); |
52
|
|
|
|
53
|
|
|
if (!$this->driverCheck()) { |
54
|
|
|
throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName())); |
55
|
|
|
} else { |
56
|
|
|
$this->driverConnect(); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
public function driverCheck() |
64
|
|
|
{ |
65
|
|
|
return extension_loaded('Mongodb'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
70
|
|
|
* @return mixed |
71
|
|
|
* @throws \InvalidArgumentException |
72
|
|
|
*/ |
73
|
|
|
public function driverWrite(CacheItemInterface $item) |
74
|
|
|
{ |
75
|
|
|
/** |
76
|
|
|
* Check for Cross-Driver type confusion |
77
|
|
|
*/ |
78
|
|
|
if ($item instanceof Item) { |
79
|
|
|
try { |
80
|
|
|
$result = (array) $this->getCollection()->update( |
81
|
|
|
['_id' => $item->getKey()], |
82
|
|
|
[ |
83
|
|
|
'$set' => [ |
84
|
|
|
self::DRIVER_TIME_WRAPPER_INDEX => ($item->getTtl() > 0 ? new MongoDate(time() + $item->getTtl()) : new MongoDate(time())), |
85
|
|
|
self::DRIVER_DATA_WRAPPER_INDEX => new MongoBinData($this->encode($item->get()), MongoBinData::BYTE_ARRAY), |
86
|
|
|
self::DRIVER_TAGS_WRAPPER_INDEX => new MongoBinData($this->encode($item->getTags()), MongoBinData::BYTE_ARRAY), |
87
|
|
|
], |
88
|
|
|
], |
89
|
|
|
['upsert' => true, 'multiple' => false] |
90
|
|
|
); |
91
|
|
|
} catch (MongoCursorException $e) { |
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return isset($result[ 'ok' ]) ? $result[ 'ok' ] == 1 : true; |
96
|
|
|
} else { |
97
|
|
|
throw new \InvalidArgumentException('Cross-Driver type confusion detected'); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string $key |
103
|
|
|
* @return mixed |
104
|
|
|
*/ |
105
|
|
|
public function driverRead($key) |
106
|
|
|
{ |
107
|
|
|
$document = $this->getCollection()->findOne(['_id' => $key], [self::DRIVER_DATA_WRAPPER_INDEX, self::DRIVER_TIME_WRAPPER_INDEX, self::DRIVER_TAGS_WRAPPER_INDEX /*'d', 'e'*/]); |
108
|
|
|
|
109
|
|
|
if ($document) { |
|
|
|
|
110
|
|
|
return [ |
111
|
|
|
self::DRIVER_DATA_WRAPPER_INDEX => $this->decode($document[ self::DRIVER_DATA_WRAPPER_INDEX ]->bin), |
112
|
|
|
self::DRIVER_TIME_WRAPPER_INDEX => (new \DateTime())->setTimestamp($document[ self::DRIVER_TIME_WRAPPER_INDEX ]->sec), |
113
|
|
|
self::DRIVER_TAGS_WRAPPER_INDEX => $this->decode($document[ self::DRIVER_TAGS_WRAPPER_INDEX ]->bin), |
114
|
|
|
]; |
115
|
|
|
} else { |
116
|
|
|
return null; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
122
|
|
|
* @return bool |
123
|
|
|
* @throws \InvalidArgumentException |
124
|
|
|
*/ |
125
|
|
|
public function driverDelete(CacheItemInterface $item) |
126
|
|
|
{ |
127
|
|
|
/** |
128
|
|
|
* Check for Cross-Driver type confusion |
129
|
|
|
*/ |
130
|
|
|
if ($item instanceof Item) { |
131
|
|
|
$deletionResult = (array) $this->getCollection()->remove(['_id' => $item->getKey()], ["w" => 1]); |
132
|
|
|
|
133
|
|
|
return (int) $deletionResult[ 'ok' ] === 1 && !$deletionResult[ 'err' ]; |
134
|
|
|
} else { |
135
|
|
|
throw new \InvalidArgumentException('Cross-Driver type confusion detected'); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return bool |
141
|
|
|
*/ |
142
|
|
|
public function driverClear() |
143
|
|
|
{ |
144
|
|
|
return $this->getCollection()->drop(); |
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return bool |
149
|
|
|
* @throws MongoConnectionException |
150
|
|
|
* @throws LogicException |
151
|
|
|
*/ |
152
|
|
|
public function driverConnect() |
153
|
|
|
{ |
154
|
|
|
if ($this->instance instanceof MongodbClient) { |
155
|
|
|
throw new LogicException('Already connected to Mongodb server'); |
156
|
|
|
} else { |
157
|
|
|
$host = isset($this->config[ 'host' ]) ? $this->config[ 'host' ] : '127.0.0.1'; |
158
|
|
|
$port = isset($server[ 'port' ]) ? $server[ 'port' ] : '27017'; |
|
|
|
|
159
|
|
|
$timeout = isset($server[ 'timeout' ]) ? $server[ 'timeout' ] : 3; |
160
|
|
|
$password = isset($this->config[ 'password' ]) ? $this->config[ 'password' ] : ''; |
161
|
|
|
$username = isset($this->config[ 'username' ]) ? $this->config[ 'username' ] : ''; |
162
|
|
|
|
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @todo make an url builder |
166
|
|
|
*/ |
167
|
|
|
$this->instance = $this->instance ?: (new MongodbClient('mongodb://' . |
168
|
|
|
($username ?: '') . |
169
|
|
|
($password ? ":{$password}" : '') . |
170
|
|
|
($username ? '@' : '') . "{$host}" . |
171
|
|
|
($port != '27017' ? ":{$port}" : ''), ['connectTimeoutMS' => $timeout * 1000]))->phpFastCache; |
172
|
|
|
// $this->instance->Cache->createIndex([self::DRIVER_TIME_WRAPPER_INDEX => 1], ['expireAfterSeconds' => 0]); |
|
|
|
|
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return \MongoCollection |
179
|
|
|
*/ |
180
|
|
|
protected function getCollection() |
181
|
|
|
{ |
182
|
|
|
return $this->instance->Cache; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
187
|
|
|
* @return bool |
188
|
|
|
* @throws \InvalidArgumentException |
189
|
|
|
*/ |
190
|
|
|
public function driverIsHit(CacheItemInterface $item) |
191
|
|
|
{ |
192
|
|
|
$document = $this->getCollection()->findOne(['_id' => $item->getKey()], [self::DRIVER_TIME_WRAPPER_INDEX /*'d', 'e'*/]); |
193
|
|
|
if ($document) { |
|
|
|
|
194
|
|
|
return $document[ self::DRIVER_TIME_WRAPPER_INDEX ]->sec >= time(); |
195
|
|
|
} else { |
196
|
|
|
return null; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/******************** |
201
|
|
|
* |
202
|
|
|
* PSR-6 Extended Methods |
203
|
|
|
* |
204
|
|
|
*******************/ |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return driverStatistic |
208
|
|
|
*/ |
209
|
|
|
public function getStats() |
210
|
|
|
{ |
211
|
|
|
$serverStatus = $this->getCollection()->db->command([ |
212
|
|
|
'serverStatus' => 1, |
213
|
|
|
'recordStats' => 0, |
214
|
|
|
'repl' => 0, |
215
|
|
|
'metrics' => 0, |
216
|
|
|
]); |
217
|
|
|
|
218
|
|
|
$collStats = $this->getCollection()->db->command([ |
219
|
|
|
'collStats' => 'Cache', |
220
|
|
|
'verbose' => true, |
221
|
|
|
]); |
222
|
|
|
|
223
|
|
|
$stats = (new driverStatistic()) |
224
|
|
|
->setInfo('MongoDB version ' . $serverStatus[ 'version' ] . ', Uptime (in days): ' . round($serverStatus[ 'uptime' ] / 86400, 1) . "\n For more information see RawData.") |
225
|
|
|
->setSize((int) $collStats[ 'size' ]) |
226
|
|
|
->setData(implode(', ', array_keys($this->itemInstances))) |
227
|
|
|
->setRawData([ |
228
|
|
|
'serverStatus' => $serverStatus, |
229
|
|
|
'collStats' => $collStats, |
230
|
|
|
]); |
231
|
|
|
|
232
|
|
|
return $stats; |
233
|
|
|
} |
234
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.