@@ -29,89 +29,89 @@ |
||
29 | 29 | */ |
30 | 30 | class Driver implements AggregatablePoolInterface |
31 | 31 | { |
32 | - use TaggableCacheItemPoolTrait; |
|
33 | - |
|
34 | - /** |
|
35 | - * @return bool |
|
36 | - */ |
|
37 | - public function driverCheck(): bool |
|
38 | - { |
|
39 | - return extension_loaded('Zend Data Cache') && function_exists('zend_shm_cache_store'); |
|
40 | - } |
|
41 | - |
|
42 | - /** |
|
43 | - * @return string |
|
44 | - */ |
|
45 | - public function getHelp(): string |
|
46 | - { |
|
47 | - return <<<HELP |
|
32 | + use TaggableCacheItemPoolTrait; |
|
33 | + |
|
34 | + /** |
|
35 | + * @return bool |
|
36 | + */ |
|
37 | + public function driverCheck(): bool |
|
38 | + { |
|
39 | + return extension_loaded('Zend Data Cache') && function_exists('zend_shm_cache_store'); |
|
40 | + } |
|
41 | + |
|
42 | + /** |
|
43 | + * @return string |
|
44 | + */ |
|
45 | + public function getHelp(): string |
|
46 | + { |
|
47 | + return <<<HELP |
|
48 | 48 | <p> |
49 | 49 | This driver rely on Zend Server 8.5+, see: https://www.zend.com/products/zend-server |
50 | 50 | </p> |
51 | 51 | HELP; |
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * @return DriverStatistic |
|
56 | - */ |
|
57 | - public function getStats(): DriverStatistic |
|
58 | - { |
|
59 | - $stats = (array)zend_shm_cache_info(); |
|
60 | - return (new DriverStatistic()) |
|
61 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
62 | - ->setInfo(sprintf("The Zend memory have %d item(s) in cache.\n For more information see RawData.", $stats['items_total'])) |
|
63 | - ->setRawData($stats) |
|
64 | - ->setSize($stats['memory_total']); |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * @return bool |
|
69 | - */ |
|
70 | - protected function driverConnect(): bool |
|
71 | - { |
|
72 | - return true; |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * @param ExtendedCacheItemInterface $item |
|
77 | - * @return ?array<string, mixed> |
|
78 | - */ |
|
79 | - protected function driverRead(ExtendedCacheItemInterface $item): ?array |
|
80 | - { |
|
81 | - $data = zend_shm_cache_fetch($item->getKey()); |
|
82 | - |
|
83 | - if (empty($data) || !\is_array($data)) { |
|
84 | - return null; |
|
85 | - } |
|
86 | - |
|
87 | - return $data; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * @param ExtendedCacheItemInterface $item |
|
92 | - * @return mixed |
|
93 | - * @throws PhpfastcacheInvalidArgumentException |
|
94 | - */ |
|
95 | - protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
96 | - { |
|
97 | - |
|
98 | - $ttl = $item->getExpirationDate()->getTimestamp() - time(); |
|
99 | - |
|
100 | - return zend_shm_cache_store($item->getKey(), $this->driverPreWrap($item), ($ttl > 0 ? $ttl : 0)); |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * @param string $key |
|
105 | - * @param string $encodedKey |
|
106 | - * @return bool |
|
107 | - */ |
|
108 | - protected function driverDelete(string $key, string $encodedKey): bool |
|
109 | - { |
|
110 | - return (bool)zend_shm_cache_delete($key); |
|
111 | - } |
|
112 | - |
|
113 | - protected function driverClear(): bool |
|
114 | - { |
|
115 | - return @zend_shm_cache_clear(); |
|
116 | - } |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * @return DriverStatistic |
|
56 | + */ |
|
57 | + public function getStats(): DriverStatistic |
|
58 | + { |
|
59 | + $stats = (array)zend_shm_cache_info(); |
|
60 | + return (new DriverStatistic()) |
|
61 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
62 | + ->setInfo(sprintf("The Zend memory have %d item(s) in cache.\n For more information see RawData.", $stats['items_total'])) |
|
63 | + ->setRawData($stats) |
|
64 | + ->setSize($stats['memory_total']); |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * @return bool |
|
69 | + */ |
|
70 | + protected function driverConnect(): bool |
|
71 | + { |
|
72 | + return true; |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * @param ExtendedCacheItemInterface $item |
|
77 | + * @return ?array<string, mixed> |
|
78 | + */ |
|
79 | + protected function driverRead(ExtendedCacheItemInterface $item): ?array |
|
80 | + { |
|
81 | + $data = zend_shm_cache_fetch($item->getKey()); |
|
82 | + |
|
83 | + if (empty($data) || !\is_array($data)) { |
|
84 | + return null; |
|
85 | + } |
|
86 | + |
|
87 | + return $data; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * @param ExtendedCacheItemInterface $item |
|
92 | + * @return mixed |
|
93 | + * @throws PhpfastcacheInvalidArgumentException |
|
94 | + */ |
|
95 | + protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
96 | + { |
|
97 | + |
|
98 | + $ttl = $item->getExpirationDate()->getTimestamp() - time(); |
|
99 | + |
|
100 | + return zend_shm_cache_store($item->getKey(), $this->driverPreWrap($item), ($ttl > 0 ? $ttl : 0)); |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * @param string $key |
|
105 | + * @param string $encodedKey |
|
106 | + * @return bool |
|
107 | + */ |
|
108 | + protected function driverDelete(string $key, string $encodedKey): bool |
|
109 | + { |
|
110 | + return (bool)zend_shm_cache_delete($key); |
|
111 | + } |
|
112 | + |
|
113 | + protected function driverClear(): bool |
|
114 | + { |
|
115 | + return @zend_shm_cache_clear(); |
|
116 | + } |
|
117 | 117 | } |
@@ -56,7 +56,7 @@ discard block |
||
56 | 56 | */ |
57 | 57 | public function getStats(): DriverStatistic |
58 | 58 | { |
59 | - $stats = (array)zend_shm_cache_info(); |
|
59 | + $stats = (array) zend_shm_cache_info(); |
|
60 | 60 | return (new DriverStatistic()) |
61 | 61 | ->setData(implode(', ', array_keys($this->itemInstances))) |
62 | 62 | ->setInfo(sprintf("The Zend memory have %d item(s) in cache.\n For more information see RawData.", $stats['items_total'])) |
@@ -107,7 +107,7 @@ discard block |
||
107 | 107 | */ |
108 | 108 | protected function driverDelete(string $key, string $encodedKey): bool |
109 | 109 | { |
110 | - return (bool)zend_shm_cache_delete($key); |
|
110 | + return (bool) zend_shm_cache_delete($key); |
|
111 | 111 | } |
112 | 112 | |
113 | 113 | protected function driverClear(): bool |
@@ -35,201 +35,201 @@ |
||
35 | 35 | */ |
36 | 36 | class Driver implements AggregatablePoolInterface |
37 | 37 | { |
38 | - use TaggableCacheItemPoolTrait; |
|
39 | - |
|
40 | - /** |
|
41 | - * @return bool |
|
42 | - */ |
|
43 | - public function driverCheck(): bool |
|
44 | - { |
|
45 | - if (extension_loaded('Redis')) { |
|
46 | - trigger_error('The native Redis extension is installed, you should use Redis instead of Predis to increase performances', E_USER_NOTICE); |
|
47 | - } |
|
48 | - |
|
49 | - return class_exists(\Predis\Client::class); |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * @return string |
|
54 | - */ |
|
55 | - public function getHelp(): string |
|
56 | - { |
|
57 | - return <<<HELP |
|
38 | + use TaggableCacheItemPoolTrait; |
|
39 | + |
|
40 | + /** |
|
41 | + * @return bool |
|
42 | + */ |
|
43 | + public function driverCheck(): bool |
|
44 | + { |
|
45 | + if (extension_loaded('Redis')) { |
|
46 | + trigger_error('The native Redis extension is installed, you should use Redis instead of Predis to increase performances', E_USER_NOTICE); |
|
47 | + } |
|
48 | + |
|
49 | + return class_exists(\Predis\Client::class); |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * @return string |
|
54 | + */ |
|
55 | + public function getHelp(): string |
|
56 | + { |
|
57 | + return <<<HELP |
|
58 | 58 | <p> |
59 | 59 | To install the Predis library via Composer: |
60 | 60 | <code>composer require "predis/predis" "~1.1.0"</code> |
61 | 61 | </p> |
62 | 62 | HELP; |
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * @return DriverStatistic |
|
67 | - */ |
|
68 | - public function getStats(): DriverStatistic |
|
69 | - { |
|
70 | - $info = $this->instance->info(); |
|
71 | - $size = ($info['Memory']['used_memory'] ?? 0); |
|
72 | - $version = ($info['Server']['redis_version'] ?? 0); |
|
73 | - $date = (isset($info['Server']['uptime_in_seconds']) ? (new DateTime())->setTimestamp(time() - $info['Server']['uptime_in_seconds']) : 'unknown date'); |
|
74 | - |
|
75 | - return (new DriverStatistic()) |
|
76 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
77 | - ->setRawData($info) |
|
78 | - ->setSize((int)$size) |
|
79 | - ->setInfo( |
|
80 | - sprintf( |
|
81 | - "The Redis daemon v%s (with Predis v%s) is up since %s.\n For more information see RawData. \n Driver size includes the memory allocation size.", |
|
82 | - $version, |
|
83 | - PredisClient::VERSION, |
|
84 | - $date->format(DATE_RFC2822) |
|
85 | - ) |
|
86 | - ); |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * @return bool |
|
91 | - * @throws PhpfastcacheDriverException |
|
92 | - * @throws PhpfastcacheLogicException |
|
93 | - */ |
|
94 | - protected function driverConnect(): bool |
|
95 | - { |
|
96 | - /** |
|
97 | - * In case of a user-provided |
|
98 | - * Predis client just return here |
|
99 | - */ |
|
100 | - if ($this->getConfig()->getPredisClient() instanceof PredisClient) { |
|
101 | - $this->instance = $this->getConfig()->getPredisClient(); |
|
102 | - if (!$this->instance->isConnected()) { |
|
103 | - $this->instance->connect(); |
|
104 | - } |
|
105 | - return true; |
|
106 | - } |
|
107 | - |
|
108 | - $options = []; |
|
109 | - |
|
110 | - if ($this->getConfig()->getOptPrefix()) { |
|
111 | - $options['prefix'] = $this->getConfig()->getOptPrefix(); |
|
112 | - } |
|
113 | - |
|
114 | - if (!empty($this->getConfig()->getPath())) { |
|
115 | - $this->instance = new PredisClient( |
|
116 | - [ |
|
117 | - 'scheme' => $this->getConfig()->getScheme(), |
|
118 | - 'persistent' => $this->getConfig()->isPersistent(), |
|
119 | - 'timeout' => $this->getConfig()->getTimeout(), |
|
120 | - 'path' => $this->getConfig()->getPath(), |
|
121 | - ], |
|
122 | - $options |
|
123 | - ); |
|
124 | - } else { |
|
125 | - $this->instance = new PredisClient($this->getConfig()->getPredisConfigArray(), $options); |
|
126 | - } |
|
127 | - |
|
128 | - try { |
|
129 | - $this->instance->connect(); |
|
130 | - } catch (PredisConnectionException $e) { |
|
131 | - throw new PhpfastcacheDriverException( |
|
132 | - 'Failed to connect to predis server. Check the Predis documentation: https://github.com/nrk/predis/tree/v1.1#how-to-install-and-use-predis', |
|
133 | - 0, |
|
134 | - $e |
|
135 | - ); |
|
136 | - } |
|
137 | - |
|
138 | - return true; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * @param ExtendedCacheItemInterface $item |
|
143 | - * @return ?array<string, mixed> |
|
144 | - */ |
|
145 | - protected function driverRead(ExtendedCacheItemInterface $item): ?array |
|
146 | - { |
|
147 | - $val = $this->instance->get($item->getKey()); |
|
148 | - |
|
149 | - if ($val === null) { |
|
150 | - return null; |
|
151 | - } |
|
152 | - |
|
153 | - return $this->decode($val); |
|
154 | - } |
|
155 | - |
|
156 | - |
|
157 | - /** |
|
158 | - * @param ExtendedCacheItemInterface ...$items |
|
159 | - * @return array<array<string, mixed>> |
|
160 | - * @throws \Phpfastcache\Exceptions\PhpfastcacheDriverException |
|
161 | - * @throws \RedisException |
|
162 | - */ |
|
163 | - protected function driverReadMultiple(ExtendedCacheItemInterface ...$items): array |
|
164 | - { |
|
165 | - $keys = $this->getKeys($items); |
|
166 | - |
|
167 | - return array_combine($keys, array_map( |
|
168 | - fn($val) => $val ? $this->decode($val) : null, |
|
169 | - $this->instance->mget($keys) |
|
170 | - )); |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * @return array<int, string> |
|
175 | - * @throws \RedisException |
|
176 | - */ |
|
177 | - protected function driverReadAllKeys(string $pattern = '*'): iterable |
|
178 | - { |
|
179 | - $keys = []; |
|
180 | - foreach (new PredisIterator\Keyspace($this->instance, $pattern, ExtendedCacheItemPoolInterface::MAX_ALL_KEYS_COUNT) as $key) { |
|
181 | - $keys[] = $key; |
|
182 | - } |
|
183 | - return $keys; |
|
184 | - } |
|
185 | - |
|
186 | - |
|
187 | - /** |
|
188 | - * @param ExtendedCacheItemInterface $item |
|
189 | - * @return mixed |
|
190 | - * @throws PhpfastcacheInvalidArgumentException |
|
191 | - * @throws PhpfastcacheLogicException |
|
192 | - */ |
|
193 | - protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
194 | - { |
|
195 | - |
|
196 | - $ttl = $item->getExpirationDate()->getTimestamp() - time(); |
|
197 | - |
|
198 | - /** |
|
199 | - * @see https://redis.io/commands/setex |
|
200 | - * @see https://redis.io/commands/expire |
|
201 | - */ |
|
202 | - if ($ttl <= 0) { |
|
203 | - return (bool)$this->instance->expire($item->getKey(), 0); |
|
204 | - } |
|
205 | - |
|
206 | - return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item)))->getPayload() === 'OK'; |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * @param string $key |
|
211 | - * @param string $encodedKey |
|
212 | - * @return bool |
|
213 | - */ |
|
214 | - protected function driverDelete(string $key, string $encodedKey): bool |
|
215 | - { |
|
216 | - return (bool)$this->instance->del([$key]); |
|
217 | - } |
|
218 | - |
|
219 | - /** |
|
220 | - * @param string[] $keys |
|
221 | - * @return bool |
|
222 | - */ |
|
223 | - protected function driverDeleteMultiple(array $keys): bool |
|
224 | - { |
|
225 | - return (bool) $this->instance->del(...$keys); |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * @return bool |
|
230 | - */ |
|
231 | - protected function driverClear(): bool |
|
232 | - { |
|
233 | - return $this->instance->flushdb()->getPayload() === 'OK'; |
|
234 | - } |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * @return DriverStatistic |
|
67 | + */ |
|
68 | + public function getStats(): DriverStatistic |
|
69 | + { |
|
70 | + $info = $this->instance->info(); |
|
71 | + $size = ($info['Memory']['used_memory'] ?? 0); |
|
72 | + $version = ($info['Server']['redis_version'] ?? 0); |
|
73 | + $date = (isset($info['Server']['uptime_in_seconds']) ? (new DateTime())->setTimestamp(time() - $info['Server']['uptime_in_seconds']) : 'unknown date'); |
|
74 | + |
|
75 | + return (new DriverStatistic()) |
|
76 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
77 | + ->setRawData($info) |
|
78 | + ->setSize((int)$size) |
|
79 | + ->setInfo( |
|
80 | + sprintf( |
|
81 | + "The Redis daemon v%s (with Predis v%s) is up since %s.\n For more information see RawData. \n Driver size includes the memory allocation size.", |
|
82 | + $version, |
|
83 | + PredisClient::VERSION, |
|
84 | + $date->format(DATE_RFC2822) |
|
85 | + ) |
|
86 | + ); |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * @return bool |
|
91 | + * @throws PhpfastcacheDriverException |
|
92 | + * @throws PhpfastcacheLogicException |
|
93 | + */ |
|
94 | + protected function driverConnect(): bool |
|
95 | + { |
|
96 | + /** |
|
97 | + * In case of a user-provided |
|
98 | + * Predis client just return here |
|
99 | + */ |
|
100 | + if ($this->getConfig()->getPredisClient() instanceof PredisClient) { |
|
101 | + $this->instance = $this->getConfig()->getPredisClient(); |
|
102 | + if (!$this->instance->isConnected()) { |
|
103 | + $this->instance->connect(); |
|
104 | + } |
|
105 | + return true; |
|
106 | + } |
|
107 | + |
|
108 | + $options = []; |
|
109 | + |
|
110 | + if ($this->getConfig()->getOptPrefix()) { |
|
111 | + $options['prefix'] = $this->getConfig()->getOptPrefix(); |
|
112 | + } |
|
113 | + |
|
114 | + if (!empty($this->getConfig()->getPath())) { |
|
115 | + $this->instance = new PredisClient( |
|
116 | + [ |
|
117 | + 'scheme' => $this->getConfig()->getScheme(), |
|
118 | + 'persistent' => $this->getConfig()->isPersistent(), |
|
119 | + 'timeout' => $this->getConfig()->getTimeout(), |
|
120 | + 'path' => $this->getConfig()->getPath(), |
|
121 | + ], |
|
122 | + $options |
|
123 | + ); |
|
124 | + } else { |
|
125 | + $this->instance = new PredisClient($this->getConfig()->getPredisConfigArray(), $options); |
|
126 | + } |
|
127 | + |
|
128 | + try { |
|
129 | + $this->instance->connect(); |
|
130 | + } catch (PredisConnectionException $e) { |
|
131 | + throw new PhpfastcacheDriverException( |
|
132 | + 'Failed to connect to predis server. Check the Predis documentation: https://github.com/nrk/predis/tree/v1.1#how-to-install-and-use-predis', |
|
133 | + 0, |
|
134 | + $e |
|
135 | + ); |
|
136 | + } |
|
137 | + |
|
138 | + return true; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * @param ExtendedCacheItemInterface $item |
|
143 | + * @return ?array<string, mixed> |
|
144 | + */ |
|
145 | + protected function driverRead(ExtendedCacheItemInterface $item): ?array |
|
146 | + { |
|
147 | + $val = $this->instance->get($item->getKey()); |
|
148 | + |
|
149 | + if ($val === null) { |
|
150 | + return null; |
|
151 | + } |
|
152 | + |
|
153 | + return $this->decode($val); |
|
154 | + } |
|
155 | + |
|
156 | + |
|
157 | + /** |
|
158 | + * @param ExtendedCacheItemInterface ...$items |
|
159 | + * @return array<array<string, mixed>> |
|
160 | + * @throws \Phpfastcache\Exceptions\PhpfastcacheDriverException |
|
161 | + * @throws \RedisException |
|
162 | + */ |
|
163 | + protected function driverReadMultiple(ExtendedCacheItemInterface ...$items): array |
|
164 | + { |
|
165 | + $keys = $this->getKeys($items); |
|
166 | + |
|
167 | + return array_combine($keys, array_map( |
|
168 | + fn($val) => $val ? $this->decode($val) : null, |
|
169 | + $this->instance->mget($keys) |
|
170 | + )); |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * @return array<int, string> |
|
175 | + * @throws \RedisException |
|
176 | + */ |
|
177 | + protected function driverReadAllKeys(string $pattern = '*'): iterable |
|
178 | + { |
|
179 | + $keys = []; |
|
180 | + foreach (new PredisIterator\Keyspace($this->instance, $pattern, ExtendedCacheItemPoolInterface::MAX_ALL_KEYS_COUNT) as $key) { |
|
181 | + $keys[] = $key; |
|
182 | + } |
|
183 | + return $keys; |
|
184 | + } |
|
185 | + |
|
186 | + |
|
187 | + /** |
|
188 | + * @param ExtendedCacheItemInterface $item |
|
189 | + * @return mixed |
|
190 | + * @throws PhpfastcacheInvalidArgumentException |
|
191 | + * @throws PhpfastcacheLogicException |
|
192 | + */ |
|
193 | + protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
194 | + { |
|
195 | + |
|
196 | + $ttl = $item->getExpirationDate()->getTimestamp() - time(); |
|
197 | + |
|
198 | + /** |
|
199 | + * @see https://redis.io/commands/setex |
|
200 | + * @see https://redis.io/commands/expire |
|
201 | + */ |
|
202 | + if ($ttl <= 0) { |
|
203 | + return (bool)$this->instance->expire($item->getKey(), 0); |
|
204 | + } |
|
205 | + |
|
206 | + return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item)))->getPayload() === 'OK'; |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * @param string $key |
|
211 | + * @param string $encodedKey |
|
212 | + * @return bool |
|
213 | + */ |
|
214 | + protected function driverDelete(string $key, string $encodedKey): bool |
|
215 | + { |
|
216 | + return (bool)$this->instance->del([$key]); |
|
217 | + } |
|
218 | + |
|
219 | + /** |
|
220 | + * @param string[] $keys |
|
221 | + * @return bool |
|
222 | + */ |
|
223 | + protected function driverDeleteMultiple(array $keys): bool |
|
224 | + { |
|
225 | + return (bool) $this->instance->del(...$keys); |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * @return bool |
|
230 | + */ |
|
231 | + protected function driverClear(): bool |
|
232 | + { |
|
233 | + return $this->instance->flushdb()->getPayload() === 'OK'; |
|
234 | + } |
|
235 | 235 | } |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | return (new DriverStatistic()) |
76 | 76 | ->setData(implode(', ', array_keys($this->itemInstances))) |
77 | 77 | ->setRawData($info) |
78 | - ->setSize((int)$size) |
|
78 | + ->setSize((int) $size) |
|
79 | 79 | ->setInfo( |
80 | 80 | sprintf( |
81 | 81 | "The Redis daemon v%s (with Predis v%s) is up since %s.\n For more information see RawData. \n Driver size includes the memory allocation size.", |
@@ -200,7 +200,7 @@ discard block |
||
200 | 200 | * @see https://redis.io/commands/expire |
201 | 201 | */ |
202 | 202 | if ($ttl <= 0) { |
203 | - return (bool)$this->instance->expire($item->getKey(), 0); |
|
203 | + return (bool) $this->instance->expire($item->getKey(), 0); |
|
204 | 204 | } |
205 | 205 | |
206 | 206 | return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item)))->getPayload() === 'OK'; |
@@ -213,7 +213,7 @@ discard block |
||
213 | 213 | */ |
214 | 214 | protected function driverDelete(string $key, string $encodedKey): bool |
215 | 215 | { |
216 | - return (bool)$this->instance->del([$key]); |
|
216 | + return (bool) $this->instance->del([$key]); |
|
217 | 217 | } |
218 | 218 | |
219 | 219 | /** |
@@ -121,13 +121,15 @@ |
||
121 | 121 | ], |
122 | 122 | $options |
123 | 123 | ); |
124 | - } else { |
|
124 | + } |
|
125 | + else { |
|
125 | 126 | $this->instance = new PredisClient($this->getConfig()->getPredisConfigArray(), $options); |
126 | 127 | } |
127 | 128 | |
128 | 129 | try { |
129 | 130 | $this->instance->connect(); |
130 | - } catch (PredisConnectionException $e) { |
|
131 | + } |
|
132 | + catch (PredisConnectionException $e) { |
|
131 | 133 | throw new PhpfastcacheDriverException( |
132 | 134 | 'Failed to connect to predis server. Check the Predis documentation: https://github.com/nrk/predis/tree/v1.1#how-to-install-and-use-predis', |
133 | 135 | 0, |
@@ -23,194 +23,194 @@ |
||
23 | 23 | |
24 | 24 | class Config extends ConfigurationOption |
25 | 25 | { |
26 | - protected string $host = '127.0.0.1'; |
|
27 | - protected int $port = 6379; |
|
28 | - protected string $password = ''; |
|
29 | - protected int $database = 0; |
|
30 | - protected ?Client $predisClient = null; |
|
31 | - protected string $optPrefix = ''; |
|
32 | - protected int $timeout = 5; |
|
33 | - protected bool $persistent = false; |
|
34 | - protected string $scheme = 'unix'; |
|
26 | + protected string $host = '127.0.0.1'; |
|
27 | + protected int $port = 6379; |
|
28 | + protected string $password = ''; |
|
29 | + protected int $database = 0; |
|
30 | + protected ?Client $predisClient = null; |
|
31 | + protected string $optPrefix = ''; |
|
32 | + protected int $timeout = 5; |
|
33 | + protected bool $persistent = false; |
|
34 | + protected string $scheme = 'unix'; |
|
35 | 35 | /** |
36 | - * @return array<string, mixed> |
|
37 | - */ |
|
38 | - public function getPredisConfigArray(): array |
|
39 | - { |
|
40 | - return [ |
|
41 | - 'host' => $this->getHost(), |
|
42 | - 'port' => $this->getPort(), |
|
43 | - 'password' => $this->getPassword() ?: null, |
|
44 | - 'database' => $this->getDatabase(), |
|
45 | - 'timeout' => $this->getTimeout(), |
|
46 | - ]; |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * @return string |
|
51 | - */ |
|
52 | - public function getHost(): string |
|
53 | - { |
|
54 | - return $this->host; |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * @param string $host |
|
59 | - * @return Config |
|
60 | - * @throws PhpfastcacheLogicException |
|
61 | - */ |
|
62 | - public function setHost(string $host): static |
|
63 | - { |
|
64 | - return $this->setProperty('host', $host); |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * @return int |
|
69 | - */ |
|
70 | - public function getPort(): int |
|
71 | - { |
|
72 | - return $this->port; |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * @param int $port |
|
77 | - * @return Config |
|
78 | - * @throws PhpfastcacheLogicException |
|
79 | - */ |
|
80 | - public function setPort(int $port): static |
|
81 | - { |
|
82 | - return $this->setProperty('port', $port); |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * @return string |
|
87 | - */ |
|
88 | - public function getPassword(): string |
|
89 | - { |
|
90 | - return $this->password; |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * @param string $password |
|
95 | - * @return self |
|
96 | - * @throws PhpfastcacheLogicException |
|
97 | - */ |
|
98 | - public function setPassword(string $password): static |
|
99 | - { |
|
100 | - return $this->setProperty('password', $password); |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * @return int |
|
105 | - */ |
|
106 | - public function getDatabase(): int |
|
107 | - { |
|
108 | - return $this->database; |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * @param int $database |
|
113 | - * @return Config |
|
114 | - * @throws PhpfastcacheLogicException |
|
115 | - */ |
|
116 | - public function setDatabase(int $database): static |
|
117 | - { |
|
118 | - return $this->setProperty('database', $database); |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * @return int |
|
123 | - */ |
|
124 | - public function getTimeout(): int |
|
125 | - { |
|
126 | - return $this->timeout; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * @param int $timeout |
|
131 | - * @return self |
|
132 | - * @throws PhpfastcacheLogicException |
|
133 | - */ |
|
134 | - public function setTimeout(int $timeout): static |
|
135 | - { |
|
136 | - return $this->setProperty('timeout', $timeout); |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * @return Client|null |
|
141 | - */ |
|
142 | - public function getPredisClient(): ?Client |
|
143 | - { |
|
144 | - return $this->predisClient; |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * @param Client|null $predisClient |
|
149 | - * @return Config |
|
150 | - * @throws PhpfastcacheLogicException |
|
151 | - */ |
|
152 | - public function setPredisClient(?Client $predisClient = null): Config |
|
153 | - { |
|
154 | - return $this->setProperty('predisClient', $predisClient); |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * @return string |
|
159 | - * @since 7.0.2 |
|
160 | - */ |
|
161 | - public function getOptPrefix(): string |
|
162 | - { |
|
163 | - return $this->optPrefix; |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * @param string $optPrefix |
|
168 | - * @return Config |
|
169 | - * @throws PhpfastcacheLogicException |
|
170 | - * @since 7.0.2 |
|
171 | - */ |
|
172 | - public function setOptPrefix(string $optPrefix): Config |
|
173 | - { |
|
174 | - return $this->setProperty('optPrefix', trim($optPrefix)); |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * @return bool |
|
179 | - */ |
|
180 | - public function isPersistent(): bool |
|
181 | - { |
|
182 | - return $this->persistent; |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * @param bool $persistent |
|
187 | - * @return Config |
|
188 | - * @throws PhpfastcacheLogicException |
|
189 | - */ |
|
190 | - public function setPersistent(bool $persistent): Config |
|
191 | - { |
|
192 | - return $this->setProperty('persistent', $persistent); |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * @return string |
|
197 | - */ |
|
198 | - public function getScheme(): string |
|
199 | - { |
|
200 | - return $this->scheme; |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * @param string $scheme |
|
205 | - * @return Config |
|
206 | - * @throws PhpfastcacheInvalidConfigurationException |
|
207 | - * @throws PhpfastcacheLogicException |
|
208 | - */ |
|
209 | - public function setScheme(string $scheme): Config |
|
210 | - { |
|
211 | - if (!in_array($scheme, ['unix', 'tls'], true)) { |
|
212 | - throw new PhpfastcacheInvalidConfigurationException('Invalid scheme: ' . $scheme); |
|
213 | - } |
|
214 | - return $this->setProperty('scheme', $scheme); |
|
215 | - } |
|
36 | + * @return array<string, mixed> |
|
37 | + */ |
|
38 | + public function getPredisConfigArray(): array |
|
39 | + { |
|
40 | + return [ |
|
41 | + 'host' => $this->getHost(), |
|
42 | + 'port' => $this->getPort(), |
|
43 | + 'password' => $this->getPassword() ?: null, |
|
44 | + 'database' => $this->getDatabase(), |
|
45 | + 'timeout' => $this->getTimeout(), |
|
46 | + ]; |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * @return string |
|
51 | + */ |
|
52 | + public function getHost(): string |
|
53 | + { |
|
54 | + return $this->host; |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * @param string $host |
|
59 | + * @return Config |
|
60 | + * @throws PhpfastcacheLogicException |
|
61 | + */ |
|
62 | + public function setHost(string $host): static |
|
63 | + { |
|
64 | + return $this->setProperty('host', $host); |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * @return int |
|
69 | + */ |
|
70 | + public function getPort(): int |
|
71 | + { |
|
72 | + return $this->port; |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * @param int $port |
|
77 | + * @return Config |
|
78 | + * @throws PhpfastcacheLogicException |
|
79 | + */ |
|
80 | + public function setPort(int $port): static |
|
81 | + { |
|
82 | + return $this->setProperty('port', $port); |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * @return string |
|
87 | + */ |
|
88 | + public function getPassword(): string |
|
89 | + { |
|
90 | + return $this->password; |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * @param string $password |
|
95 | + * @return self |
|
96 | + * @throws PhpfastcacheLogicException |
|
97 | + */ |
|
98 | + public function setPassword(string $password): static |
|
99 | + { |
|
100 | + return $this->setProperty('password', $password); |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * @return int |
|
105 | + */ |
|
106 | + public function getDatabase(): int |
|
107 | + { |
|
108 | + return $this->database; |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * @param int $database |
|
113 | + * @return Config |
|
114 | + * @throws PhpfastcacheLogicException |
|
115 | + */ |
|
116 | + public function setDatabase(int $database): static |
|
117 | + { |
|
118 | + return $this->setProperty('database', $database); |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * @return int |
|
123 | + */ |
|
124 | + public function getTimeout(): int |
|
125 | + { |
|
126 | + return $this->timeout; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * @param int $timeout |
|
131 | + * @return self |
|
132 | + * @throws PhpfastcacheLogicException |
|
133 | + */ |
|
134 | + public function setTimeout(int $timeout): static |
|
135 | + { |
|
136 | + return $this->setProperty('timeout', $timeout); |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * @return Client|null |
|
141 | + */ |
|
142 | + public function getPredisClient(): ?Client |
|
143 | + { |
|
144 | + return $this->predisClient; |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * @param Client|null $predisClient |
|
149 | + * @return Config |
|
150 | + * @throws PhpfastcacheLogicException |
|
151 | + */ |
|
152 | + public function setPredisClient(?Client $predisClient = null): Config |
|
153 | + { |
|
154 | + return $this->setProperty('predisClient', $predisClient); |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * @return string |
|
159 | + * @since 7.0.2 |
|
160 | + */ |
|
161 | + public function getOptPrefix(): string |
|
162 | + { |
|
163 | + return $this->optPrefix; |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * @param string $optPrefix |
|
168 | + * @return Config |
|
169 | + * @throws PhpfastcacheLogicException |
|
170 | + * @since 7.0.2 |
|
171 | + */ |
|
172 | + public function setOptPrefix(string $optPrefix): Config |
|
173 | + { |
|
174 | + return $this->setProperty('optPrefix', trim($optPrefix)); |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * @return bool |
|
179 | + */ |
|
180 | + public function isPersistent(): bool |
|
181 | + { |
|
182 | + return $this->persistent; |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * @param bool $persistent |
|
187 | + * @return Config |
|
188 | + * @throws PhpfastcacheLogicException |
|
189 | + */ |
|
190 | + public function setPersistent(bool $persistent): Config |
|
191 | + { |
|
192 | + return $this->setProperty('persistent', $persistent); |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * @return string |
|
197 | + */ |
|
198 | + public function getScheme(): string |
|
199 | + { |
|
200 | + return $this->scheme; |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * @param string $scheme |
|
205 | + * @return Config |
|
206 | + * @throws PhpfastcacheInvalidConfigurationException |
|
207 | + * @throws PhpfastcacheLogicException |
|
208 | + */ |
|
209 | + public function setScheme(string $scheme): Config |
|
210 | + { |
|
211 | + if (!in_array($scheme, ['unix', 'tls'], true)) { |
|
212 | + throw new PhpfastcacheInvalidConfigurationException('Invalid scheme: ' . $scheme); |
|
213 | + } |
|
214 | + return $this->setProperty('scheme', $scheme); |
|
215 | + } |
|
216 | 216 | } |
@@ -42,189 +42,189 @@ |
||
42 | 42 | */ |
43 | 43 | class Driver implements AggregatablePoolInterface |
44 | 44 | { |
45 | - use TaggableCacheItemPoolTrait { |
|
46 | - __construct as protected __parentConstruct; |
|
47 | - } |
|
48 | - use MemcacheDriverCollisionDetectorTrait; |
|
49 | - |
|
50 | - /** |
|
51 | - * Driver constructor. |
|
52 | - * @param ConfigurationOption $config |
|
53 | - * @param string $instanceId |
|
54 | - * @param EventManagerInterface $em |
|
55 | - * @throws PhpfastcacheDriverConnectException |
|
56 | - * @throws PhpfastcacheInvalidArgumentException |
|
57 | - * @throws PhpfastcacheCoreException |
|
58 | - * @throws PhpfastcacheDriverCheckException |
|
59 | - * @throws PhpfastcacheIOException |
|
60 | - */ |
|
61 | - public function __construct(ConfigurationOption $config, string $instanceId, EventManagerInterface $em) |
|
62 | - { |
|
63 | - self::checkCollision('Memcached'); |
|
64 | - $this->__parentConstruct($config, $instanceId, $em); |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * @return bool |
|
69 | - */ |
|
70 | - public function driverCheck(): bool |
|
71 | - { |
|
72 | - return class_exists('Memcached'); |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * @return DriverStatistic |
|
77 | - */ |
|
78 | - public function getStats(): DriverStatistic |
|
79 | - { |
|
80 | - $stats = current($this->instance->getStats()); |
|
81 | - $stats['uptime'] = $stats['uptime'] ?? 0; |
|
82 | - $stats['bytes'] = $stats['bytes'] ?? 0; |
|
83 | - $stats['version'] = $stats['version'] ?? $this->instance->getVersion(); |
|
84 | - |
|
85 | - $date = (new DateTime())->setTimestamp(time() - $stats['uptime']); |
|
86 | - |
|
87 | - return (new DriverStatistic()) |
|
88 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
89 | - ->setInfo(sprintf("The memcache daemon v%s is up since %s.\n For more information see RawData.", $stats['version'], $date->format(DATE_RFC2822))) |
|
90 | - ->setRawData($stats) |
|
91 | - ->setSize((int)$stats['bytes']); |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * @return bool |
|
96 | - * @throws PhpfastcacheDriverException |
|
97 | - */ |
|
98 | - protected function driverConnect(): bool |
|
99 | - { |
|
100 | - $this->instance = new MemcachedSoftware(); |
|
101 | - $optPrefix = $this->getConfig()->getOptPrefix(); |
|
102 | - $this->instance->setOption(MemcachedSoftware::OPT_BINARY_PROTOCOL, true); |
|
103 | - |
|
104 | - if ($optPrefix) { |
|
105 | - $this->instance->setOption(MemcachedSoftware::OPT_PREFIX_KEY, $optPrefix); |
|
106 | - } |
|
107 | - |
|
108 | - foreach ($this->getConfig()->getServers() as $server) { |
|
109 | - $connected = false; |
|
110 | - /** |
|
111 | - * If path is provided we consider it as an UNIX Socket |
|
112 | - */ |
|
113 | - if (!empty($server['path'])) { |
|
114 | - $connected = $this->instance->addServer($server['path'], 0); |
|
115 | - } elseif (!empty($server['host'])) { |
|
116 | - $connected = $this->instance->addServer($server['host'], $server['port']); |
|
117 | - } |
|
118 | - if (!empty($server['saslUser']) && !empty($server['saslPassword'])) { |
|
119 | - $this->instance->setSaslAuthData($server['saslUser'], $server['saslPassword']); |
|
120 | - } |
|
121 | - if (!$connected) { |
|
122 | - throw new PhpfastcacheDriverConnectException( |
|
123 | - sprintf( |
|
124 | - 'Failed to connect to memcache host/path "%s".', |
|
125 | - $server['host'] ?: $server['path'], |
|
126 | - ) |
|
127 | - ); |
|
128 | - } |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Since Memcached does not throw |
|
133 | - * any error if not connected ... |
|
134 | - */ |
|
135 | - $version = $this->instance->getVersion(); |
|
136 | - if (!$version || $this->instance->getResultCode() !== MemcachedSoftware::RES_SUCCESS) { |
|
137 | - throw new PhpfastcacheDriverException('Memcached seems to not be connected'); |
|
138 | - } |
|
139 | - return true; |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * @param ExtendedCacheItemInterface $item |
|
144 | - * @return ?array<string, mixed> |
|
145 | - */ |
|
146 | - protected function driverRead(ExtendedCacheItemInterface $item): ?array |
|
147 | - { |
|
148 | - $val = $this->instance->get($item->getKey()); |
|
149 | - |
|
150 | - if (empty($val) || !\is_array($val)) { |
|
151 | - return null; |
|
152 | - } |
|
153 | - |
|
154 | - return $val; |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * @param ExtendedCacheItemInterface ...$items |
|
159 | - * @return array<array<string, mixed>> |
|
160 | - */ |
|
161 | - protected function driverReadMultiple(ExtendedCacheItemInterface ...$items): array |
|
162 | - { |
|
163 | - $keys = $this->getKeys($items); |
|
164 | - |
|
165 | - $val = $this->instance->getMulti($keys); |
|
166 | - |
|
167 | - if (empty($val) || !\is_array($val)) { |
|
168 | - return []; |
|
169 | - } |
|
170 | - |
|
171 | - return $val; |
|
172 | - } |
|
173 | - |
|
174 | - |
|
175 | - /** |
|
176 | - * @return array<string, mixed> |
|
177 | - * @throws PhpfastcacheInvalidArgumentException |
|
178 | - */ |
|
179 | - protected function driverReadAllKeys(string $pattern = ''): iterable |
|
180 | - { |
|
181 | - if ($pattern !== '') { |
|
182 | - $this->throwUnsupportedDriverReadAllPattern('https://www.php.net/manual/en/memcached.getallkeys.php'); |
|
183 | - } |
|
184 | - $keys = $this->instance->getAllKeys(); |
|
185 | - |
|
186 | - if (is_iterable($keys)) { |
|
187 | - return $keys; |
|
188 | - } else { |
|
189 | - return []; |
|
190 | - } |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * @param ExtendedCacheItemInterface $item |
|
195 | - * @return bool |
|
196 | - * @throws PhpfastcacheInvalidArgumentException |
|
197 | - * @throws PhpfastcacheLogicException |
|
198 | - */ |
|
199 | - protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
200 | - { |
|
201 | - |
|
202 | - $ttl = $item->getExpirationDate()->getTimestamp() - time(); |
|
203 | - |
|
204 | - // Memcache will only allow a expiration timer less than 2592000 seconds, |
|
205 | - // otherwise, it will assume you're giving it a UNIX timestamp. |
|
206 | - if ($ttl > 2592000) { |
|
207 | - $ttl = time() + $ttl; |
|
208 | - } |
|
209 | - |
|
210 | - return $this->instance->set($item->getKey(), $this->driverPreWrap($item), $ttl); |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * @param string $key |
|
215 | - * @param string $encodedKey |
|
216 | - * @return bool |
|
217 | - */ |
|
218 | - protected function driverDelete(string $key, string $encodedKey): bool |
|
219 | - { |
|
220 | - return $this->instance->delete($key); |
|
221 | - } |
|
222 | - |
|
223 | - /** |
|
224 | - * @return bool |
|
225 | - */ |
|
226 | - protected function driverClear(): bool |
|
227 | - { |
|
228 | - return $this->instance->flush(); |
|
229 | - } |
|
45 | + use TaggableCacheItemPoolTrait { |
|
46 | + __construct as protected __parentConstruct; |
|
47 | + } |
|
48 | + use MemcacheDriverCollisionDetectorTrait; |
|
49 | + |
|
50 | + /** |
|
51 | + * Driver constructor. |
|
52 | + * @param ConfigurationOption $config |
|
53 | + * @param string $instanceId |
|
54 | + * @param EventManagerInterface $em |
|
55 | + * @throws PhpfastcacheDriverConnectException |
|
56 | + * @throws PhpfastcacheInvalidArgumentException |
|
57 | + * @throws PhpfastcacheCoreException |
|
58 | + * @throws PhpfastcacheDriverCheckException |
|
59 | + * @throws PhpfastcacheIOException |
|
60 | + */ |
|
61 | + public function __construct(ConfigurationOption $config, string $instanceId, EventManagerInterface $em) |
|
62 | + { |
|
63 | + self::checkCollision('Memcached'); |
|
64 | + $this->__parentConstruct($config, $instanceId, $em); |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * @return bool |
|
69 | + */ |
|
70 | + public function driverCheck(): bool |
|
71 | + { |
|
72 | + return class_exists('Memcached'); |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * @return DriverStatistic |
|
77 | + */ |
|
78 | + public function getStats(): DriverStatistic |
|
79 | + { |
|
80 | + $stats = current($this->instance->getStats()); |
|
81 | + $stats['uptime'] = $stats['uptime'] ?? 0; |
|
82 | + $stats['bytes'] = $stats['bytes'] ?? 0; |
|
83 | + $stats['version'] = $stats['version'] ?? $this->instance->getVersion(); |
|
84 | + |
|
85 | + $date = (new DateTime())->setTimestamp(time() - $stats['uptime']); |
|
86 | + |
|
87 | + return (new DriverStatistic()) |
|
88 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
89 | + ->setInfo(sprintf("The memcache daemon v%s is up since %s.\n For more information see RawData.", $stats['version'], $date->format(DATE_RFC2822))) |
|
90 | + ->setRawData($stats) |
|
91 | + ->setSize((int)$stats['bytes']); |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * @return bool |
|
96 | + * @throws PhpfastcacheDriverException |
|
97 | + */ |
|
98 | + protected function driverConnect(): bool |
|
99 | + { |
|
100 | + $this->instance = new MemcachedSoftware(); |
|
101 | + $optPrefix = $this->getConfig()->getOptPrefix(); |
|
102 | + $this->instance->setOption(MemcachedSoftware::OPT_BINARY_PROTOCOL, true); |
|
103 | + |
|
104 | + if ($optPrefix) { |
|
105 | + $this->instance->setOption(MemcachedSoftware::OPT_PREFIX_KEY, $optPrefix); |
|
106 | + } |
|
107 | + |
|
108 | + foreach ($this->getConfig()->getServers() as $server) { |
|
109 | + $connected = false; |
|
110 | + /** |
|
111 | + * If path is provided we consider it as an UNIX Socket |
|
112 | + */ |
|
113 | + if (!empty($server['path'])) { |
|
114 | + $connected = $this->instance->addServer($server['path'], 0); |
|
115 | + } elseif (!empty($server['host'])) { |
|
116 | + $connected = $this->instance->addServer($server['host'], $server['port']); |
|
117 | + } |
|
118 | + if (!empty($server['saslUser']) && !empty($server['saslPassword'])) { |
|
119 | + $this->instance->setSaslAuthData($server['saslUser'], $server['saslPassword']); |
|
120 | + } |
|
121 | + if (!$connected) { |
|
122 | + throw new PhpfastcacheDriverConnectException( |
|
123 | + sprintf( |
|
124 | + 'Failed to connect to memcache host/path "%s".', |
|
125 | + $server['host'] ?: $server['path'], |
|
126 | + ) |
|
127 | + ); |
|
128 | + } |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Since Memcached does not throw |
|
133 | + * any error if not connected ... |
|
134 | + */ |
|
135 | + $version = $this->instance->getVersion(); |
|
136 | + if (!$version || $this->instance->getResultCode() !== MemcachedSoftware::RES_SUCCESS) { |
|
137 | + throw new PhpfastcacheDriverException('Memcached seems to not be connected'); |
|
138 | + } |
|
139 | + return true; |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * @param ExtendedCacheItemInterface $item |
|
144 | + * @return ?array<string, mixed> |
|
145 | + */ |
|
146 | + protected function driverRead(ExtendedCacheItemInterface $item): ?array |
|
147 | + { |
|
148 | + $val = $this->instance->get($item->getKey()); |
|
149 | + |
|
150 | + if (empty($val) || !\is_array($val)) { |
|
151 | + return null; |
|
152 | + } |
|
153 | + |
|
154 | + return $val; |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * @param ExtendedCacheItemInterface ...$items |
|
159 | + * @return array<array<string, mixed>> |
|
160 | + */ |
|
161 | + protected function driverReadMultiple(ExtendedCacheItemInterface ...$items): array |
|
162 | + { |
|
163 | + $keys = $this->getKeys($items); |
|
164 | + |
|
165 | + $val = $this->instance->getMulti($keys); |
|
166 | + |
|
167 | + if (empty($val) || !\is_array($val)) { |
|
168 | + return []; |
|
169 | + } |
|
170 | + |
|
171 | + return $val; |
|
172 | + } |
|
173 | + |
|
174 | + |
|
175 | + /** |
|
176 | + * @return array<string, mixed> |
|
177 | + * @throws PhpfastcacheInvalidArgumentException |
|
178 | + */ |
|
179 | + protected function driverReadAllKeys(string $pattern = ''): iterable |
|
180 | + { |
|
181 | + if ($pattern !== '') { |
|
182 | + $this->throwUnsupportedDriverReadAllPattern('https://www.php.net/manual/en/memcached.getallkeys.php'); |
|
183 | + } |
|
184 | + $keys = $this->instance->getAllKeys(); |
|
185 | + |
|
186 | + if (is_iterable($keys)) { |
|
187 | + return $keys; |
|
188 | + } else { |
|
189 | + return []; |
|
190 | + } |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * @param ExtendedCacheItemInterface $item |
|
195 | + * @return bool |
|
196 | + * @throws PhpfastcacheInvalidArgumentException |
|
197 | + * @throws PhpfastcacheLogicException |
|
198 | + */ |
|
199 | + protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
200 | + { |
|
201 | + |
|
202 | + $ttl = $item->getExpirationDate()->getTimestamp() - time(); |
|
203 | + |
|
204 | + // Memcache will only allow a expiration timer less than 2592000 seconds, |
|
205 | + // otherwise, it will assume you're giving it a UNIX timestamp. |
|
206 | + if ($ttl > 2592000) { |
|
207 | + $ttl = time() + $ttl; |
|
208 | + } |
|
209 | + |
|
210 | + return $this->instance->set($item->getKey(), $this->driverPreWrap($item), $ttl); |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * @param string $key |
|
215 | + * @param string $encodedKey |
|
216 | + * @return bool |
|
217 | + */ |
|
218 | + protected function driverDelete(string $key, string $encodedKey): bool |
|
219 | + { |
|
220 | + return $this->instance->delete($key); |
|
221 | + } |
|
222 | + |
|
223 | + /** |
|
224 | + * @return bool |
|
225 | + */ |
|
226 | + protected function driverClear(): bool |
|
227 | + { |
|
228 | + return $this->instance->flush(); |
|
229 | + } |
|
230 | 230 | } |
@@ -78,7 +78,7 @@ |
||
78 | 78 | ->setData(implode(', ', array_keys($this->itemInstances))) |
79 | 79 | ->setInfo(sprintf("The memcache daemon v%s is up since %s.\n For more information see RawData.", $stats['version'], $date->format(DATE_RFC2822))) |
80 | 80 | ->setRawData($stats) |
81 | - ->setSize((int)$stats['bytes']); |
|
81 | + ->setSize((int) $stats['bytes']); |
|
82 | 82 | } |
83 | 83 | |
84 | 84 | /** |
@@ -58,8 +58,7 @@ discard block |
||
58 | 58 | * @throws PhpfastcacheDriverCheckException |
59 | 59 | * @throws PhpfastcacheIOException |
60 | 60 | */ |
61 | - public function __construct(ConfigurationOption $config, string $instanceId, EventManagerInterface $em) |
|
62 | - { |
|
61 | + public function __construct(ConfigurationOption $config, string $instanceId, EventManagerInterface $em) { |
|
63 | 62 | self::checkCollision('Memcached'); |
64 | 63 | $this->__parentConstruct($config, $instanceId, $em); |
65 | 64 | } |
@@ -112,7 +111,8 @@ discard block |
||
112 | 111 | */ |
113 | 112 | if (!empty($server['path'])) { |
114 | 113 | $connected = $this->instance->addServer($server['path'], 0); |
115 | - } elseif (!empty($server['host'])) { |
|
114 | + } |
|
115 | + elseif (!empty($server['host'])) { |
|
116 | 116 | $connected = $this->instance->addServer($server['host'], $server['port']); |
117 | 117 | } |
118 | 118 | if (!empty($server['saslUser']) && !empty($server['saslPassword'])) { |
@@ -185,7 +185,8 @@ discard block |
||
185 | 185 | |
186 | 186 | if (is_iterable($keys)) { |
187 | 187 | return $keys; |
188 | - } else { |
|
188 | + } |
|
189 | + else { |
|
189 | 190 | return []; |
190 | 191 | } |
191 | 192 | } |
@@ -22,182 +22,182 @@ |
||
22 | 22 | |
23 | 23 | class Config extends ConfigurationOption |
24 | 24 | { |
25 | - /** |
|
26 | - * @var array |
|
27 | - * |
|
28 | - * Multiple server can be added this way: |
|
29 | - * $cfg->setServers([ |
|
30 | - * [ |
|
31 | - * // If you use a UNIX socket set the host and port to null |
|
32 | - * 'host' => '127.0.0.1', |
|
33 | - * //'path' => 'path/to/unix/socket', |
|
34 | - * 'port' => 11211, |
|
35 | - * 'saslUser' => null, |
|
36 | - * 'saslPassword' => null, |
|
37 | - * ] |
|
38 | - * ]); |
|
39 | - */ |
|
40 | - |
|
41 | - /** @var array<array<string, mixed>> */ |
|
42 | - protected array $servers = []; |
|
43 | - protected string $host = '127.0.0.1'; |
|
44 | - protected int $port = 11211; |
|
45 | - protected string $saslUser = ''; |
|
46 | - protected string $saslPassword = ''; |
|
47 | - protected string $optPrefix = ''; |
|
25 | + /** |
|
26 | + * @var array |
|
27 | + * |
|
28 | + * Multiple server can be added this way: |
|
29 | + * $cfg->setServers([ |
|
30 | + * [ |
|
31 | + * // If you use a UNIX socket set the host and port to null |
|
32 | + * 'host' => '127.0.0.1', |
|
33 | + * //'path' => 'path/to/unix/socket', |
|
34 | + * 'port' => 11211, |
|
35 | + * 'saslUser' => null, |
|
36 | + * 'saslPassword' => null, |
|
37 | + * ] |
|
38 | + * ]); |
|
39 | + */ |
|
40 | + |
|
41 | + /** @var array<array<string, mixed>> */ |
|
42 | + protected array $servers = []; |
|
43 | + protected string $host = '127.0.0.1'; |
|
44 | + protected int $port = 11211; |
|
45 | + protected string $saslUser = ''; |
|
46 | + protected string $saslPassword = ''; |
|
47 | + protected string $optPrefix = ''; |
|
48 | 48 | /** |
49 | - * @return string |
|
50 | - */ |
|
51 | - public function getSaslUser(): string |
|
52 | - { |
|
53 | - return $this->saslUser; |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * @param string $saslUser |
|
58 | - * @return self |
|
59 | - * @throws PhpfastcacheLogicException |
|
60 | - */ |
|
61 | - public function setSaslUser(string $saslUser): static |
|
62 | - { |
|
63 | - return $this->setProperty('saslUser', $saslUser); |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * @return string |
|
68 | - */ |
|
69 | - public function getSaslPassword(): string |
|
70 | - { |
|
71 | - return $this->saslPassword; |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * @param string $saslPassword |
|
76 | - * @return self |
|
77 | - * @throws PhpfastcacheLogicException |
|
78 | - */ |
|
79 | - public function setSaslPassword(string $saslPassword): static |
|
80 | - { |
|
81 | - return $this->setProperty('saslPassword', $saslPassword); |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * @return array<array<string, mixed>> |
|
86 | - */ |
|
87 | - public function getServers(): array |
|
88 | - { |
|
89 | - if (!count($this->servers)) { |
|
90 | - return [ |
|
91 | - [ |
|
92 | - 'host' => $this->getHost(), |
|
93 | - 'path' => $this->getPath(), |
|
94 | - 'port' => $this->getPort(), |
|
95 | - 'saslUser' => $this->getSaslUser() ?: null, |
|
96 | - 'saslPassword' => $this->getSaslPassword() ?: null, |
|
97 | - ], |
|
98 | - ]; |
|
99 | - } |
|
100 | - |
|
101 | - return $this->servers; |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * @param array<array<string, mixed>> $servers |
|
106 | - * @return self |
|
107 | - * @throws PhpfastcacheInvalidConfigurationException |
|
108 | - * @throws PhpfastcacheLogicException |
|
109 | - * |
|
110 | - * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
|
111 | - * @SuppressWarnings(PHPMD.NPathComplexity) |
|
112 | - */ |
|
113 | - public function setServers(array $servers): static |
|
114 | - { |
|
115 | - foreach ($servers as $server) { |
|
116 | - if ($diff = array_diff(array_keys($server), ['host', 'port', 'saslUser', 'saslPassword', 'path'])) { |
|
117 | - throw new PhpfastcacheInvalidConfigurationException('Unknown keys for memcached server: ' . implode(', ', $diff)); |
|
118 | - } |
|
119 | - |
|
120 | - if (!empty($server['host']) && !empty($server['path'])) { |
|
121 | - throw new PhpfastcacheInvalidConfigurationException('Host and path cannot be simultaneous defined.'); |
|
122 | - } |
|
123 | - |
|
124 | - if ((isset($server['host']) && !is_string($server['host'])) || (empty($server['path']) && empty($server['host']))) { |
|
125 | - throw new PhpfastcacheInvalidConfigurationException('Host must be a valid string in "$server" configuration array if path is not defined'); |
|
126 | - } |
|
127 | - |
|
128 | - if ((isset($server['path']) && !is_string($server['path'])) || (empty($server['host']) && empty($server['path']))) { |
|
129 | - throw new PhpfastcacheInvalidConfigurationException('Path must be a valid string in "$server" configuration array if host is not defined'); |
|
130 | - } |
|
131 | - |
|
132 | - if (!empty($server['host']) && (empty($server['port']) || !is_int($server['port']) || $server['port'] < 1)) { |
|
133 | - throw new PhpfastcacheInvalidConfigurationException('Port must be a valid integer in "$server" configuration array'); |
|
134 | - } |
|
135 | - |
|
136 | - if (!empty($server['port']) && !empty($server['path'])) { |
|
137 | - throw new PhpfastcacheInvalidConfigurationException('Port should not be defined along with path'); |
|
138 | - } |
|
139 | - |
|
140 | - if (!empty($server['saslUser']) && !empty($server['saslPassword']) && (!is_string($server['saslUser']) || !is_string($server['saslPassword']))) { |
|
141 | - throw new PhpfastcacheInvalidConfigurationException('If provided, saslUser and saslPassword must be a string'); |
|
142 | - } |
|
143 | - } |
|
144 | - |
|
145 | - return $this->setProperty('servers', $servers); |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * @return string |
|
150 | - */ |
|
151 | - public function getHost(): string |
|
152 | - { |
|
153 | - return $this->host; |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * @param string $host |
|
158 | - * @return self |
|
159 | - * @throws PhpfastcacheLogicException |
|
160 | - */ |
|
161 | - public function setHost(string $host): static |
|
162 | - { |
|
163 | - return $this->setProperty('host', $host); |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * @return int |
|
168 | - */ |
|
169 | - public function getPort(): int |
|
170 | - { |
|
171 | - return $this->port; |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * @param int $port |
|
176 | - * @return Config |
|
177 | - * @throws PhpfastcacheLogicException |
|
178 | - */ |
|
179 | - public function setPort(int $port): static |
|
180 | - { |
|
181 | - return $this->setProperty('port', $port); |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * @return string |
|
186 | - * @since 8.0.2 |
|
187 | - */ |
|
188 | - public function getOptPrefix(): string |
|
189 | - { |
|
190 | - return $this->optPrefix; |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * @param string $optPrefix |
|
195 | - * @return Config |
|
196 | - * @throws PhpfastcacheLogicException |
|
197 | - * @since 8.0.2 |
|
198 | - */ |
|
199 | - public function setOptPrefix(string $optPrefix): Config |
|
200 | - { |
|
201 | - return $this->setProperty('optPrefix', trim($optPrefix)); |
|
202 | - } |
|
49 | + * @return string |
|
50 | + */ |
|
51 | + public function getSaslUser(): string |
|
52 | + { |
|
53 | + return $this->saslUser; |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * @param string $saslUser |
|
58 | + * @return self |
|
59 | + * @throws PhpfastcacheLogicException |
|
60 | + */ |
|
61 | + public function setSaslUser(string $saslUser): static |
|
62 | + { |
|
63 | + return $this->setProperty('saslUser', $saslUser); |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * @return string |
|
68 | + */ |
|
69 | + public function getSaslPassword(): string |
|
70 | + { |
|
71 | + return $this->saslPassword; |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * @param string $saslPassword |
|
76 | + * @return self |
|
77 | + * @throws PhpfastcacheLogicException |
|
78 | + */ |
|
79 | + public function setSaslPassword(string $saslPassword): static |
|
80 | + { |
|
81 | + return $this->setProperty('saslPassword', $saslPassword); |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * @return array<array<string, mixed>> |
|
86 | + */ |
|
87 | + public function getServers(): array |
|
88 | + { |
|
89 | + if (!count($this->servers)) { |
|
90 | + return [ |
|
91 | + [ |
|
92 | + 'host' => $this->getHost(), |
|
93 | + 'path' => $this->getPath(), |
|
94 | + 'port' => $this->getPort(), |
|
95 | + 'saslUser' => $this->getSaslUser() ?: null, |
|
96 | + 'saslPassword' => $this->getSaslPassword() ?: null, |
|
97 | + ], |
|
98 | + ]; |
|
99 | + } |
|
100 | + |
|
101 | + return $this->servers; |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * @param array<array<string, mixed>> $servers |
|
106 | + * @return self |
|
107 | + * @throws PhpfastcacheInvalidConfigurationException |
|
108 | + * @throws PhpfastcacheLogicException |
|
109 | + * |
|
110 | + * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
|
111 | + * @SuppressWarnings(PHPMD.NPathComplexity) |
|
112 | + */ |
|
113 | + public function setServers(array $servers): static |
|
114 | + { |
|
115 | + foreach ($servers as $server) { |
|
116 | + if ($diff = array_diff(array_keys($server), ['host', 'port', 'saslUser', 'saslPassword', 'path'])) { |
|
117 | + throw new PhpfastcacheInvalidConfigurationException('Unknown keys for memcached server: ' . implode(', ', $diff)); |
|
118 | + } |
|
119 | + |
|
120 | + if (!empty($server['host']) && !empty($server['path'])) { |
|
121 | + throw new PhpfastcacheInvalidConfigurationException('Host and path cannot be simultaneous defined.'); |
|
122 | + } |
|
123 | + |
|
124 | + if ((isset($server['host']) && !is_string($server['host'])) || (empty($server['path']) && empty($server['host']))) { |
|
125 | + throw new PhpfastcacheInvalidConfigurationException('Host must be a valid string in "$server" configuration array if path is not defined'); |
|
126 | + } |
|
127 | + |
|
128 | + if ((isset($server['path']) && !is_string($server['path'])) || (empty($server['host']) && empty($server['path']))) { |
|
129 | + throw new PhpfastcacheInvalidConfigurationException('Path must be a valid string in "$server" configuration array if host is not defined'); |
|
130 | + } |
|
131 | + |
|
132 | + if (!empty($server['host']) && (empty($server['port']) || !is_int($server['port']) || $server['port'] < 1)) { |
|
133 | + throw new PhpfastcacheInvalidConfigurationException('Port must be a valid integer in "$server" configuration array'); |
|
134 | + } |
|
135 | + |
|
136 | + if (!empty($server['port']) && !empty($server['path'])) { |
|
137 | + throw new PhpfastcacheInvalidConfigurationException('Port should not be defined along with path'); |
|
138 | + } |
|
139 | + |
|
140 | + if (!empty($server['saslUser']) && !empty($server['saslPassword']) && (!is_string($server['saslUser']) || !is_string($server['saslPassword']))) { |
|
141 | + throw new PhpfastcacheInvalidConfigurationException('If provided, saslUser and saslPassword must be a string'); |
|
142 | + } |
|
143 | + } |
|
144 | + |
|
145 | + return $this->setProperty('servers', $servers); |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * @return string |
|
150 | + */ |
|
151 | + public function getHost(): string |
|
152 | + { |
|
153 | + return $this->host; |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * @param string $host |
|
158 | + * @return self |
|
159 | + * @throws PhpfastcacheLogicException |
|
160 | + */ |
|
161 | + public function setHost(string $host): static |
|
162 | + { |
|
163 | + return $this->setProperty('host', $host); |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * @return int |
|
168 | + */ |
|
169 | + public function getPort(): int |
|
170 | + { |
|
171 | + return $this->port; |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * @param int $port |
|
176 | + * @return Config |
|
177 | + * @throws PhpfastcacheLogicException |
|
178 | + */ |
|
179 | + public function setPort(int $port): static |
|
180 | + { |
|
181 | + return $this->setProperty('port', $port); |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * @return string |
|
186 | + * @since 8.0.2 |
|
187 | + */ |
|
188 | + public function getOptPrefix(): string |
|
189 | + { |
|
190 | + return $this->optPrefix; |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * @param string $optPrefix |
|
195 | + * @return Config |
|
196 | + * @throws PhpfastcacheLogicException |
|
197 | + * @since 8.0.2 |
|
198 | + */ |
|
199 | + public function setOptPrefix(string $optPrefix): Config |
|
200 | + { |
|
201 | + return $this->setProperty('optPrefix', trim($optPrefix)); |
|
202 | + } |
|
203 | 203 | } |
@@ -35,104 +35,104 @@ |
||
35 | 35 | */ |
36 | 36 | class Driver implements AggregatablePoolInterface |
37 | 37 | { |
38 | - use TaggableCacheItemPoolTrait; |
|
39 | - |
|
40 | - /** |
|
41 | - * @return bool |
|
42 | - */ |
|
43 | - public function driverCheck(): bool |
|
44 | - { |
|
45 | - static $driverCheck; |
|
46 | - |
|
47 | - return $driverCheck ?? ($driverCheck = class_exists(SSDB::class)); |
|
48 | - } |
|
49 | - |
|
50 | - /** |
|
51 | - * @return DriverStatistic |
|
52 | - */ |
|
53 | - public function getStats(): DriverStatistic |
|
54 | - { |
|
55 | - $stat = new DriverStatistic(); |
|
56 | - $info = $this->instance->info(); |
|
57 | - |
|
58 | - /** |
|
59 | - * Data returned by Ssdb are very poorly formatted |
|
60 | - * using hardcoded offset of pair key-value :-( |
|
61 | - */ |
|
62 | - $stat->setInfo(sprintf("Ssdb-server v%s with a total of %s call(s).\n For more information see RawData.", $info[2], $info[6])) |
|
63 | - ->setRawData($info) |
|
64 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
65 | - ->setSize($this->instance->dbsize()); |
|
66 | - |
|
67 | - return $stat; |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * @return bool |
|
72 | - * @throws PhpfastcacheDriverException |
|
73 | - */ |
|
74 | - protected function driverConnect(): bool |
|
75 | - { |
|
76 | - try { |
|
77 | - $clientConfig = $this->getConfig(); |
|
78 | - |
|
79 | - $this->instance = new SimpleSSDB($clientConfig->getHost(), $clientConfig->getPort(), $clientConfig->getTimeout()); |
|
80 | - if (!empty($clientConfig->getPassword())) { |
|
81 | - $this->instance->auth($clientConfig->getPassword()); |
|
82 | - } |
|
83 | - $this->instance->ping(); |
|
84 | - |
|
85 | - return true; |
|
86 | - } catch (SSDBException $e) { |
|
87 | - throw new PhpfastcacheDriverCheckException('Ssdb failed to connect with error: ' . $e->getMessage(), 0, $e); |
|
88 | - } |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * @param ExtendedCacheItemInterface $item |
|
93 | - * @return ?array<string, mixed> |
|
94 | - */ |
|
95 | - protected function driverRead(ExtendedCacheItemInterface $item): ?array |
|
96 | - { |
|
97 | - $val = $this->instance->get($item->getEncodedKey()); |
|
98 | - |
|
99 | - if (empty($val)) { |
|
100 | - return null; |
|
101 | - } |
|
102 | - |
|
103 | - return $this->decode($val); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * @param ExtendedCacheItemInterface $item |
|
108 | - * @return mixed |
|
109 | - * @throws PhpfastcacheInvalidArgumentException |
|
110 | - * @throws PhpfastcacheLogicException |
|
111 | - */ |
|
112 | - protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
113 | - { |
|
114 | - |
|
115 | - return (bool)$this->instance->setx($item->getEncodedKey(), $this->encode($this->driverPreWrap($item)), $item->getTtl()); |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * @param string $key |
|
120 | - * @param string $encodedKey |
|
121 | - * @return bool |
|
122 | - */ |
|
123 | - protected function driverDelete(string $key, string $encodedKey): bool |
|
124 | - { |
|
125 | - return (bool)$this->instance->del($encodedKey); |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * @return bool |
|
130 | - */ |
|
131 | - protected function driverClear(): bool |
|
132 | - { |
|
133 | - $this->instance->flushdb('kv'); |
|
134 | - |
|
135 | - // Status not returned, then we assume its true |
|
136 | - return true; |
|
137 | - } |
|
38 | + use TaggableCacheItemPoolTrait; |
|
39 | + |
|
40 | + /** |
|
41 | + * @return bool |
|
42 | + */ |
|
43 | + public function driverCheck(): bool |
|
44 | + { |
|
45 | + static $driverCheck; |
|
46 | + |
|
47 | + return $driverCheck ?? ($driverCheck = class_exists(SSDB::class)); |
|
48 | + } |
|
49 | + |
|
50 | + /** |
|
51 | + * @return DriverStatistic |
|
52 | + */ |
|
53 | + public function getStats(): DriverStatistic |
|
54 | + { |
|
55 | + $stat = new DriverStatistic(); |
|
56 | + $info = $this->instance->info(); |
|
57 | + |
|
58 | + /** |
|
59 | + * Data returned by Ssdb are very poorly formatted |
|
60 | + * using hardcoded offset of pair key-value :-( |
|
61 | + */ |
|
62 | + $stat->setInfo(sprintf("Ssdb-server v%s with a total of %s call(s).\n For more information see RawData.", $info[2], $info[6])) |
|
63 | + ->setRawData($info) |
|
64 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
65 | + ->setSize($this->instance->dbsize()); |
|
66 | + |
|
67 | + return $stat; |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * @return bool |
|
72 | + * @throws PhpfastcacheDriverException |
|
73 | + */ |
|
74 | + protected function driverConnect(): bool |
|
75 | + { |
|
76 | + try { |
|
77 | + $clientConfig = $this->getConfig(); |
|
78 | + |
|
79 | + $this->instance = new SimpleSSDB($clientConfig->getHost(), $clientConfig->getPort(), $clientConfig->getTimeout()); |
|
80 | + if (!empty($clientConfig->getPassword())) { |
|
81 | + $this->instance->auth($clientConfig->getPassword()); |
|
82 | + } |
|
83 | + $this->instance->ping(); |
|
84 | + |
|
85 | + return true; |
|
86 | + } catch (SSDBException $e) { |
|
87 | + throw new PhpfastcacheDriverCheckException('Ssdb failed to connect with error: ' . $e->getMessage(), 0, $e); |
|
88 | + } |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * @param ExtendedCacheItemInterface $item |
|
93 | + * @return ?array<string, mixed> |
|
94 | + */ |
|
95 | + protected function driverRead(ExtendedCacheItemInterface $item): ?array |
|
96 | + { |
|
97 | + $val = $this->instance->get($item->getEncodedKey()); |
|
98 | + |
|
99 | + if (empty($val)) { |
|
100 | + return null; |
|
101 | + } |
|
102 | + |
|
103 | + return $this->decode($val); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * @param ExtendedCacheItemInterface $item |
|
108 | + * @return mixed |
|
109 | + * @throws PhpfastcacheInvalidArgumentException |
|
110 | + * @throws PhpfastcacheLogicException |
|
111 | + */ |
|
112 | + protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
113 | + { |
|
114 | + |
|
115 | + return (bool)$this->instance->setx($item->getEncodedKey(), $this->encode($this->driverPreWrap($item)), $item->getTtl()); |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * @param string $key |
|
120 | + * @param string $encodedKey |
|
121 | + * @return bool |
|
122 | + */ |
|
123 | + protected function driverDelete(string $key, string $encodedKey): bool |
|
124 | + { |
|
125 | + return (bool)$this->instance->del($encodedKey); |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * @return bool |
|
130 | + */ |
|
131 | + protected function driverClear(): bool |
|
132 | + { |
|
133 | + $this->instance->flushdb('kv'); |
|
134 | + |
|
135 | + // Status not returned, then we assume its true |
|
136 | + return true; |
|
137 | + } |
|
138 | 138 | } |
@@ -112,7 +112,7 @@ discard block |
||
112 | 112 | protected function driverWrite(ExtendedCacheItemInterface $item): bool |
113 | 113 | { |
114 | 114 | |
115 | - return (bool)$this->instance->setx($item->getEncodedKey(), $this->encode($this->driverPreWrap($item)), $item->getTtl()); |
|
115 | + return (bool) $this->instance->setx($item->getEncodedKey(), $this->encode($this->driverPreWrap($item)), $item->getTtl()); |
|
116 | 116 | } |
117 | 117 | |
118 | 118 | /** |
@@ -122,7 +122,7 @@ discard block |
||
122 | 122 | */ |
123 | 123 | protected function driverDelete(string $key, string $encodedKey): bool |
124 | 124 | { |
125 | - return (bool)$this->instance->del($encodedKey); |
|
125 | + return (bool) $this->instance->del($encodedKey); |
|
126 | 126 | } |
127 | 127 | |
128 | 128 | /** |
@@ -83,7 +83,8 @@ |
||
83 | 83 | $this->instance->ping(); |
84 | 84 | |
85 | 85 | return true; |
86 | - } catch (SSDBException $e) { |
|
86 | + } |
|
87 | + catch (SSDBException $e) { |
|
87 | 88 | throw new PhpfastcacheDriverCheckException('Ssdb failed to connect with error: ' . $e->getMessage(), 0, $e); |
88 | 89 | } |
89 | 90 | } |
@@ -21,79 +21,79 @@ |
||
21 | 21 | |
22 | 22 | class Config extends ConfigurationOption |
23 | 23 | { |
24 | - protected string $host = '127.0.0.1'; |
|
25 | - protected int $port = 8888; |
|
26 | - protected string $password = ''; |
|
27 | - protected int $timeout = 2000; |
|
24 | + protected string $host = '127.0.0.1'; |
|
25 | + protected int $port = 8888; |
|
26 | + protected string $password = ''; |
|
27 | + protected int $timeout = 2000; |
|
28 | 28 | /** |
29 | - * @return string |
|
30 | - */ |
|
31 | - public function getHost(): string |
|
32 | - { |
|
33 | - return $this->host; |
|
34 | - } |
|
29 | + * @return string |
|
30 | + */ |
|
31 | + public function getHost(): string |
|
32 | + { |
|
33 | + return $this->host; |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * @param string $host |
|
38 | - * @return Config |
|
39 | - * @throws PhpfastcacheLogicException |
|
40 | - */ |
|
41 | - public function setHost(string $host): Config |
|
42 | - { |
|
43 | - return $this->setProperty('host', $host); |
|
44 | - } |
|
36 | + /** |
|
37 | + * @param string $host |
|
38 | + * @return Config |
|
39 | + * @throws PhpfastcacheLogicException |
|
40 | + */ |
|
41 | + public function setHost(string $host): Config |
|
42 | + { |
|
43 | + return $this->setProperty('host', $host); |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * @return int |
|
48 | - */ |
|
49 | - public function getPort(): int |
|
50 | - { |
|
51 | - return $this->port; |
|
52 | - } |
|
46 | + /** |
|
47 | + * @return int |
|
48 | + */ |
|
49 | + public function getPort(): int |
|
50 | + { |
|
51 | + return $this->port; |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * @param int $port |
|
56 | - * @return Config |
|
57 | - * @throws PhpfastcacheLogicException |
|
58 | - */ |
|
59 | - public function setPort(int $port): Config |
|
60 | - { |
|
61 | - return $this->setProperty('port', $port); |
|
62 | - } |
|
54 | + /** |
|
55 | + * @param int $port |
|
56 | + * @return Config |
|
57 | + * @throws PhpfastcacheLogicException |
|
58 | + */ |
|
59 | + public function setPort(int $port): Config |
|
60 | + { |
|
61 | + return $this->setProperty('port', $port); |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * @return string |
|
66 | - */ |
|
67 | - public function getPassword(): string |
|
68 | - { |
|
69 | - return $this->password; |
|
70 | - } |
|
64 | + /** |
|
65 | + * @return string |
|
66 | + */ |
|
67 | + public function getPassword(): string |
|
68 | + { |
|
69 | + return $this->password; |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * @param string $password |
|
74 | - * @return Config |
|
75 | - * @throws PhpfastcacheLogicException |
|
76 | - */ |
|
77 | - public function setPassword(string $password): Config |
|
78 | - { |
|
79 | - return $this->setProperty('password', $password); |
|
80 | - } |
|
72 | + /** |
|
73 | + * @param string $password |
|
74 | + * @return Config |
|
75 | + * @throws PhpfastcacheLogicException |
|
76 | + */ |
|
77 | + public function setPassword(string $password): Config |
|
78 | + { |
|
79 | + return $this->setProperty('password', $password); |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * @return int |
|
84 | - */ |
|
85 | - public function getTimeout(): int |
|
86 | - { |
|
87 | - return $this->timeout; |
|
88 | - } |
|
82 | + /** |
|
83 | + * @return int |
|
84 | + */ |
|
85 | + public function getTimeout(): int |
|
86 | + { |
|
87 | + return $this->timeout; |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * @param int $timeout |
|
92 | - * @return Config |
|
93 | - * @throws PhpfastcacheLogicException |
|
94 | - */ |
|
95 | - public function setTimeout(int $timeout): Config |
|
96 | - { |
|
97 | - return $this->setProperty('timeout', $timeout); |
|
98 | - } |
|
90 | + /** |
|
91 | + * @param int $timeout |
|
92 | + * @return Config |
|
93 | + * @throws PhpfastcacheLogicException |
|
94 | + */ |
|
95 | + public function setTimeout(int $timeout): Config |
|
96 | + { |
|
97 | + return $this->setProperty('timeout', $timeout); |
|
98 | + } |
|
99 | 99 | } |
@@ -33,86 +33,86 @@ |
||
33 | 33 | */ |
34 | 34 | class Driver implements AggregatablePoolInterface |
35 | 35 | { |
36 | - use TaggableCacheItemPoolTrait; |
|
37 | - |
|
38 | - /** |
|
39 | - * @return bool |
|
40 | - */ |
|
41 | - public function driverCheck(): bool |
|
42 | - { |
|
43 | - return extension_loaded('apcu') && ((ini_get('apc.enabled') && SapiDetector::isWebScript()) || (ini_get('apc.enable_cli') && SapiDetector::isCliScript())); |
|
44 | - } |
|
45 | - |
|
46 | - /** |
|
47 | - * @return DriverStatistic |
|
48 | - */ |
|
49 | - public function getStats(): DriverStatistic |
|
50 | - { |
|
51 | - $stats = (array)apcu_cache_info(); |
|
52 | - $date = (new DateTime())->setTimestamp($stats['start_time']); |
|
53 | - |
|
54 | - return (new DriverStatistic()) |
|
55 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
56 | - ->setInfo( |
|
57 | - sprintf( |
|
58 | - "The APCU cache is up since %s, and have %d item(s) in cache.\n For more information see RawData.", |
|
59 | - $date->format(DATE_RFC2822), |
|
60 | - $stats['num_entries'] |
|
61 | - ) |
|
62 | - ) |
|
63 | - ->setRawData($stats) |
|
64 | - ->setSize((int)$stats['mem_size']); |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * @return bool |
|
69 | - */ |
|
70 | - protected function driverConnect(): bool |
|
71 | - { |
|
72 | - return true; |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * @param ExtendedCacheItemInterface $item |
|
77 | - * @return bool |
|
78 | - * @throws PhpfastcacheInvalidArgumentException |
|
79 | - */ |
|
80 | - protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
81 | - { |
|
82 | - |
|
83 | - return (bool)apcu_store($item->getKey(), $this->driverPreWrap($item), $item->getTtl()); |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * @param ExtendedCacheItemInterface $item |
|
88 | - * @return ?array<string, mixed> |
|
89 | - */ |
|
90 | - protected function driverRead(ExtendedCacheItemInterface $item): ?array |
|
91 | - { |
|
92 | - $data = apcu_fetch($item->getKey(), $success); |
|
93 | - |
|
94 | - if ($success === false || !\is_array($data)) { |
|
95 | - return null; |
|
96 | - } |
|
97 | - |
|
98 | - return $data; |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * @param string $key |
|
103 | - * @param string $encodedKey |
|
104 | - * @return bool |
|
105 | - */ |
|
106 | - protected function driverDelete(string $key, string $encodedKey): bool |
|
107 | - { |
|
108 | - return (bool)apcu_delete($key); |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * @return bool |
|
113 | - */ |
|
114 | - protected function driverClear(): bool |
|
115 | - { |
|
116 | - return @apcu_clear_cache(); |
|
117 | - } |
|
36 | + use TaggableCacheItemPoolTrait; |
|
37 | + |
|
38 | + /** |
|
39 | + * @return bool |
|
40 | + */ |
|
41 | + public function driverCheck(): bool |
|
42 | + { |
|
43 | + return extension_loaded('apcu') && ((ini_get('apc.enabled') && SapiDetector::isWebScript()) || (ini_get('apc.enable_cli') && SapiDetector::isCliScript())); |
|
44 | + } |
|
45 | + |
|
46 | + /** |
|
47 | + * @return DriverStatistic |
|
48 | + */ |
|
49 | + public function getStats(): DriverStatistic |
|
50 | + { |
|
51 | + $stats = (array)apcu_cache_info(); |
|
52 | + $date = (new DateTime())->setTimestamp($stats['start_time']); |
|
53 | + |
|
54 | + return (new DriverStatistic()) |
|
55 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
56 | + ->setInfo( |
|
57 | + sprintf( |
|
58 | + "The APCU cache is up since %s, and have %d item(s) in cache.\n For more information see RawData.", |
|
59 | + $date->format(DATE_RFC2822), |
|
60 | + $stats['num_entries'] |
|
61 | + ) |
|
62 | + ) |
|
63 | + ->setRawData($stats) |
|
64 | + ->setSize((int)$stats['mem_size']); |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * @return bool |
|
69 | + */ |
|
70 | + protected function driverConnect(): bool |
|
71 | + { |
|
72 | + return true; |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * @param ExtendedCacheItemInterface $item |
|
77 | + * @return bool |
|
78 | + * @throws PhpfastcacheInvalidArgumentException |
|
79 | + */ |
|
80 | + protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
81 | + { |
|
82 | + |
|
83 | + return (bool)apcu_store($item->getKey(), $this->driverPreWrap($item), $item->getTtl()); |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * @param ExtendedCacheItemInterface $item |
|
88 | + * @return ?array<string, mixed> |
|
89 | + */ |
|
90 | + protected function driverRead(ExtendedCacheItemInterface $item): ?array |
|
91 | + { |
|
92 | + $data = apcu_fetch($item->getKey(), $success); |
|
93 | + |
|
94 | + if ($success === false || !\is_array($data)) { |
|
95 | + return null; |
|
96 | + } |
|
97 | + |
|
98 | + return $data; |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * @param string $key |
|
103 | + * @param string $encodedKey |
|
104 | + * @return bool |
|
105 | + */ |
|
106 | + protected function driverDelete(string $key, string $encodedKey): bool |
|
107 | + { |
|
108 | + return (bool)apcu_delete($key); |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * @return bool |
|
113 | + */ |
|
114 | + protected function driverClear(): bool |
|
115 | + { |
|
116 | + return @apcu_clear_cache(); |
|
117 | + } |
|
118 | 118 | } |
@@ -48,7 +48,7 @@ discard block |
||
48 | 48 | */ |
49 | 49 | public function getStats(): DriverStatistic |
50 | 50 | { |
51 | - $stats = (array)apcu_cache_info(); |
|
51 | + $stats = (array) apcu_cache_info(); |
|
52 | 52 | $date = (new DateTime())->setTimestamp($stats['start_time']); |
53 | 53 | |
54 | 54 | return (new DriverStatistic()) |
@@ -61,7 +61,7 @@ discard block |
||
61 | 61 | ) |
62 | 62 | ) |
63 | 63 | ->setRawData($stats) |
64 | - ->setSize((int)$stats['mem_size']); |
|
64 | + ->setSize((int) $stats['mem_size']); |
|
65 | 65 | } |
66 | 66 | |
67 | 67 | /** |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | protected function driverWrite(ExtendedCacheItemInterface $item): bool |
81 | 81 | { |
82 | 82 | |
83 | - return (bool)apcu_store($item->getKey(), $this->driverPreWrap($item), $item->getTtl()); |
|
83 | + return (bool) apcu_store($item->getKey(), $this->driverPreWrap($item), $item->getTtl()); |
|
84 | 84 | } |
85 | 85 | |
86 | 86 | /** |
@@ -105,7 +105,7 @@ discard block |
||
105 | 105 | */ |
106 | 106 | protected function driverDelete(string $key, string $encodedKey): bool |
107 | 107 | { |
108 | - return (bool)apcu_delete($key); |
|
108 | + return (bool) apcu_delete($key); |
|
109 | 109 | } |
110 | 110 | |
111 | 111 | /** |
@@ -36,72 +36,72 @@ discard block |
||
36 | 36 | */ |
37 | 37 | class Driver implements AggregatablePoolInterface |
38 | 38 | { |
39 | - use TaggableCacheItemPoolTrait; |
|
40 | - |
|
41 | - protected const CASSANDRA_KEY_SPACE = 'phpfastcache'; |
|
42 | - protected const CASSANDRA_TABLE = 'cacheItems'; |
|
43 | - |
|
44 | - /** |
|
45 | - * @return bool |
|
46 | - */ |
|
47 | - public function driverCheck(): bool |
|
48 | - { |
|
49 | - return extension_loaded('Cassandra') && class_exists(Cassandra::class); |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * @return bool |
|
54 | - * @throws PhpfastcacheLogicException |
|
55 | - * @throws Exception |
|
56 | - */ |
|
57 | - protected function driverConnect(): bool |
|
58 | - { |
|
59 | - $clientConfig = $this->getConfig(); |
|
60 | - |
|
61 | - $clusterBuilder = Cassandra::cluster() |
|
62 | - ->withContactPoints($clientConfig->getHost()) |
|
63 | - ->withPort($clientConfig->getPort()); |
|
64 | - |
|
65 | - if (!empty($clientConfig->isSslEnabled())) { |
|
66 | - $sslBuilder = Cassandra::ssl(); |
|
67 | - if (!empty($clientConfig->isSslVerify())) { |
|
68 | - $sslBuilder->withVerifyFlags(Cassandra::VERIFY_PEER_CERT); |
|
69 | - } else { |
|
70 | - $sslBuilder->withVerifyFlags(Cassandra::VERIFY_NONE); |
|
71 | - } |
|
72 | - |
|
73 | - $clusterBuilder->withSSL($sslBuilder->build()); |
|
74 | - } |
|
75 | - |
|
76 | - $clusterBuilder->withConnectTimeout($clientConfig->getTimeout()); |
|
77 | - |
|
78 | - if ($clientConfig->getUsername()) { |
|
79 | - $clusterBuilder->withCredentials($clientConfig->getUsername(), $clientConfig->getPassword()); |
|
80 | - } |
|
81 | - |
|
82 | - $this->instance = $clusterBuilder->build()->connect(''); |
|
83 | - |
|
84 | - /** |
|
85 | - * In case of emergency: |
|
86 | - * $this->instance->execute( |
|
87 | - * new Cassandra\SimpleStatement(\sprintf("DROP KEYSPACE %s;", self::CASSANDRA_KEY_SPACE)) |
|
88 | - * ); |
|
89 | - */ |
|
90 | - |
|
91 | - $this->instance->execute( |
|
92 | - new Cassandra\SimpleStatement( |
|
93 | - sprintf( |
|
94 | - "CREATE KEYSPACE IF NOT EXISTS %s WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };", |
|
95 | - self::CASSANDRA_KEY_SPACE |
|
96 | - ) |
|
97 | - ), |
|
98 | - [] |
|
99 | - ); |
|
100 | - $this->instance->execute(new Cassandra\SimpleStatement(sprintf('USE %s;', self::CASSANDRA_KEY_SPACE)), []); |
|
101 | - $this->instance->execute( |
|
102 | - new Cassandra\SimpleStatement( |
|
103 | - sprintf( |
|
104 | - ' |
|
39 | + use TaggableCacheItemPoolTrait; |
|
40 | + |
|
41 | + protected const CASSANDRA_KEY_SPACE = 'phpfastcache'; |
|
42 | + protected const CASSANDRA_TABLE = 'cacheItems'; |
|
43 | + |
|
44 | + /** |
|
45 | + * @return bool |
|
46 | + */ |
|
47 | + public function driverCheck(): bool |
|
48 | + { |
|
49 | + return extension_loaded('Cassandra') && class_exists(Cassandra::class); |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * @return bool |
|
54 | + * @throws PhpfastcacheLogicException |
|
55 | + * @throws Exception |
|
56 | + */ |
|
57 | + protected function driverConnect(): bool |
|
58 | + { |
|
59 | + $clientConfig = $this->getConfig(); |
|
60 | + |
|
61 | + $clusterBuilder = Cassandra::cluster() |
|
62 | + ->withContactPoints($clientConfig->getHost()) |
|
63 | + ->withPort($clientConfig->getPort()); |
|
64 | + |
|
65 | + if (!empty($clientConfig->isSslEnabled())) { |
|
66 | + $sslBuilder = Cassandra::ssl(); |
|
67 | + if (!empty($clientConfig->isSslVerify())) { |
|
68 | + $sslBuilder->withVerifyFlags(Cassandra::VERIFY_PEER_CERT); |
|
69 | + } else { |
|
70 | + $sslBuilder->withVerifyFlags(Cassandra::VERIFY_NONE); |
|
71 | + } |
|
72 | + |
|
73 | + $clusterBuilder->withSSL($sslBuilder->build()); |
|
74 | + } |
|
75 | + |
|
76 | + $clusterBuilder->withConnectTimeout($clientConfig->getTimeout()); |
|
77 | + |
|
78 | + if ($clientConfig->getUsername()) { |
|
79 | + $clusterBuilder->withCredentials($clientConfig->getUsername(), $clientConfig->getPassword()); |
|
80 | + } |
|
81 | + |
|
82 | + $this->instance = $clusterBuilder->build()->connect(''); |
|
83 | + |
|
84 | + /** |
|
85 | + * In case of emergency: |
|
86 | + * $this->instance->execute( |
|
87 | + * new Cassandra\SimpleStatement(\sprintf("DROP KEYSPACE %s;", self::CASSANDRA_KEY_SPACE)) |
|
88 | + * ); |
|
89 | + */ |
|
90 | + |
|
91 | + $this->instance->execute( |
|
92 | + new Cassandra\SimpleStatement( |
|
93 | + sprintf( |
|
94 | + "CREATE KEYSPACE IF NOT EXISTS %s WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };", |
|
95 | + self::CASSANDRA_KEY_SPACE |
|
96 | + ) |
|
97 | + ), |
|
98 | + [] |
|
99 | + ); |
|
100 | + $this->instance->execute(new Cassandra\SimpleStatement(sprintf('USE %s;', self::CASSANDRA_KEY_SPACE)), []); |
|
101 | + $this->instance->execute( |
|
102 | + new Cassandra\SimpleStatement( |
|
103 | + sprintf( |
|
104 | + ' |
|
105 | 105 | CREATE TABLE IF NOT EXISTS %s ( |
106 | 106 | cache_uuid uuid, |
107 | 107 | cache_id varchar, |
@@ -111,72 +111,72 @@ discard block |
||
111 | 111 | cache_length int, |
112 | 112 | PRIMARY KEY (cache_id) |
113 | 113 | );', |
114 | - self::CASSANDRA_TABLE |
|
115 | - ) |
|
116 | - ), |
|
117 | - [] |
|
118 | - ); |
|
119 | - |
|
120 | - return true; |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * @param ExtendedCacheItemInterface $item |
|
125 | - * @return ?array<string, mixed> |
|
126 | - */ |
|
127 | - protected function driverRead(ExtendedCacheItemInterface $item): ?array |
|
128 | - { |
|
129 | - try { |
|
130 | - $options = $this->getCompatibleExecutionOptionsArgument( |
|
131 | - [ |
|
132 | - 'arguments' => ['cache_id' => $item->getKey()], |
|
133 | - 'page_size' => 1, |
|
134 | - ] |
|
135 | - ); |
|
136 | - $query = sprintf( |
|
137 | - 'SELECT cache_data FROM %s.%s WHERE cache_id = :cache_id;', |
|
138 | - self::CASSANDRA_KEY_SPACE, |
|
139 | - self::CASSANDRA_TABLE |
|
140 | - ); |
|
141 | - $results = $this->instance->execute(new Cassandra\SimpleStatement($query), $options); |
|
142 | - |
|
143 | - if ($results instanceof Cassandra\Rows && $results->count() === 1) { |
|
144 | - return $this->decode($results->first()['cache_data']) ?: null; |
|
145 | - } |
|
146 | - |
|
147 | - return null; |
|
148 | - } catch (Exception $e) { |
|
149 | - return null; |
|
150 | - } |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * @param ExtendedCacheItemInterface $item |
|
155 | - * @return bool |
|
156 | - * @throws PhpfastcacheInvalidArgumentException |
|
157 | - */ |
|
158 | - protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
159 | - { |
|
160 | - |
|
161 | - try { |
|
162 | - $cacheData = $this->encode($this->driverPreWrap($item)); |
|
163 | - $options = $this->getCompatibleExecutionOptionsArgument( |
|
164 | - [ |
|
165 | - 'arguments' => [ |
|
166 | - 'cache_uuid' => new Cassandra\Uuid(''), |
|
167 | - 'cache_id' => $item->getKey(), |
|
168 | - 'cache_data' => $cacheData, |
|
169 | - 'cache_creation_date' => new Cassandra\Timestamp((new DateTime())->getTimestamp(), 0), |
|
170 | - 'cache_expiration_date' => new Cassandra\Timestamp($item->getExpirationDate()->getTimestamp(), 0), |
|
171 | - 'cache_length' => strlen($cacheData), |
|
172 | - ], |
|
173 | - 'consistency' => Cassandra::CONSISTENCY_ALL, |
|
174 | - 'serial_consistency' => Cassandra::CONSISTENCY_SERIAL, |
|
175 | - ] |
|
176 | - ); |
|
177 | - |
|
178 | - $query = sprintf( |
|
179 | - 'INSERT INTO %s.%s |
|
114 | + self::CASSANDRA_TABLE |
|
115 | + ) |
|
116 | + ), |
|
117 | + [] |
|
118 | + ); |
|
119 | + |
|
120 | + return true; |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * @param ExtendedCacheItemInterface $item |
|
125 | + * @return ?array<string, mixed> |
|
126 | + */ |
|
127 | + protected function driverRead(ExtendedCacheItemInterface $item): ?array |
|
128 | + { |
|
129 | + try { |
|
130 | + $options = $this->getCompatibleExecutionOptionsArgument( |
|
131 | + [ |
|
132 | + 'arguments' => ['cache_id' => $item->getKey()], |
|
133 | + 'page_size' => 1, |
|
134 | + ] |
|
135 | + ); |
|
136 | + $query = sprintf( |
|
137 | + 'SELECT cache_data FROM %s.%s WHERE cache_id = :cache_id;', |
|
138 | + self::CASSANDRA_KEY_SPACE, |
|
139 | + self::CASSANDRA_TABLE |
|
140 | + ); |
|
141 | + $results = $this->instance->execute(new Cassandra\SimpleStatement($query), $options); |
|
142 | + |
|
143 | + if ($results instanceof Cassandra\Rows && $results->count() === 1) { |
|
144 | + return $this->decode($results->first()['cache_data']) ?: null; |
|
145 | + } |
|
146 | + |
|
147 | + return null; |
|
148 | + } catch (Exception $e) { |
|
149 | + return null; |
|
150 | + } |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * @param ExtendedCacheItemInterface $item |
|
155 | + * @return bool |
|
156 | + * @throws PhpfastcacheInvalidArgumentException |
|
157 | + */ |
|
158 | + protected function driverWrite(ExtendedCacheItemInterface $item): bool |
|
159 | + { |
|
160 | + |
|
161 | + try { |
|
162 | + $cacheData = $this->encode($this->driverPreWrap($item)); |
|
163 | + $options = $this->getCompatibleExecutionOptionsArgument( |
|
164 | + [ |
|
165 | + 'arguments' => [ |
|
166 | + 'cache_uuid' => new Cassandra\Uuid(''), |
|
167 | + 'cache_id' => $item->getKey(), |
|
168 | + 'cache_data' => $cacheData, |
|
169 | + 'cache_creation_date' => new Cassandra\Timestamp((new DateTime())->getTimestamp(), 0), |
|
170 | + 'cache_expiration_date' => new Cassandra\Timestamp($item->getExpirationDate()->getTimestamp(), 0), |
|
171 | + 'cache_length' => strlen($cacheData), |
|
172 | + ], |
|
173 | + 'consistency' => Cassandra::CONSISTENCY_ALL, |
|
174 | + 'serial_consistency' => Cassandra::CONSISTENCY_SERIAL, |
|
175 | + ] |
|
176 | + ); |
|
177 | + |
|
178 | + $query = sprintf( |
|
179 | + 'INSERT INTO %s.%s |
|
180 | 180 | ( |
181 | 181 | cache_uuid, |
182 | 182 | cache_id, |
@@ -187,89 +187,89 @@ discard block |
||
187 | 187 | ) |
188 | 188 | VALUES (:cache_uuid, :cache_id, :cache_data, :cache_creation_date, :cache_expiration_date, :cache_length); |
189 | 189 | ', |
190 | - self::CASSANDRA_KEY_SPACE, |
|
191 | - self::CASSANDRA_TABLE |
|
192 | - ); |
|
193 | - |
|
194 | - $result = $this->instance->execute(new Cassandra\SimpleStatement($query), $options); |
|
195 | - /** |
|
196 | - * There's no real way atm |
|
197 | - * to know if the item has |
|
198 | - * been really upserted |
|
199 | - */ |
|
200 | - return $result instanceof Cassandra\Rows; |
|
201 | - } catch (InvalidArgumentException $e) { |
|
202 | - throw new PhpfastcacheInvalidArgumentException($e->getMessage(), 0, $e); |
|
203 | - } |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * @param string $key |
|
208 | - * @param string $encodedKey |
|
209 | - * @return bool |
|
210 | - */ |
|
211 | - protected function driverDelete(string $key, string $encodedKey): bool |
|
212 | - { |
|
213 | - |
|
214 | - try { |
|
215 | - $options = $this->getCompatibleExecutionOptionsArgument( |
|
216 | - [ |
|
217 | - 'arguments' => [ |
|
218 | - 'cache_id' => $key, |
|
219 | - ], |
|
220 | - ] |
|
221 | - ); |
|
222 | - $result = $this->instance->execute( |
|
223 | - new Cassandra\SimpleStatement( |
|
224 | - sprintf( |
|
225 | - 'DELETE FROM %s.%s WHERE cache_id = :cache_id;', |
|
226 | - self::CASSANDRA_KEY_SPACE, |
|
227 | - self::CASSANDRA_TABLE |
|
228 | - ) |
|
229 | - ), |
|
230 | - $options |
|
231 | - ); |
|
232 | - |
|
233 | - /** |
|
234 | - * There's no real way atm |
|
235 | - * to know if the item has |
|
236 | - * been really deleted |
|
237 | - */ |
|
238 | - return $result instanceof Cassandra\Rows; |
|
239 | - } catch (Exception $e) { |
|
240 | - return false; |
|
241 | - } |
|
242 | - } |
|
243 | - |
|
244 | - /** |
|
245 | - * @return bool |
|
246 | - */ |
|
247 | - protected function driverClear(): bool |
|
248 | - { |
|
249 | - try { |
|
250 | - $this->instance->execute( |
|
251 | - new Cassandra\SimpleStatement( |
|
252 | - sprintf( |
|
253 | - 'TRUNCATE %s.%s;', |
|
254 | - self::CASSANDRA_KEY_SPACE, |
|
255 | - self::CASSANDRA_TABLE |
|
256 | - ) |
|
257 | - ), |
|
258 | - null |
|
259 | - ); |
|
260 | - |
|
261 | - return true; |
|
262 | - } catch (Exception) { |
|
263 | - return false; |
|
264 | - } |
|
265 | - } |
|
266 | - |
|
267 | - /** |
|
268 | - * @return string |
|
269 | - */ |
|
270 | - public function getHelp(): string |
|
271 | - { |
|
272 | - return <<<HELP |
|
190 | + self::CASSANDRA_KEY_SPACE, |
|
191 | + self::CASSANDRA_TABLE |
|
192 | + ); |
|
193 | + |
|
194 | + $result = $this->instance->execute(new Cassandra\SimpleStatement($query), $options); |
|
195 | + /** |
|
196 | + * There's no real way atm |
|
197 | + * to know if the item has |
|
198 | + * been really upserted |
|
199 | + */ |
|
200 | + return $result instanceof Cassandra\Rows; |
|
201 | + } catch (InvalidArgumentException $e) { |
|
202 | + throw new PhpfastcacheInvalidArgumentException($e->getMessage(), 0, $e); |
|
203 | + } |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * @param string $key |
|
208 | + * @param string $encodedKey |
|
209 | + * @return bool |
|
210 | + */ |
|
211 | + protected function driverDelete(string $key, string $encodedKey): bool |
|
212 | + { |
|
213 | + |
|
214 | + try { |
|
215 | + $options = $this->getCompatibleExecutionOptionsArgument( |
|
216 | + [ |
|
217 | + 'arguments' => [ |
|
218 | + 'cache_id' => $key, |
|
219 | + ], |
|
220 | + ] |
|
221 | + ); |
|
222 | + $result = $this->instance->execute( |
|
223 | + new Cassandra\SimpleStatement( |
|
224 | + sprintf( |
|
225 | + 'DELETE FROM %s.%s WHERE cache_id = :cache_id;', |
|
226 | + self::CASSANDRA_KEY_SPACE, |
|
227 | + self::CASSANDRA_TABLE |
|
228 | + ) |
|
229 | + ), |
|
230 | + $options |
|
231 | + ); |
|
232 | + |
|
233 | + /** |
|
234 | + * There's no real way atm |
|
235 | + * to know if the item has |
|
236 | + * been really deleted |
|
237 | + */ |
|
238 | + return $result instanceof Cassandra\Rows; |
|
239 | + } catch (Exception $e) { |
|
240 | + return false; |
|
241 | + } |
|
242 | + } |
|
243 | + |
|
244 | + /** |
|
245 | + * @return bool |
|
246 | + */ |
|
247 | + protected function driverClear(): bool |
|
248 | + { |
|
249 | + try { |
|
250 | + $this->instance->execute( |
|
251 | + new Cassandra\SimpleStatement( |
|
252 | + sprintf( |
|
253 | + 'TRUNCATE %s.%s;', |
|
254 | + self::CASSANDRA_KEY_SPACE, |
|
255 | + self::CASSANDRA_TABLE |
|
256 | + ) |
|
257 | + ), |
|
258 | + null |
|
259 | + ); |
|
260 | + |
|
261 | + return true; |
|
262 | + } catch (Exception) { |
|
263 | + return false; |
|
264 | + } |
|
265 | + } |
|
266 | + |
|
267 | + /** |
|
268 | + * @return string |
|
269 | + */ |
|
270 | + public function getHelp(): string |
|
271 | + { |
|
272 | + return <<<HELP |
|
273 | 273 | <p> |
274 | 274 | To install the php Cassandra extension via Pecl: |
275 | 275 | <code>sudo pecl install cassandra</code> |
@@ -277,42 +277,42 @@ discard block |
||
277 | 277 | Please note that this repository only provide php stubs and C/C++ sources, it does NOT provide php client. |
278 | 278 | </p> |
279 | 279 | HELP; |
280 | - } |
|
281 | - |
|
282 | - /** |
|
283 | - * @return DriverStatistic |
|
284 | - * @throws Exception |
|
285 | - */ |
|
286 | - public function getStats(): DriverStatistic |
|
287 | - { |
|
288 | - $result = $this->instance->execute( |
|
289 | - new Cassandra\SimpleStatement( |
|
290 | - sprintf( |
|
291 | - 'SELECT SUM(cache_length) as cache_size FROM %s.%s', |
|
292 | - self::CASSANDRA_KEY_SPACE, |
|
293 | - self::CASSANDRA_TABLE |
|
294 | - ) |
|
295 | - ), |
|
296 | - null |
|
297 | - ); |
|
298 | - |
|
299 | - return (new DriverStatistic()) |
|
300 | - ->setSize($result->first()['cache_size']) |
|
301 | - ->setRawData([]) |
|
302 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
303 | - ->setInfo('The cache size represents only the cache data itself without counting data structures associated to the cache entries.'); |
|
304 | - } |
|
305 | - |
|
306 | - /** |
|
307 | - * @param array<string, mixed> $options |
|
308 | - * @return array<string, mixed>|Cassandra\ExecutionOptions |
|
309 | - */ |
|
310 | - protected function getCompatibleExecutionOptionsArgument(array $options): mixed |
|
311 | - { |
|
312 | - if ($this->getConfig()->isUseLegacyExecutionOptions()) { |
|
313 | - return new Cassandra\ExecutionOptions($options); |
|
314 | - } |
|
315 | - |
|
316 | - return $options; |
|
317 | - } |
|
280 | + } |
|
281 | + |
|
282 | + /** |
|
283 | + * @return DriverStatistic |
|
284 | + * @throws Exception |
|
285 | + */ |
|
286 | + public function getStats(): DriverStatistic |
|
287 | + { |
|
288 | + $result = $this->instance->execute( |
|
289 | + new Cassandra\SimpleStatement( |
|
290 | + sprintf( |
|
291 | + 'SELECT SUM(cache_length) as cache_size FROM %s.%s', |
|
292 | + self::CASSANDRA_KEY_SPACE, |
|
293 | + self::CASSANDRA_TABLE |
|
294 | + ) |
|
295 | + ), |
|
296 | + null |
|
297 | + ); |
|
298 | + |
|
299 | + return (new DriverStatistic()) |
|
300 | + ->setSize($result->first()['cache_size']) |
|
301 | + ->setRawData([]) |
|
302 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
303 | + ->setInfo('The cache size represents only the cache data itself without counting data structures associated to the cache entries.'); |
|
304 | + } |
|
305 | + |
|
306 | + /** |
|
307 | + * @param array<string, mixed> $options |
|
308 | + * @return array<string, mixed>|Cassandra\ExecutionOptions |
|
309 | + */ |
|
310 | + protected function getCompatibleExecutionOptionsArgument(array $options): mixed |
|
311 | + { |
|
312 | + if ($this->getConfig()->isUseLegacyExecutionOptions()) { |
|
313 | + return new Cassandra\ExecutionOptions($options); |
|
314 | + } |
|
315 | + |
|
316 | + return $options; |
|
317 | + } |
|
318 | 318 | } |
@@ -66,7 +66,8 @@ discard block |
||
66 | 66 | $sslBuilder = Cassandra::ssl(); |
67 | 67 | if (!empty($clientConfig->isSslVerify())) { |
68 | 68 | $sslBuilder->withVerifyFlags(Cassandra::VERIFY_PEER_CERT); |
69 | - } else { |
|
69 | + } |
|
70 | + else { |
|
70 | 71 | $sslBuilder->withVerifyFlags(Cassandra::VERIFY_NONE); |
71 | 72 | } |
72 | 73 | |
@@ -145,7 +146,8 @@ discard block |
||
145 | 146 | } |
146 | 147 | |
147 | 148 | return null; |
148 | - } catch (Exception $e) { |
|
149 | + } |
|
150 | + catch (Exception $e) { |
|
149 | 151 | return null; |
150 | 152 | } |
151 | 153 | } |
@@ -198,7 +200,8 @@ discard block |
||
198 | 200 | * been really upserted |
199 | 201 | */ |
200 | 202 | return $result instanceof Cassandra\Rows; |
201 | - } catch (InvalidArgumentException $e) { |
|
203 | + } |
|
204 | + catch (InvalidArgumentException $e) { |
|
202 | 205 | throw new PhpfastcacheInvalidArgumentException($e->getMessage(), 0, $e); |
203 | 206 | } |
204 | 207 | } |
@@ -236,7 +239,8 @@ discard block |
||
236 | 239 | * been really deleted |
237 | 240 | */ |
238 | 241 | return $result instanceof Cassandra\Rows; |
239 | - } catch (Exception $e) { |
|
242 | + } |
|
243 | + catch (Exception $e) { |
|
240 | 244 | return false; |
241 | 245 | } |
242 | 246 | } |
@@ -259,7 +263,8 @@ discard block |
||
259 | 263 | ); |
260 | 264 | |
261 | 265 | return true; |
262 | - } catch (Exception) { |
|
266 | + } |
|
267 | + catch (Exception) { |
|
263 | 268 | return false; |
264 | 269 | } |
265 | 270 | } |