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
|
|
|
declare(strict_types=1); |
15
|
|
|
|
16
|
|
|
namespace Phpfastcache\Drivers\Cassandra; |
17
|
|
|
|
18
|
|
|
use Cassandra; |
|
|
|
|
19
|
|
|
use Cassandra\Session as CassandraSession; |
|
|
|
|
20
|
|
|
use Phpfastcache\Core\Pool\{DriverBaseTrait, ExtendedCacheItemPoolInterface}; |
21
|
|
|
use Phpfastcache\Entities\DriverStatistic; |
22
|
|
|
use Phpfastcache\Exceptions\{ |
23
|
|
|
phpFastCacheInvalidArgumentException, phpFastCacheLogicException |
24
|
|
|
}; |
25
|
|
|
use Phpfastcache\Util\ArrayObject; |
26
|
|
|
use Psr\Cache\CacheItemInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class Driver |
30
|
|
|
* @package phpFastCache\Drivers |
31
|
|
|
* @property CassandraSession $instance Instance of driver service |
32
|
|
|
* @property Config $config Config object |
33
|
|
|
* @method Config getConfig() Return the config object |
34
|
|
|
*/ |
35
|
|
|
class Driver implements ExtendedCacheItemPoolInterface |
36
|
|
|
{ |
37
|
|
|
const CASSANDRA_KEY_SPACE = 'phpfastcache'; |
38
|
|
|
const CASSANDRA_TABLE = 'cacheItems'; |
39
|
|
|
|
40
|
|
|
use DriverBaseTrait; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
|
|
public function driverCheck(): bool |
46
|
|
|
{ |
47
|
|
|
return \extension_loaded('Cassandra') && \class_exists(\Cassandra::class); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return bool |
52
|
|
|
* @throws phpFastCacheLogicException |
53
|
|
|
* @throws \Cassandra\Exception |
54
|
|
|
*/ |
55
|
|
|
protected function driverConnect(): bool |
56
|
|
|
{ |
57
|
|
|
if ($this->instance instanceof CassandraSession) { |
58
|
|
|
throw new phpFastCacheLogicException('Already connected to Couchbase server'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$clientConfig = $this->getConfig(); |
62
|
|
|
|
63
|
|
|
$clusterBuilder = Cassandra::cluster() |
64
|
|
|
->withContactPoints($clientConfig[ 'host' ]) |
65
|
|
|
->withPort($clientConfig[ 'port' ]); |
66
|
|
|
|
67
|
|
|
if (!empty($clientConfig[ 'sslEnabled' ])) { |
68
|
|
|
if (!empty($clientConfig[ 'sslVerify' ])) { |
69
|
|
|
$sslBuilder = Cassandra::ssl()->withVerifyFlags(Cassandra::VERIFY_PEER_CERT); |
70
|
|
|
} else { |
71
|
|
|
$sslBuilder = Cassandra::ssl()->withVerifyFlags(Cassandra::VERIFY_NONE); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$clusterBuilder->withSSL($sslBuilder->build()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$clusterBuilder->withConnectTimeout($clientConfig[ 'timeout' ]); |
78
|
|
|
|
79
|
|
|
if ($clientConfig[ 'username' ]) { |
80
|
|
|
$clusterBuilder->withCredentials($clientConfig[ 'username' ], $clientConfig[ 'password' ]); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$this->instance = $clusterBuilder->build()->connect(); |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* In case of emergency: |
87
|
|
|
* $this->instance->execute( |
88
|
|
|
* new Cassandra\SimpleStatement(\sprintf("DROP KEYSPACE %s;", self::CASSANDRA_KEY_SPACE)) |
89
|
|
|
* ); |
90
|
|
|
*/ |
91
|
|
|
|
92
|
|
|
$this->instance->execute(new Cassandra\SimpleStatement(\sprintf( |
|
|
|
|
93
|
|
|
"CREATE KEYSPACE IF NOT EXISTS %s WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };", |
94
|
|
|
self::CASSANDRA_KEY_SPACE |
95
|
|
|
))); |
96
|
|
|
$this->instance->execute(new Cassandra\SimpleStatement(\sprintf('USE %s;', self::CASSANDRA_KEY_SPACE))); |
97
|
|
|
$this->instance->execute(new Cassandra\SimpleStatement(\sprintf(' |
98
|
|
|
CREATE TABLE IF NOT EXISTS %s ( |
99
|
|
|
cache_uuid uuid, |
100
|
|
|
cache_id varchar, |
101
|
|
|
cache_data text, |
102
|
|
|
cache_creation_date timestamp, |
103
|
|
|
cache_expiration_date timestamp, |
104
|
|
|
cache_length int, |
105
|
|
|
PRIMARY KEY (cache_id) |
106
|
|
|
);', self::CASSANDRA_TABLE |
107
|
|
|
))); |
108
|
|
|
|
109
|
|
|
return true; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
114
|
|
|
* @return null|array |
115
|
|
|
*/ |
116
|
|
|
protected function driverRead(CacheItemInterface $item) |
117
|
|
|
{ |
118
|
|
|
try { |
119
|
|
|
$options = new Cassandra\ExecutionOptions([ |
|
|
|
|
120
|
|
|
'arguments' => ['cache_id' => $item->getKey()], |
121
|
|
|
'page_size' => 1, |
122
|
|
|
]); |
123
|
|
|
$query =\sprintf( |
124
|
|
|
'SELECT cache_data FROM %s.%s WHERE cache_id = :cache_id;', |
125
|
|
|
self::CASSANDRA_KEY_SPACE, |
126
|
|
|
self::CASSANDRA_TABLE |
127
|
|
|
); |
128
|
|
|
$results = $this->instance->execute(new Cassandra\SimpleStatement($query), $options); |
129
|
|
|
|
130
|
|
|
if ($results instanceof Cassandra\Rows && $results->count() === 1) { |
|
|
|
|
131
|
|
|
return $this->decode($results->first()[ 'cache_data' ]); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return null; |
135
|
|
|
} catch (Cassandra\Exception $e) { |
|
|
|
|
136
|
|
|
return null; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
142
|
|
|
* @return bool |
143
|
|
|
* @throws phpFastCacheInvalidArgumentException |
144
|
|
|
*/ |
145
|
|
|
protected function driverWrite(CacheItemInterface $item): bool |
146
|
|
|
{ |
147
|
|
|
/** |
148
|
|
|
* Check for Cross-Driver type confusion |
149
|
|
|
*/ |
150
|
|
|
if ($item instanceof Item) { |
151
|
|
|
try { |
152
|
|
|
$cacheData = $this->encode($this->driverPreWrap($item)); |
153
|
|
|
$options = new Cassandra\ExecutionOptions([ |
154
|
|
|
'arguments' => [ |
155
|
|
|
'cache_uuid' => new Cassandra\Uuid(), |
|
|
|
|
156
|
|
|
'cache_id' => $item->getKey(), |
157
|
|
|
'cache_data' => $cacheData, |
158
|
|
|
'cache_creation_date' => new Cassandra\Timestamp((new \DateTime())->getTimestamp()), |
|
|
|
|
159
|
|
|
'cache_expiration_date' => new Cassandra\Timestamp($item->getExpirationDate()->getTimestamp()), |
160
|
|
|
'cache_length' => strlen($cacheData), |
161
|
|
|
], |
162
|
|
|
'consistency' => Cassandra::CONSISTENCY_ALL, |
163
|
|
|
'serial_consistency' => Cassandra::CONSISTENCY_SERIAL, |
164
|
|
|
]); |
165
|
|
|
|
166
|
|
|
$query =\sprintf('INSERT INTO %s.%s |
167
|
|
|
( |
168
|
|
|
cache_uuid, |
169
|
|
|
cache_id, |
170
|
|
|
cache_data, |
171
|
|
|
cache_creation_date, |
172
|
|
|
cache_expiration_date, |
173
|
|
|
cache_length |
174
|
|
|
) |
175
|
|
|
VALUES (:cache_uuid, :cache_id, :cache_data, :cache_creation_date, :cache_expiration_date, :cache_length); |
176
|
|
|
', self::CASSANDRA_KEY_SPACE, self::CASSANDRA_TABLE); |
177
|
|
|
|
178
|
|
|
$result = $this->instance->execute(new Cassandra\SimpleStatement($query), $options); |
179
|
|
|
/** |
180
|
|
|
* There's no real way atm |
181
|
|
|
* to know if the item has |
182
|
|
|
* been really upserted |
183
|
|
|
*/ |
184
|
|
|
return $result instanceof Cassandra\Rows; |
185
|
|
|
} catch (\Cassandra\Exception\InvalidArgumentException $e) { |
|
|
|
|
186
|
|
|
throw new phpFastCacheInvalidArgumentException($e, 0, $e); |
187
|
|
|
} |
188
|
|
|
} else { |
189
|
|
|
throw new phpFastCacheInvalidArgumentException('Cross-Driver type confusion detected'); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
195
|
|
|
* @return bool |
196
|
|
|
* @throws phpFastCacheInvalidArgumentException |
197
|
|
|
*/ |
198
|
|
|
protected function driverDelete(CacheItemInterface $item): bool |
199
|
|
|
{ |
200
|
|
|
/** |
201
|
|
|
* Check for Cross-Driver type confusion |
202
|
|
|
*/ |
203
|
|
|
if ($item instanceof Item) { |
204
|
|
|
try { |
205
|
|
|
$options = new Cassandra\ExecutionOptions([ |
206
|
|
|
'arguments' => [ |
207
|
|
|
'cache_id' => $item->getKey(), |
208
|
|
|
], |
209
|
|
|
]); |
210
|
|
|
$result = $this->instance->execute(new Cassandra\SimpleStatement(\sprintf( |
211
|
|
|
'DELETE FROM %s.%s WHERE cache_id = :cache_id;', |
212
|
|
|
self::CASSANDRA_KEY_SPACE, |
213
|
|
|
self::CASSANDRA_TABLE |
214
|
|
|
)), $options); |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* There's no real way atm |
218
|
|
|
* to know if the item has |
219
|
|
|
* been really deleted |
220
|
|
|
*/ |
221
|
|
|
return $result instanceof Cassandra\Rows; |
222
|
|
|
} catch (Cassandra\Exception $e) { |
223
|
|
|
return false; |
224
|
|
|
} |
225
|
|
|
} else { |
226
|
|
|
throw new phpFastCacheInvalidArgumentException('Cross-Driver type confusion detected'); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @return bool |
232
|
|
|
*/ |
233
|
|
|
protected function driverClear(): bool |
234
|
|
|
{ |
235
|
|
|
try { |
236
|
|
|
$this->instance->execute(new Cassandra\SimpleStatement(\sprintf( |
237
|
|
|
'TRUNCATE %s.%s;', |
238
|
|
|
self::CASSANDRA_KEY_SPACE, self::CASSANDRA_TABLE |
239
|
|
|
))); |
240
|
|
|
|
241
|
|
|
return true; |
242
|
|
|
} catch (Cassandra\Exception $e) { |
243
|
|
|
return false; |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/******************** |
248
|
|
|
* |
249
|
|
|
* PSR-6 Extended Methods |
250
|
|
|
* |
251
|
|
|
*******************/ |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @return string |
255
|
|
|
*/ |
256
|
|
|
public function getHelp(): string |
257
|
|
|
{ |
258
|
|
|
return <<<HELP |
259
|
|
|
<p> |
260
|
|
|
To install the php Cassandra extension via Pecl: |
261
|
|
|
<code>sudo pecl install cassandra</code> |
262
|
|
|
More information on: https://github.com/datastax/php-driver |
263
|
|
|
Please not that this repository only provide php stubs and C/C++ sources, it does NOT provide php client. |
264
|
|
|
</p> |
265
|
|
|
HELP; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @return DriverStatistic |
270
|
|
|
* @throws \Cassandra\Exception |
271
|
|
|
*/ |
272
|
|
|
public function getStats(): DriverStatistic |
273
|
|
|
{ |
274
|
|
|
$result = $this->instance->execute(new Cassandra\SimpleStatement(\sprintf( |
275
|
|
|
'SELECT SUM(cache_length) as cache_size FROM %s.%s', |
276
|
|
|
self::CASSANDRA_KEY_SPACE, |
277
|
|
|
self::CASSANDRA_TABLE |
278
|
|
|
))); |
279
|
|
|
|
280
|
|
|
return (new DriverStatistic()) |
281
|
|
|
->setSize($result->first()[ 'cache_size' ]) |
282
|
|
|
->setRawData([]) |
283
|
|
|
->setData(\implode(', ', \array_keys($this->itemInstances))) |
284
|
|
|
->setInfo('The cache size represents only the cache data itself without counting data structures associated to the cache entries.'); |
285
|
|
|
} |
286
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths