@@ -28,34 +28,34 @@ |
||
28 | 28 | */ |
29 | 29 | class Item implements ExtendedCacheItemInterface |
30 | 30 | { |
31 | - use ItemBaseTrait { |
|
32 | - ItemBaseTrait::__construct as __BaseConstruct; |
|
33 | - } |
|
34 | - |
|
35 | - /** |
|
36 | - * Item constructor. |
|
37 | - * @param Driver $driver |
|
38 | - * @param $key |
|
39 | - * @throws PhpfastcacheInvalidArgumentException |
|
40 | - */ |
|
41 | - public function __construct(CouchdbDriver $driver, $key) |
|
42 | - { |
|
43 | - $this->__BaseConstruct($driver, $key); |
|
44 | - } |
|
45 | - |
|
46 | - /** |
|
47 | - * @param ExtendedCacheItemPoolInterface $driver |
|
48 | - * @return static |
|
49 | - * @throws PhpfastcacheInvalidArgumentException |
|
50 | - */ |
|
51 | - public function setDriver(ExtendedCacheItemPoolInterface $driver) |
|
52 | - { |
|
53 | - if ($driver instanceof CouchdbDriver) { |
|
54 | - $this->driver = $driver; |
|
55 | - |
|
56 | - return $this; |
|
57 | - } |
|
58 | - |
|
59 | - throw new PhpfastcacheInvalidArgumentException('Invalid driver instance'); |
|
60 | - } |
|
31 | + use ItemBaseTrait { |
|
32 | + ItemBaseTrait::__construct as __BaseConstruct; |
|
33 | + } |
|
34 | + |
|
35 | + /** |
|
36 | + * Item constructor. |
|
37 | + * @param Driver $driver |
|
38 | + * @param $key |
|
39 | + * @throws PhpfastcacheInvalidArgumentException |
|
40 | + */ |
|
41 | + public function __construct(CouchdbDriver $driver, $key) |
|
42 | + { |
|
43 | + $this->__BaseConstruct($driver, $key); |
|
44 | + } |
|
45 | + |
|
46 | + /** |
|
47 | + * @param ExtendedCacheItemPoolInterface $driver |
|
48 | + * @return static |
|
49 | + * @throws PhpfastcacheInvalidArgumentException |
|
50 | + */ |
|
51 | + public function setDriver(ExtendedCacheItemPoolInterface $driver) |
|
52 | + { |
|
53 | + if ($driver instanceof CouchdbDriver) { |
|
54 | + $this->driver = $driver; |
|
55 | + |
|
56 | + return $this; |
|
57 | + } |
|
58 | + |
|
59 | + throw new PhpfastcacheInvalidArgumentException('Invalid driver instance'); |
|
60 | + } |
|
61 | 61 | } |
62 | 62 | \ No newline at end of file |
@@ -33,266 +33,266 @@ |
||
33 | 33 | */ |
34 | 34 | class Driver implements ExtendedCacheItemPoolInterface, AggregatablePoolInterface |
35 | 35 | { |
36 | - public const COUCHDB_DEFAULT_DB_NAME = 'phpfastcache'; // Public because used in config |
|
37 | - |
|
38 | - use DriverBaseTrait; |
|
39 | - |
|
40 | - /** |
|
41 | - * @return bool |
|
42 | - */ |
|
43 | - public function driverCheck(): bool |
|
44 | - { |
|
45 | - return class_exists(CouchDBClient::class); |
|
46 | - } |
|
47 | - |
|
48 | - /** |
|
49 | - * @return string |
|
50 | - */ |
|
51 | - public function getHelp(): string |
|
52 | - { |
|
53 | - return <<<HELP |
|
36 | + public const COUCHDB_DEFAULT_DB_NAME = 'phpfastcache'; // Public because used in config |
|
37 | + |
|
38 | + use DriverBaseTrait; |
|
39 | + |
|
40 | + /** |
|
41 | + * @return bool |
|
42 | + */ |
|
43 | + public function driverCheck(): bool |
|
44 | + { |
|
45 | + return class_exists(CouchDBClient::class); |
|
46 | + } |
|
47 | + |
|
48 | + /** |
|
49 | + * @return string |
|
50 | + */ |
|
51 | + public function getHelp(): string |
|
52 | + { |
|
53 | + return <<<HELP |
|
54 | 54 | <p> |
55 | 55 | To install the Couchdb HTTP client library via Composer: |
56 | 56 | <code>composer require "doctrine/couchdb" "@dev"</code> |
57 | 57 | </p> |
58 | 58 | HELP; |
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * @return DriverStatistic |
|
63 | - */ |
|
64 | - public function getStats(): DriverStatistic |
|
65 | - { |
|
66 | - $info = $this->instance->getDatabaseInfo(); |
|
67 | - |
|
68 | - return (new DriverStatistic()) |
|
69 | - ->setSize($info['sizes']['active'] ?? 0) |
|
70 | - ->setRawData($info) |
|
71 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
72 | - ->setInfo('Couchdb version ' . $this->instance->getVersion() . "\n For more information see RawData."); |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * @return bool |
|
77 | - * @throws PhpfastcacheLogicException |
|
78 | - */ |
|
79 | - protected function driverConnect(): bool |
|
80 | - { |
|
81 | - if ($this->instance instanceof CouchDBClient) { |
|
82 | - throw new PhpfastcacheLogicException('Already connected to Couchdb server'); |
|
83 | - } |
|
84 | - |
|
85 | - $clientConfig = $this->getConfig(); |
|
86 | - |
|
87 | - $url = ($clientConfig->isSsl() ? 'https://' : 'http://'); |
|
88 | - if ($clientConfig->getUsername()) { |
|
89 | - $url .= $clientConfig->getUsername(); |
|
90 | - if ($clientConfig->getPassword()) { |
|
91 | - $url .= ":{$clientConfig->getPassword()}"; |
|
92 | - } |
|
93 | - $url .= '@'; |
|
94 | - } |
|
95 | - $url .= $clientConfig->getHost(); |
|
96 | - $url .= ":{$clientConfig->getPort()}"; |
|
97 | - $url .= '/' . \urlencode($this->getDatabaseName()); |
|
98 | - |
|
99 | - $this->instance = CouchDBClient::create( |
|
100 | - [ |
|
101 | - 'dbname' => $this->getDatabaseName(), |
|
102 | - 'url' => $url, |
|
103 | - 'timeout' => $clientConfig->getTimeout(), |
|
104 | - ] |
|
105 | - ); |
|
106 | - |
|
107 | - $this->createDatabase(); |
|
108 | - |
|
109 | - return true; |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * @return string |
|
114 | - */ |
|
115 | - protected function getDatabaseName(): string |
|
116 | - { |
|
117 | - return $this->getConfig()->getDatabase() ?: self::COUCHDB_DEFAULT_DB_NAME; |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * @return void |
|
122 | - */ |
|
123 | - protected function createDatabase() |
|
124 | - { |
|
125 | - try{ |
|
126 | - $this->instance->getDatabaseInfo($this->getDatabaseName()); |
|
127 | - } catch(HTTPException $e){ |
|
128 | - $this->instance->createDatabase($this->getDatabaseName()); |
|
129 | - } |
|
130 | - } |
|
131 | - |
|
132 | - protected function getCouchDbItemKey(CacheItemInterface $item) |
|
133 | - { |
|
134 | - return 'pfc_' . $item->getEncodedKey(); |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * @param CacheItemInterface $item |
|
139 | - * @return null|array |
|
140 | - * @throws PhpfastcacheDriverException |
|
141 | - */ |
|
142 | - protected function driverRead(CacheItemInterface $item) |
|
143 | - { |
|
144 | - try { |
|
145 | - $response = $this->instance->findDocument($this->getCouchDbItemKey($item)); |
|
146 | - } catch (CouchDBException $e) { |
|
147 | - throw new PhpfastcacheDriverException('Got error while trying to get a document: ' . $e->getMessage(), 0, $e); |
|
148 | - } |
|
149 | - |
|
150 | - if ($response->status === 404 || empty($response->body[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX])) { |
|
151 | - return null; |
|
152 | - } |
|
153 | - |
|
154 | - if ($response->status === 200) { |
|
155 | - return $this->decode($response->body); |
|
156 | - } |
|
157 | - |
|
158 | - throw new PhpfastcacheDriverException('Got unexpected HTTP status: ' . $response->status); |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * @param CacheItemInterface $item |
|
163 | - * @return bool |
|
164 | - * @throws PhpfastcacheDriverException |
|
165 | - * @throws PhpfastcacheInvalidArgumentException |
|
166 | - */ |
|
167 | - protected function driverWrite(CacheItemInterface $item): bool |
|
168 | - { |
|
169 | - /** |
|
170 | - * Check for Cross-Driver type confusion |
|
171 | - */ |
|
172 | - if ($item instanceof Item) { |
|
173 | - try { |
|
174 | - $this->instance->putDocument( |
|
175 | - $this->encodeDocument($this->driverPreWrap($item)), |
|
176 | - $this->getCouchDbItemKey($item), |
|
177 | - $this->getLatestDocumentRevision($this->getCouchDbItemKey($item)) |
|
178 | - ); |
|
179 | - } catch (CouchDBException $e) { |
|
180 | - throw new PhpfastcacheDriverException('Got error while trying to upsert a document: ' . $e->getMessage(), 0, $e); |
|
181 | - } |
|
182 | - return true; |
|
183 | - } |
|
184 | - |
|
185 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * @return string|null |
|
190 | - */ |
|
191 | - protected function getLatestDocumentRevision($docId) |
|
192 | - { |
|
193 | - $path = '/' . \urlencode($this->getDatabaseName()) . '/' . urlencode($docId); |
|
194 | - |
|
195 | - $response = $this->instance->getHttpClient()->request( |
|
196 | - 'HEAD', |
|
197 | - $path, |
|
198 | - null, |
|
199 | - false |
|
200 | - ); |
|
201 | - if (!empty($response->headers['etag'])) { |
|
202 | - return trim($response->headers['etag'], " '\"\t\n\r\0\x0B"); |
|
203 | - } |
|
204 | - |
|
205 | - return null; |
|
206 | - } |
|
207 | - |
|
208 | - /******************** |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * @return DriverStatistic |
|
63 | + */ |
|
64 | + public function getStats(): DriverStatistic |
|
65 | + { |
|
66 | + $info = $this->instance->getDatabaseInfo(); |
|
67 | + |
|
68 | + return (new DriverStatistic()) |
|
69 | + ->setSize($info['sizes']['active'] ?? 0) |
|
70 | + ->setRawData($info) |
|
71 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
72 | + ->setInfo('Couchdb version ' . $this->instance->getVersion() . "\n For more information see RawData."); |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * @return bool |
|
77 | + * @throws PhpfastcacheLogicException |
|
78 | + */ |
|
79 | + protected function driverConnect(): bool |
|
80 | + { |
|
81 | + if ($this->instance instanceof CouchDBClient) { |
|
82 | + throw new PhpfastcacheLogicException('Already connected to Couchdb server'); |
|
83 | + } |
|
84 | + |
|
85 | + $clientConfig = $this->getConfig(); |
|
86 | + |
|
87 | + $url = ($clientConfig->isSsl() ? 'https://' : 'http://'); |
|
88 | + if ($clientConfig->getUsername()) { |
|
89 | + $url .= $clientConfig->getUsername(); |
|
90 | + if ($clientConfig->getPassword()) { |
|
91 | + $url .= ":{$clientConfig->getPassword()}"; |
|
92 | + } |
|
93 | + $url .= '@'; |
|
94 | + } |
|
95 | + $url .= $clientConfig->getHost(); |
|
96 | + $url .= ":{$clientConfig->getPort()}"; |
|
97 | + $url .= '/' . \urlencode($this->getDatabaseName()); |
|
98 | + |
|
99 | + $this->instance = CouchDBClient::create( |
|
100 | + [ |
|
101 | + 'dbname' => $this->getDatabaseName(), |
|
102 | + 'url' => $url, |
|
103 | + 'timeout' => $clientConfig->getTimeout(), |
|
104 | + ] |
|
105 | + ); |
|
106 | + |
|
107 | + $this->createDatabase(); |
|
108 | + |
|
109 | + return true; |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * @return string |
|
114 | + */ |
|
115 | + protected function getDatabaseName(): string |
|
116 | + { |
|
117 | + return $this->getConfig()->getDatabase() ?: self::COUCHDB_DEFAULT_DB_NAME; |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * @return void |
|
122 | + */ |
|
123 | + protected function createDatabase() |
|
124 | + { |
|
125 | + try{ |
|
126 | + $this->instance->getDatabaseInfo($this->getDatabaseName()); |
|
127 | + } catch(HTTPException $e){ |
|
128 | + $this->instance->createDatabase($this->getDatabaseName()); |
|
129 | + } |
|
130 | + } |
|
131 | + |
|
132 | + protected function getCouchDbItemKey(CacheItemInterface $item) |
|
133 | + { |
|
134 | + return 'pfc_' . $item->getEncodedKey(); |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * @param CacheItemInterface $item |
|
139 | + * @return null|array |
|
140 | + * @throws PhpfastcacheDriverException |
|
141 | + */ |
|
142 | + protected function driverRead(CacheItemInterface $item) |
|
143 | + { |
|
144 | + try { |
|
145 | + $response = $this->instance->findDocument($this->getCouchDbItemKey($item)); |
|
146 | + } catch (CouchDBException $e) { |
|
147 | + throw new PhpfastcacheDriverException('Got error while trying to get a document: ' . $e->getMessage(), 0, $e); |
|
148 | + } |
|
149 | + |
|
150 | + if ($response->status === 404 || empty($response->body[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX])) { |
|
151 | + return null; |
|
152 | + } |
|
153 | + |
|
154 | + if ($response->status === 200) { |
|
155 | + return $this->decode($response->body); |
|
156 | + } |
|
157 | + |
|
158 | + throw new PhpfastcacheDriverException('Got unexpected HTTP status: ' . $response->status); |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * @param CacheItemInterface $item |
|
163 | + * @return bool |
|
164 | + * @throws PhpfastcacheDriverException |
|
165 | + * @throws PhpfastcacheInvalidArgumentException |
|
166 | + */ |
|
167 | + protected function driverWrite(CacheItemInterface $item): bool |
|
168 | + { |
|
169 | + /** |
|
170 | + * Check for Cross-Driver type confusion |
|
171 | + */ |
|
172 | + if ($item instanceof Item) { |
|
173 | + try { |
|
174 | + $this->instance->putDocument( |
|
175 | + $this->encodeDocument($this->driverPreWrap($item)), |
|
176 | + $this->getCouchDbItemKey($item), |
|
177 | + $this->getLatestDocumentRevision($this->getCouchDbItemKey($item)) |
|
178 | + ); |
|
179 | + } catch (CouchDBException $e) { |
|
180 | + throw new PhpfastcacheDriverException('Got error while trying to upsert a document: ' . $e->getMessage(), 0, $e); |
|
181 | + } |
|
182 | + return true; |
|
183 | + } |
|
184 | + |
|
185 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * @return string|null |
|
190 | + */ |
|
191 | + protected function getLatestDocumentRevision($docId) |
|
192 | + { |
|
193 | + $path = '/' . \urlencode($this->getDatabaseName()) . '/' . urlencode($docId); |
|
194 | + |
|
195 | + $response = $this->instance->getHttpClient()->request( |
|
196 | + 'HEAD', |
|
197 | + $path, |
|
198 | + null, |
|
199 | + false |
|
200 | + ); |
|
201 | + if (!empty($response->headers['etag'])) { |
|
202 | + return trim($response->headers['etag'], " '\"\t\n\r\0\x0B"); |
|
203 | + } |
|
204 | + |
|
205 | + return null; |
|
206 | + } |
|
207 | + |
|
208 | + /******************** |
|
209 | 209 | * |
210 | 210 | * PSR-6 Extended Methods |
211 | 211 | * |
212 | 212 | *******************/ |
213 | 213 | |
214 | - /** |
|
215 | - * @param CacheItemInterface $item |
|
216 | - * @return bool |
|
217 | - * @throws PhpfastcacheDriverException |
|
218 | - * @throws PhpfastcacheInvalidArgumentException |
|
219 | - */ |
|
220 | - protected function driverDelete(CacheItemInterface $item): bool |
|
221 | - { |
|
222 | - /** |
|
223 | - * Check for Cross-Driver type confusion |
|
224 | - */ |
|
225 | - if ($item instanceof Item) { |
|
226 | - try { |
|
227 | - $this->instance->deleteDocument($this->getCouchDbItemKey($item), $this->getLatestDocumentRevision($this->getCouchDbItemKey($item))); |
|
228 | - } catch (CouchDBException $e) { |
|
229 | - throw new PhpfastcacheDriverException('Got error while trying to delete a document: ' . $e->getMessage(), 0, $e); |
|
230 | - } |
|
231 | - return true; |
|
232 | - } |
|
233 | - |
|
234 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
235 | - } |
|
236 | - |
|
237 | - /** |
|
238 | - * @return bool |
|
239 | - * @throws PhpfastcacheDriverException |
|
240 | - */ |
|
241 | - protected function driverClear(): bool |
|
242 | - { |
|
243 | - try { |
|
244 | - $this->instance->deleteDatabase($this->getDatabaseName()); |
|
245 | - $this->createDatabase(); |
|
246 | - } catch (CouchDBException $e) { |
|
247 | - throw new PhpfastcacheDriverException('Got error while trying to delete and recreate the database: ' . $e->getMessage(), 0, $e); |
|
248 | - } |
|
249 | - |
|
250 | - return true; |
|
251 | - } |
|
252 | - |
|
253 | - /** |
|
254 | - * @param array $data |
|
255 | - * @return array |
|
256 | - */ |
|
257 | - protected function encodeDocument(array $data): array |
|
258 | - { |
|
259 | - $data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX] = $this->encode($data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX]); |
|
260 | - |
|
261 | - return $data; |
|
262 | - } |
|
263 | - |
|
264 | - /** |
|
265 | - * Specific document decoder for Couchdb |
|
266 | - * since we dont store encoded version |
|
267 | - * for performance purposes |
|
268 | - * |
|
269 | - * @param $value |
|
270 | - * @return mixed |
|
271 | - * @throws \Exception |
|
272 | - */ |
|
273 | - protected function decode($value) |
|
274 | - { |
|
275 | - $value[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX] = \unserialize($value[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX], ['allowed_classes' => true]); |
|
276 | - |
|
277 | - $value[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] = new \DateTime( |
|
278 | - $value[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX]['date'], |
|
279 | - new \DateTimeZone($value[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX]['timezone']) |
|
280 | - ); |
|
281 | - |
|
282 | - if(isset($value[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX])){ |
|
283 | - $value[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = new \DateTime( |
|
284 | - $value[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX]['date'], |
|
285 | - new \DateTimeZone($value[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX]['timezone']) |
|
286 | - ); |
|
287 | - } |
|
288 | - |
|
289 | - if(isset($value[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX])){ |
|
290 | - $value[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = new \DateTime( |
|
291 | - $value[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX]['date'], |
|
292 | - new \DateTimeZone($value[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX]['timezone']) |
|
293 | - ); |
|
294 | - } |
|
295 | - |
|
296 | - return $value; |
|
297 | - } |
|
214 | + /** |
|
215 | + * @param CacheItemInterface $item |
|
216 | + * @return bool |
|
217 | + * @throws PhpfastcacheDriverException |
|
218 | + * @throws PhpfastcacheInvalidArgumentException |
|
219 | + */ |
|
220 | + protected function driverDelete(CacheItemInterface $item): bool |
|
221 | + { |
|
222 | + /** |
|
223 | + * Check for Cross-Driver type confusion |
|
224 | + */ |
|
225 | + if ($item instanceof Item) { |
|
226 | + try { |
|
227 | + $this->instance->deleteDocument($this->getCouchDbItemKey($item), $this->getLatestDocumentRevision($this->getCouchDbItemKey($item))); |
|
228 | + } catch (CouchDBException $e) { |
|
229 | + throw new PhpfastcacheDriverException('Got error while trying to delete a document: ' . $e->getMessage(), 0, $e); |
|
230 | + } |
|
231 | + return true; |
|
232 | + } |
|
233 | + |
|
234 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
235 | + } |
|
236 | + |
|
237 | + /** |
|
238 | + * @return bool |
|
239 | + * @throws PhpfastcacheDriverException |
|
240 | + */ |
|
241 | + protected function driverClear(): bool |
|
242 | + { |
|
243 | + try { |
|
244 | + $this->instance->deleteDatabase($this->getDatabaseName()); |
|
245 | + $this->createDatabase(); |
|
246 | + } catch (CouchDBException $e) { |
|
247 | + throw new PhpfastcacheDriverException('Got error while trying to delete and recreate the database: ' . $e->getMessage(), 0, $e); |
|
248 | + } |
|
249 | + |
|
250 | + return true; |
|
251 | + } |
|
252 | + |
|
253 | + /** |
|
254 | + * @param array $data |
|
255 | + * @return array |
|
256 | + */ |
|
257 | + protected function encodeDocument(array $data): array |
|
258 | + { |
|
259 | + $data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX] = $this->encode($data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX]); |
|
260 | + |
|
261 | + return $data; |
|
262 | + } |
|
263 | + |
|
264 | + /** |
|
265 | + * Specific document decoder for Couchdb |
|
266 | + * since we dont store encoded version |
|
267 | + * for performance purposes |
|
268 | + * |
|
269 | + * @param $value |
|
270 | + * @return mixed |
|
271 | + * @throws \Exception |
|
272 | + */ |
|
273 | + protected function decode($value) |
|
274 | + { |
|
275 | + $value[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX] = \unserialize($value[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX], ['allowed_classes' => true]); |
|
276 | + |
|
277 | + $value[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] = new \DateTime( |
|
278 | + $value[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX]['date'], |
|
279 | + new \DateTimeZone($value[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX]['timezone']) |
|
280 | + ); |
|
281 | + |
|
282 | + if(isset($value[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX])){ |
|
283 | + $value[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = new \DateTime( |
|
284 | + $value[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX]['date'], |
|
285 | + new \DateTimeZone($value[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX]['timezone']) |
|
286 | + ); |
|
287 | + } |
|
288 | + |
|
289 | + if(isset($value[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX])){ |
|
290 | + $value[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = new \DateTime( |
|
291 | + $value[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX]['date'], |
|
292 | + new \DateTimeZone($value[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX]['timezone']) |
|
293 | + ); |
|
294 | + } |
|
295 | + |
|
296 | + return $value; |
|
297 | + } |
|
298 | 298 | } |
@@ -20,5 +20,5 @@ |
||
20 | 20 | |
21 | 21 | class Config extends ConfigurationOption |
22 | 22 | { |
23 | - use IOConfigurationOptionTrait; |
|
23 | + use IOConfigurationOptionTrait; |
|
24 | 24 | } |
25 | 25 | \ No newline at end of file |
@@ -27,34 +27,34 @@ |
||
27 | 27 | */ |
28 | 28 | class Item implements ExtendedCacheItemInterface |
29 | 29 | { |
30 | - use ItemBaseTrait { |
|
31 | - ItemBaseTrait::__construct as __BaseConstruct; |
|
32 | - } |
|
33 | - |
|
34 | - /** |
|
35 | - * Item constructor. |
|
36 | - * @param Driver $driver |
|
37 | - * @param $key |
|
38 | - * @throws PhpfastcacheInvalidArgumentException |
|
39 | - */ |
|
40 | - public function __construct(FilesDriver $driver, $key) |
|
41 | - { |
|
42 | - $this->__BaseConstruct($driver, $key); |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * @param ExtendedCacheItemPoolInterface $driver |
|
47 | - * @return static |
|
48 | - * @throws PhpfastcacheInvalidArgumentException |
|
49 | - */ |
|
50 | - public function setDriver(ExtendedCacheItemPoolInterface $driver) |
|
51 | - { |
|
52 | - if ($driver instanceof FilesDriver) { |
|
53 | - $this->driver = $driver; |
|
54 | - |
|
55 | - return $this; |
|
56 | - } |
|
57 | - |
|
58 | - throw new PhpfastcacheInvalidArgumentException('Invalid driver instance'); |
|
59 | - } |
|
30 | + use ItemBaseTrait { |
|
31 | + ItemBaseTrait::__construct as __BaseConstruct; |
|
32 | + } |
|
33 | + |
|
34 | + /** |
|
35 | + * Item constructor. |
|
36 | + * @param Driver $driver |
|
37 | + * @param $key |
|
38 | + * @throws PhpfastcacheInvalidArgumentException |
|
39 | + */ |
|
40 | + public function __construct(FilesDriver $driver, $key) |
|
41 | + { |
|
42 | + $this->__BaseConstruct($driver, $key); |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * @param ExtendedCacheItemPoolInterface $driver |
|
47 | + * @return static |
|
48 | + * @throws PhpfastcacheInvalidArgumentException |
|
49 | + */ |
|
50 | + public function setDriver(ExtendedCacheItemPoolInterface $driver) |
|
51 | + { |
|
52 | + if ($driver instanceof FilesDriver) { |
|
53 | + $this->driver = $driver; |
|
54 | + |
|
55 | + return $this; |
|
56 | + } |
|
57 | + |
|
58 | + throw new PhpfastcacheInvalidArgumentException('Invalid driver instance'); |
|
59 | + } |
|
60 | 60 | } |
61 | 61 | \ No newline at end of file |
@@ -37,114 +37,114 @@ |
||
37 | 37 | */ |
38 | 38 | class Driver implements ExtendedCacheItemPoolInterface, AggregatablePoolInterface |
39 | 39 | { |
40 | - use IOHelperTrait; |
|
41 | - use DriverBaseTrait { |
|
42 | - DriverBaseTrait::__construct as private __parentConstruct; |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * Driver constructor. |
|
47 | - * @param Config $config |
|
48 | - * @param string $instanceId |
|
49 | - */ |
|
50 | - public function __construct(Config $config, string $instanceId) |
|
51 | - { |
|
52 | - $this->__parentConstruct($config, $instanceId); |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * @return bool |
|
57 | - */ |
|
58 | - public function driverCheck(): bool |
|
59 | - { |
|
60 | - return is_writable($this->getPath()) || mkdir($concurrentDirectory = $this->getPath(), $this->getDefaultChmod(), true) || is_dir($concurrentDirectory); |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * @return bool |
|
65 | - */ |
|
66 | - protected function driverConnect(): bool |
|
67 | - { |
|
68 | - return true; |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * @param CacheItemInterface $item |
|
73 | - * @return null|array |
|
74 | - */ |
|
75 | - protected function driverRead(CacheItemInterface $item) |
|
76 | - { |
|
77 | - $file_path = $this->getFilePath($item->getKey(), true); |
|
78 | - |
|
79 | - try{ |
|
80 | - $content = $this->readFile($file_path); |
|
81 | - }catch (PhpfastcacheIOException $e){ |
|
82 | - return null; |
|
83 | - } |
|
84 | - |
|
85 | - return $this->decode($content); |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * @param CacheItemInterface $item |
|
90 | - * @return bool |
|
91 | - * @throws PhpfastcacheInvalidArgumentException |
|
92 | - */ |
|
93 | - protected function driverWrite(CacheItemInterface $item): bool |
|
94 | - { |
|
95 | - /** |
|
96 | - * Check for Cross-Driver type confusion |
|
97 | - */ |
|
98 | - if ($item instanceof Item) { |
|
99 | - $file_path = $this->getFilePath($item->getKey()); |
|
100 | - $data = $this->encode($this->driverPreWrap($item)); |
|
101 | - |
|
102 | - /** |
|
103 | - * Force write |
|
104 | - */ |
|
105 | - try { |
|
106 | - return $this->writefile($file_path, $data, $this->getConfig()->isSecureFileManipulation()); |
|
107 | - } catch (Exception $e) { |
|
108 | - return false; |
|
109 | - } |
|
110 | - } |
|
111 | - |
|
112 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * @param CacheItemInterface $item |
|
117 | - * @return bool |
|
118 | - * @throws PhpfastcacheInvalidArgumentException |
|
119 | - */ |
|
120 | - protected function driverDelete(CacheItemInterface $item): bool |
|
121 | - { |
|
122 | - /** |
|
123 | - * Check for Cross-Driver type confusion |
|
124 | - */ |
|
125 | - if ($item instanceof Item) { |
|
126 | - $file_path = $this->getFilePath($item->getKey(), true); |
|
127 | - if (\file_exists($file_path) && @\unlink($file_path)) { |
|
128 | - \clearstatcache(true, $file_path); |
|
129 | - $dir = \dirname($file_path); |
|
130 | - if (!(new FilesystemIterator($dir))->valid()) { |
|
131 | - \rmdir($dir); |
|
132 | - } |
|
133 | - return true; |
|
134 | - } |
|
135 | - |
|
136 | - return false; |
|
137 | - } |
|
138 | - |
|
139 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * @return bool |
|
144 | - * @throws \Phpfastcache\Exceptions\PhpfastcacheIOException |
|
145 | - */ |
|
146 | - protected function driverClear(): bool |
|
147 | - { |
|
148 | - return Directory::rrmdir($this->getPath(true)); |
|
149 | - } |
|
40 | + use IOHelperTrait; |
|
41 | + use DriverBaseTrait { |
|
42 | + DriverBaseTrait::__construct as private __parentConstruct; |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * Driver constructor. |
|
47 | + * @param Config $config |
|
48 | + * @param string $instanceId |
|
49 | + */ |
|
50 | + public function __construct(Config $config, string $instanceId) |
|
51 | + { |
|
52 | + $this->__parentConstruct($config, $instanceId); |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * @return bool |
|
57 | + */ |
|
58 | + public function driverCheck(): bool |
|
59 | + { |
|
60 | + return is_writable($this->getPath()) || mkdir($concurrentDirectory = $this->getPath(), $this->getDefaultChmod(), true) || is_dir($concurrentDirectory); |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * @return bool |
|
65 | + */ |
|
66 | + protected function driverConnect(): bool |
|
67 | + { |
|
68 | + return true; |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * @param CacheItemInterface $item |
|
73 | + * @return null|array |
|
74 | + */ |
|
75 | + protected function driverRead(CacheItemInterface $item) |
|
76 | + { |
|
77 | + $file_path = $this->getFilePath($item->getKey(), true); |
|
78 | + |
|
79 | + try{ |
|
80 | + $content = $this->readFile($file_path); |
|
81 | + }catch (PhpfastcacheIOException $e){ |
|
82 | + return null; |
|
83 | + } |
|
84 | + |
|
85 | + return $this->decode($content); |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * @param CacheItemInterface $item |
|
90 | + * @return bool |
|
91 | + * @throws PhpfastcacheInvalidArgumentException |
|
92 | + */ |
|
93 | + protected function driverWrite(CacheItemInterface $item): bool |
|
94 | + { |
|
95 | + /** |
|
96 | + * Check for Cross-Driver type confusion |
|
97 | + */ |
|
98 | + if ($item instanceof Item) { |
|
99 | + $file_path = $this->getFilePath($item->getKey()); |
|
100 | + $data = $this->encode($this->driverPreWrap($item)); |
|
101 | + |
|
102 | + /** |
|
103 | + * Force write |
|
104 | + */ |
|
105 | + try { |
|
106 | + return $this->writefile($file_path, $data, $this->getConfig()->isSecureFileManipulation()); |
|
107 | + } catch (Exception $e) { |
|
108 | + return false; |
|
109 | + } |
|
110 | + } |
|
111 | + |
|
112 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * @param CacheItemInterface $item |
|
117 | + * @return bool |
|
118 | + * @throws PhpfastcacheInvalidArgumentException |
|
119 | + */ |
|
120 | + protected function driverDelete(CacheItemInterface $item): bool |
|
121 | + { |
|
122 | + /** |
|
123 | + * Check for Cross-Driver type confusion |
|
124 | + */ |
|
125 | + if ($item instanceof Item) { |
|
126 | + $file_path = $this->getFilePath($item->getKey(), true); |
|
127 | + if (\file_exists($file_path) && @\unlink($file_path)) { |
|
128 | + \clearstatcache(true, $file_path); |
|
129 | + $dir = \dirname($file_path); |
|
130 | + if (!(new FilesystemIterator($dir))->valid()) { |
|
131 | + \rmdir($dir); |
|
132 | + } |
|
133 | + return true; |
|
134 | + } |
|
135 | + |
|
136 | + return false; |
|
137 | + } |
|
138 | + |
|
139 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * @return bool |
|
144 | + * @throws \Phpfastcache\Exceptions\PhpfastcacheIOException |
|
145 | + */ |
|
146 | + protected function driverClear(): bool |
|
147 | + { |
|
148 | + return Directory::rrmdir($this->getPath(true)); |
|
149 | + } |
|
150 | 150 | } |
@@ -20,95 +20,95 @@ |
||
20 | 20 | |
21 | 21 | class Config extends ConfigurationOption |
22 | 22 | { |
23 | - /** |
|
24 | - * @var string |
|
25 | - */ |
|
26 | - protected $host = '127.0.0.1'; |
|
23 | + /** |
|
24 | + * @var string |
|
25 | + */ |
|
26 | + protected $host = '127.0.0.1'; |
|
27 | 27 | |
28 | - /** |
|
29 | - * @var int |
|
30 | - */ |
|
31 | - protected $port = 8888; |
|
28 | + /** |
|
29 | + * @var int |
|
30 | + */ |
|
31 | + protected $port = 8888; |
|
32 | 32 | |
33 | - /** |
|
34 | - * @var string |
|
35 | - */ |
|
36 | - protected $password = ''; |
|
33 | + /** |
|
34 | + * @var string |
|
35 | + */ |
|
36 | + protected $password = ''; |
|
37 | 37 | |
38 | - /** |
|
39 | - * @var int |
|
40 | - */ |
|
41 | - protected $timeout = 2000; |
|
38 | + /** |
|
39 | + * @var int |
|
40 | + */ |
|
41 | + protected $timeout = 2000; |
|
42 | 42 | |
43 | - /** |
|
44 | - * @return string |
|
45 | - */ |
|
46 | - public function getHost(): string |
|
47 | - { |
|
48 | - return $this->host; |
|
49 | - } |
|
43 | + /** |
|
44 | + * @return string |
|
45 | + */ |
|
46 | + public function getHost(): string |
|
47 | + { |
|
48 | + return $this->host; |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * @param string $host |
|
53 | - * @return Config |
|
54 | - */ |
|
55 | - public function setHost(string $host): Config |
|
56 | - { |
|
57 | - $this->host = $host; |
|
58 | - return $this; |
|
59 | - } |
|
51 | + /** |
|
52 | + * @param string $host |
|
53 | + * @return Config |
|
54 | + */ |
|
55 | + public function setHost(string $host): Config |
|
56 | + { |
|
57 | + $this->host = $host; |
|
58 | + return $this; |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * @return int |
|
63 | - */ |
|
64 | - public function getPort(): int |
|
65 | - { |
|
66 | - return $this->port; |
|
67 | - } |
|
61 | + /** |
|
62 | + * @return int |
|
63 | + */ |
|
64 | + public function getPort(): int |
|
65 | + { |
|
66 | + return $this->port; |
|
67 | + } |
|
68 | 68 | |
69 | - /** |
|
70 | - * @param int $port |
|
71 | - * @return Config |
|
72 | - */ |
|
73 | - public function setPort(int $port): Config |
|
74 | - { |
|
75 | - $this->port = $port; |
|
76 | - return $this; |
|
77 | - } |
|
69 | + /** |
|
70 | + * @param int $port |
|
71 | + * @return Config |
|
72 | + */ |
|
73 | + public function setPort(int $port): Config |
|
74 | + { |
|
75 | + $this->port = $port; |
|
76 | + return $this; |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * @return string |
|
81 | - */ |
|
82 | - public function getPassword(): string |
|
83 | - { |
|
84 | - return $this->password; |
|
85 | - } |
|
79 | + /** |
|
80 | + * @return string |
|
81 | + */ |
|
82 | + public function getPassword(): string |
|
83 | + { |
|
84 | + return $this->password; |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * @param string $password |
|
89 | - * @return Config |
|
90 | - */ |
|
91 | - public function setPassword(string $password): Config |
|
92 | - { |
|
93 | - $this->password = $password; |
|
94 | - return $this; |
|
95 | - } |
|
87 | + /** |
|
88 | + * @param string $password |
|
89 | + * @return Config |
|
90 | + */ |
|
91 | + public function setPassword(string $password): Config |
|
92 | + { |
|
93 | + $this->password = $password; |
|
94 | + return $this; |
|
95 | + } |
|
96 | 96 | |
97 | - /** |
|
98 | - * @return int |
|
99 | - */ |
|
100 | - public function getTimeout(): int |
|
101 | - { |
|
102 | - return $this->timeout; |
|
103 | - } |
|
97 | + /** |
|
98 | + * @return int |
|
99 | + */ |
|
100 | + public function getTimeout(): int |
|
101 | + { |
|
102 | + return $this->timeout; |
|
103 | + } |
|
104 | 104 | |
105 | - /** |
|
106 | - * @param int $timeout |
|
107 | - * @return Config |
|
108 | - */ |
|
109 | - public function setTimeout(int $timeout): Config |
|
110 | - { |
|
111 | - $this->timeout = $timeout; |
|
112 | - return $this; |
|
113 | - } |
|
105 | + /** |
|
106 | + * @param int $timeout |
|
107 | + * @return Config |
|
108 | + */ |
|
109 | + public function setTimeout(int $timeout): Config |
|
110 | + { |
|
111 | + $this->timeout = $timeout; |
|
112 | + return $this; |
|
113 | + } |
|
114 | 114 | } |
115 | 115 | \ No newline at end of file |
@@ -27,34 +27,34 @@ |
||
27 | 27 | */ |
28 | 28 | class Item implements ExtendedCacheItemInterface |
29 | 29 | { |
30 | - use ItemBaseTrait { |
|
31 | - ItemBaseTrait::__construct as __BaseConstruct; |
|
32 | - } |
|
33 | - |
|
34 | - /** |
|
35 | - * Item constructor. |
|
36 | - * @param Driver $driver |
|
37 | - * @param $key |
|
38 | - * @throws PhpfastcacheInvalidArgumentException |
|
39 | - */ |
|
40 | - public function __construct(SsdbDriver $driver, $key) |
|
41 | - { |
|
42 | - $this->__BaseConstruct($driver, $key); |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * @param ExtendedCacheItemPoolInterface $driver |
|
47 | - * @return static |
|
48 | - * @throws PhpfastcacheInvalidArgumentException |
|
49 | - */ |
|
50 | - public function setDriver(ExtendedCacheItemPoolInterface $driver) |
|
51 | - { |
|
52 | - if ($driver instanceof SsdbDriver) { |
|
53 | - $this->driver = $driver; |
|
54 | - |
|
55 | - return $this; |
|
56 | - } |
|
57 | - |
|
58 | - throw new PhpfastcacheInvalidArgumentException('Invalid driver instance'); |
|
59 | - } |
|
30 | + use ItemBaseTrait { |
|
31 | + ItemBaseTrait::__construct as __BaseConstruct; |
|
32 | + } |
|
33 | + |
|
34 | + /** |
|
35 | + * Item constructor. |
|
36 | + * @param Driver $driver |
|
37 | + * @param $key |
|
38 | + * @throws PhpfastcacheInvalidArgumentException |
|
39 | + */ |
|
40 | + public function __construct(SsdbDriver $driver, $key) |
|
41 | + { |
|
42 | + $this->__BaseConstruct($driver, $key); |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * @param ExtendedCacheItemPoolInterface $driver |
|
47 | + * @return static |
|
48 | + * @throws PhpfastcacheInvalidArgumentException |
|
49 | + */ |
|
50 | + public function setDriver(ExtendedCacheItemPoolInterface $driver) |
|
51 | + { |
|
52 | + if ($driver instanceof SsdbDriver) { |
|
53 | + $this->driver = $driver; |
|
54 | + |
|
55 | + return $this; |
|
56 | + } |
|
57 | + |
|
58 | + throw new PhpfastcacheInvalidArgumentException('Invalid driver instance'); |
|
59 | + } |
|
60 | 60 | } |
61 | 61 | \ No newline at end of file |
@@ -33,124 +33,124 @@ |
||
33 | 33 | */ |
34 | 34 | class Driver implements ExtendedCacheItemPoolInterface, AggregatablePoolInterface |
35 | 35 | { |
36 | - use DriverBaseTrait; |
|
37 | - |
|
38 | - /** |
|
39 | - * @return bool |
|
40 | - */ |
|
41 | - public function driverCheck(): bool |
|
42 | - { |
|
43 | - static $driverCheck; |
|
44 | - if ($driverCheck === null) { |
|
45 | - return ($driverCheck = class_exists('phpssdb\Core\SSDB')); |
|
46 | - } |
|
47 | - |
|
48 | - return $driverCheck; |
|
49 | - } |
|
50 | - |
|
51 | - /** |
|
52 | - * @return DriverStatistic |
|
53 | - */ |
|
54 | - public function getStats(): DriverStatistic |
|
55 | - { |
|
56 | - $stat = new DriverStatistic(); |
|
57 | - $info = $this->instance->info(); |
|
58 | - |
|
59 | - /** |
|
60 | - * Data returned by Ssdb are very poorly formatted |
|
61 | - * using hardcoded offset of pair key-value :-( |
|
62 | - */ |
|
63 | - $stat->setInfo(sprintf("Ssdb-server v%s with a total of %s call(s).\n For more information see RawData.", $info[2], $info[6])) |
|
64 | - ->setRawData($info) |
|
65 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
66 | - ->setSize($this->instance->dbsize()); |
|
67 | - |
|
68 | - return $stat; |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * @return bool |
|
73 | - * @throws PhpfastcacheDriverException |
|
74 | - */ |
|
75 | - protected function driverConnect(): bool |
|
76 | - { |
|
77 | - try { |
|
78 | - $clientConfig = $this->getConfig(); |
|
79 | - |
|
80 | - $this->instance = new SimpleSSDB($clientConfig->getHost(), $clientConfig->getPort(), $clientConfig->getTimeout()); |
|
81 | - if (!empty($clientConfig->getPassword())) { |
|
82 | - $this->instance->auth($clientConfig->getPassword()); |
|
83 | - } |
|
84 | - |
|
85 | - if (!$this->instance) { |
|
86 | - return false; |
|
87 | - } |
|
88 | - |
|
89 | - return true; |
|
90 | - } catch (SSDBException $e) { |
|
91 | - throw new PhpfastcacheDriverCheckException('Ssdb failed to connect with error: ' . $e->getMessage(), 0, $e); |
|
92 | - } |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * @param CacheItemInterface $item |
|
97 | - * @return null|array |
|
98 | - */ |
|
99 | - protected function driverRead(CacheItemInterface $item) |
|
100 | - { |
|
101 | - $val = $this->instance->get($item->getEncodedKey()); |
|
102 | - if ($val == false) { |
|
103 | - return null; |
|
104 | - } |
|
105 | - |
|
106 | - return $this->decode($val); |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * @param CacheItemInterface $item |
|
111 | - * @return mixed |
|
112 | - * @throws PhpfastcacheInvalidArgumentException |
|
113 | - */ |
|
114 | - protected function driverWrite(CacheItemInterface $item): bool |
|
115 | - { |
|
116 | - /** |
|
117 | - * Check for Cross-Driver type confusion |
|
118 | - */ |
|
119 | - if ($item instanceof Item) { |
|
120 | - return (bool)$this->instance->setx($item->getEncodedKey(), $this->encode($this->driverPreWrap($item)), $item->getTtl()); |
|
121 | - } |
|
122 | - |
|
123 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * @param CacheItemInterface $item |
|
128 | - * @return bool |
|
129 | - * @throws PhpfastcacheInvalidArgumentException |
|
130 | - */ |
|
131 | - protected function driverDelete(CacheItemInterface $item): bool |
|
132 | - { |
|
133 | - /** |
|
134 | - * Check for Cross-Driver type confusion |
|
135 | - */ |
|
136 | - if ($item instanceof Item) { |
|
137 | - return (bool)$this->instance->del($item->getEncodedKey()); |
|
138 | - } |
|
139 | - |
|
140 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
141 | - } |
|
142 | - |
|
143 | - /******************** |
|
36 | + use DriverBaseTrait; |
|
37 | + |
|
38 | + /** |
|
39 | + * @return bool |
|
40 | + */ |
|
41 | + public function driverCheck(): bool |
|
42 | + { |
|
43 | + static $driverCheck; |
|
44 | + if ($driverCheck === null) { |
|
45 | + return ($driverCheck = class_exists('phpssdb\Core\SSDB')); |
|
46 | + } |
|
47 | + |
|
48 | + return $driverCheck; |
|
49 | + } |
|
50 | + |
|
51 | + /** |
|
52 | + * @return DriverStatistic |
|
53 | + */ |
|
54 | + public function getStats(): DriverStatistic |
|
55 | + { |
|
56 | + $stat = new DriverStatistic(); |
|
57 | + $info = $this->instance->info(); |
|
58 | + |
|
59 | + /** |
|
60 | + * Data returned by Ssdb are very poorly formatted |
|
61 | + * using hardcoded offset of pair key-value :-( |
|
62 | + */ |
|
63 | + $stat->setInfo(sprintf("Ssdb-server v%s with a total of %s call(s).\n For more information see RawData.", $info[2], $info[6])) |
|
64 | + ->setRawData($info) |
|
65 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
66 | + ->setSize($this->instance->dbsize()); |
|
67 | + |
|
68 | + return $stat; |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * @return bool |
|
73 | + * @throws PhpfastcacheDriverException |
|
74 | + */ |
|
75 | + protected function driverConnect(): bool |
|
76 | + { |
|
77 | + try { |
|
78 | + $clientConfig = $this->getConfig(); |
|
79 | + |
|
80 | + $this->instance = new SimpleSSDB($clientConfig->getHost(), $clientConfig->getPort(), $clientConfig->getTimeout()); |
|
81 | + if (!empty($clientConfig->getPassword())) { |
|
82 | + $this->instance->auth($clientConfig->getPassword()); |
|
83 | + } |
|
84 | + |
|
85 | + if (!$this->instance) { |
|
86 | + return false; |
|
87 | + } |
|
88 | + |
|
89 | + return true; |
|
90 | + } catch (SSDBException $e) { |
|
91 | + throw new PhpfastcacheDriverCheckException('Ssdb failed to connect with error: ' . $e->getMessage(), 0, $e); |
|
92 | + } |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * @param CacheItemInterface $item |
|
97 | + * @return null|array |
|
98 | + */ |
|
99 | + protected function driverRead(CacheItemInterface $item) |
|
100 | + { |
|
101 | + $val = $this->instance->get($item->getEncodedKey()); |
|
102 | + if ($val == false) { |
|
103 | + return null; |
|
104 | + } |
|
105 | + |
|
106 | + return $this->decode($val); |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * @param CacheItemInterface $item |
|
111 | + * @return mixed |
|
112 | + * @throws PhpfastcacheInvalidArgumentException |
|
113 | + */ |
|
114 | + protected function driverWrite(CacheItemInterface $item): bool |
|
115 | + { |
|
116 | + /** |
|
117 | + * Check for Cross-Driver type confusion |
|
118 | + */ |
|
119 | + if ($item instanceof Item) { |
|
120 | + return (bool)$this->instance->setx($item->getEncodedKey(), $this->encode($this->driverPreWrap($item)), $item->getTtl()); |
|
121 | + } |
|
122 | + |
|
123 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * @param CacheItemInterface $item |
|
128 | + * @return bool |
|
129 | + * @throws PhpfastcacheInvalidArgumentException |
|
130 | + */ |
|
131 | + protected function driverDelete(CacheItemInterface $item): bool |
|
132 | + { |
|
133 | + /** |
|
134 | + * Check for Cross-Driver type confusion |
|
135 | + */ |
|
136 | + if ($item instanceof Item) { |
|
137 | + return (bool)$this->instance->del($item->getEncodedKey()); |
|
138 | + } |
|
139 | + |
|
140 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
141 | + } |
|
142 | + |
|
143 | + /******************** |
|
144 | 144 | * |
145 | 145 | * PSR-6 Extended Methods |
146 | 146 | * |
147 | 147 | *******************/ |
148 | 148 | |
149 | - /** |
|
150 | - * @return bool |
|
151 | - */ |
|
152 | - protected function driverClear(): bool |
|
153 | - { |
|
154 | - return (bool)$this->instance->flushdb('kv'); |
|
155 | - } |
|
149 | + /** |
|
150 | + * @return bool |
|
151 | + */ |
|
152 | + protected function driverClear(): bool |
|
153 | + { |
|
154 | + return (bool)$this->instance->flushdb('kv'); |
|
155 | + } |
|
156 | 156 | } |
@@ -20,158 +20,158 @@ |
||
20 | 20 | |
21 | 21 | class Config extends ConfigurationOption |
22 | 22 | { |
23 | - /** |
|
24 | - * @var string |
|
25 | - */ |
|
26 | - protected $host = '127.0.0.1'; |
|
27 | - /** |
|
28 | - * @var int |
|
29 | - */ |
|
30 | - protected $port = 9042; |
|
31 | - /** |
|
32 | - * @var int |
|
33 | - */ |
|
34 | - protected $timeout = 2; |
|
35 | - /** |
|
36 | - * @var string |
|
37 | - */ |
|
38 | - protected $username = ''; |
|
39 | - /** |
|
40 | - * @var string |
|
41 | - */ |
|
42 | - protected $password = ''; |
|
43 | - /** |
|
44 | - * @var bool |
|
45 | - */ |
|
46 | - protected $sslEnabled = false; |
|
47 | - /** |
|
48 | - * @var bool |
|
49 | - */ |
|
50 | - protected $sslVerify = false; |
|
51 | - |
|
52 | - /** |
|
53 | - * @return string |
|
54 | - */ |
|
55 | - public function getHost(): string |
|
56 | - { |
|
57 | - return $this->host; |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * @param string $host |
|
62 | - * @return self |
|
63 | - */ |
|
64 | - public function setHost(string $host): self |
|
65 | - { |
|
66 | - $this->host = $host; |
|
67 | - return $this; |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * @return int |
|
72 | - */ |
|
73 | - public function getPort(): int |
|
74 | - { |
|
75 | - return $this->port; |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * @param int $port |
|
80 | - * @return self |
|
81 | - */ |
|
82 | - public function setPort(int $port): self |
|
83 | - { |
|
84 | - $this->port = $port; |
|
85 | - return $this; |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * @return int |
|
90 | - */ |
|
91 | - public function getTimeout(): int |
|
92 | - { |
|
93 | - return $this->timeout; |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * @param int $timeout |
|
98 | - * @return self |
|
99 | - */ |
|
100 | - public function setTimeout(int $timeout): self |
|
101 | - { |
|
102 | - $this->timeout = $timeout; |
|
103 | - return $this; |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * @return string |
|
108 | - */ |
|
109 | - public function getUsername(): string |
|
110 | - { |
|
111 | - return $this->username; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * @param string $username |
|
116 | - * @return self |
|
117 | - */ |
|
118 | - public function setUsername(string $username): self |
|
119 | - { |
|
120 | - $this->username = $username; |
|
121 | - return $this; |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * @return string |
|
126 | - */ |
|
127 | - public function getPassword(): string |
|
128 | - { |
|
129 | - return $this->password; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * @param string $password |
|
134 | - * @return self |
|
135 | - */ |
|
136 | - public function setPassword(string $password): self |
|
137 | - { |
|
138 | - $this->password = $password; |
|
139 | - return $this; |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * @return bool |
|
144 | - */ |
|
145 | - public function isSslEnabled(): bool |
|
146 | - { |
|
147 | - return $this->sslEnabled; |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * @param bool $sslEnabled |
|
152 | - * @return self |
|
153 | - */ |
|
154 | - public function setSslEnabled(bool $sslEnabled): self |
|
155 | - { |
|
156 | - $this->sslEnabled = $sslEnabled; |
|
157 | - return $this; |
|
158 | - } |
|
159 | - |
|
160 | - /** |
|
161 | - * @return bool |
|
162 | - */ |
|
163 | - public function isSslVerify(): bool |
|
164 | - { |
|
165 | - return $this->sslVerify; |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * @param bool $sslVerify |
|
170 | - * @return self |
|
171 | - */ |
|
172 | - public function setSslVerify(bool $sslVerify): self |
|
173 | - { |
|
174 | - $this->sslVerify = $sslVerify; |
|
175 | - return $this; |
|
176 | - } |
|
23 | + /** |
|
24 | + * @var string |
|
25 | + */ |
|
26 | + protected $host = '127.0.0.1'; |
|
27 | + /** |
|
28 | + * @var int |
|
29 | + */ |
|
30 | + protected $port = 9042; |
|
31 | + /** |
|
32 | + * @var int |
|
33 | + */ |
|
34 | + protected $timeout = 2; |
|
35 | + /** |
|
36 | + * @var string |
|
37 | + */ |
|
38 | + protected $username = ''; |
|
39 | + /** |
|
40 | + * @var string |
|
41 | + */ |
|
42 | + protected $password = ''; |
|
43 | + /** |
|
44 | + * @var bool |
|
45 | + */ |
|
46 | + protected $sslEnabled = false; |
|
47 | + /** |
|
48 | + * @var bool |
|
49 | + */ |
|
50 | + protected $sslVerify = false; |
|
51 | + |
|
52 | + /** |
|
53 | + * @return string |
|
54 | + */ |
|
55 | + public function getHost(): string |
|
56 | + { |
|
57 | + return $this->host; |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * @param string $host |
|
62 | + * @return self |
|
63 | + */ |
|
64 | + public function setHost(string $host): self |
|
65 | + { |
|
66 | + $this->host = $host; |
|
67 | + return $this; |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * @return int |
|
72 | + */ |
|
73 | + public function getPort(): int |
|
74 | + { |
|
75 | + return $this->port; |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * @param int $port |
|
80 | + * @return self |
|
81 | + */ |
|
82 | + public function setPort(int $port): self |
|
83 | + { |
|
84 | + $this->port = $port; |
|
85 | + return $this; |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * @return int |
|
90 | + */ |
|
91 | + public function getTimeout(): int |
|
92 | + { |
|
93 | + return $this->timeout; |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * @param int $timeout |
|
98 | + * @return self |
|
99 | + */ |
|
100 | + public function setTimeout(int $timeout): self |
|
101 | + { |
|
102 | + $this->timeout = $timeout; |
|
103 | + return $this; |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * @return string |
|
108 | + */ |
|
109 | + public function getUsername(): string |
|
110 | + { |
|
111 | + return $this->username; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * @param string $username |
|
116 | + * @return self |
|
117 | + */ |
|
118 | + public function setUsername(string $username): self |
|
119 | + { |
|
120 | + $this->username = $username; |
|
121 | + return $this; |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * @return string |
|
126 | + */ |
|
127 | + public function getPassword(): string |
|
128 | + { |
|
129 | + return $this->password; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * @param string $password |
|
134 | + * @return self |
|
135 | + */ |
|
136 | + public function setPassword(string $password): self |
|
137 | + { |
|
138 | + $this->password = $password; |
|
139 | + return $this; |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * @return bool |
|
144 | + */ |
|
145 | + public function isSslEnabled(): bool |
|
146 | + { |
|
147 | + return $this->sslEnabled; |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * @param bool $sslEnabled |
|
152 | + * @return self |
|
153 | + */ |
|
154 | + public function setSslEnabled(bool $sslEnabled): self |
|
155 | + { |
|
156 | + $this->sslEnabled = $sslEnabled; |
|
157 | + return $this; |
|
158 | + } |
|
159 | + |
|
160 | + /** |
|
161 | + * @return bool |
|
162 | + */ |
|
163 | + public function isSslVerify(): bool |
|
164 | + { |
|
165 | + return $this->sslVerify; |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * @param bool $sslVerify |
|
170 | + * @return self |
|
171 | + */ |
|
172 | + public function setSslVerify(bool $sslVerify): self |
|
173 | + { |
|
174 | + $this->sslVerify = $sslVerify; |
|
175 | + return $this; |
|
176 | + } |
|
177 | 177 | } |
178 | 178 | \ No newline at end of file |