|
24 | 24 | use Phpfastcache\Core\Pool\TaggableCacheItemPoolTrait;
|
25 | 25 | use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
|
26 | 26 | use Phpfastcache\Entities\DriverStatistic;
|
| 27 | +use Phpfastcache\Event\EventReferenceParameter; |
27 | 28 | use Phpfastcache\Exceptions\PhpfastcacheDriverException;
|
28 | 29 | use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
|
29 | 30 | use Phpfastcache\Exceptions\PhpfastcacheLogicException;
|
@@ -95,13 +96,16 @@ protected function driverConnect(): bool
|
95 | 96 | $url .= ":{$clientConfig->getPort()}";
|
96 | 97 | $url .= '/' . \urlencode($this->getDatabaseName());
|
97 | 98 |
|
98 |
| - $this->instance = CouchDBClient::create( |
99 |
| - [ |
100 |
| - 'dbname' => $this->getDatabaseName(), |
101 |
| - 'url' => $url, |
102 |
| - 'timeout' => $clientConfig->getTimeout(), |
103 |
| - ] |
104 |
| - ); |
| 99 | + $options = [ |
| 100 | + 'dbname' => $this->getDatabaseName(), |
| 101 | + 'url' => $url, |
| 102 | + 'timeout' => $clientConfig->getTimeout(), |
| 103 | + 'headers' => [] |
| 104 | + ]; |
| 105 | + |
| 106 | + $this->eventManager->dispatch(Event::COUCHDB_CREATE_OPTIONS, $this, new EventReferenceParameter($options)); |
| 107 | + |
| 108 | + $this->instance = CouchDBClient::create($options); |
105 | 109 |
|
106 | 110 | $this->createDatabase();
|
107 | 111 |
|
@@ -164,11 +168,66 @@ protected function driverRead(ExtendedCacheItemInterface $item): ?array
|
164 | 168 | throw new PhpfastcacheDriverException('Got unexpected HTTP status: ' . $response->status);
|
165 | 169 | }
|
166 | 170 |
|
| 171 | + /** |
| 172 | + * @param ExtendedCacheItemInterface ...$items |
| 173 | + * @return array<array<string, mixed>> |
| 174 | + * @throws PhpfastcacheDriverException |
| 175 | + */ |
| 176 | + protected function driverReadMultiple(ExtendedCacheItemInterface ...$items): array |
| 177 | + { |
| 178 | + $response = $this->instance->findDocuments( |
| 179 | + array_map( |
| 180 | + fn(string $key) => $this->getCouchDbKey($key), |
| 181 | + $this->getKeys($items, true) |
| 182 | + ) |
| 183 | + ); |
| 184 | + |
| 185 | + if ($response->status === 404 || empty($response->body['rows'])) { |
| 186 | + return []; |
| 187 | + } |
| 188 | + |
| 189 | + if ($response->status === 200) { |
| 190 | + $driverArrays = []; |
| 191 | + foreach ($response->body['rows'] as $row) { |
| 192 | + if (isset($row['doc'])) { |
| 193 | + $doc = $this->decodeDocument($row['doc']); |
| 194 | + $driverArrays[$doc[self::DRIVER_KEY_WRAPPER_INDEX]] = $doc; |
| 195 | + } |
| 196 | + } |
| 197 | + return $driverArrays; |
| 198 | + } |
| 199 | + |
| 200 | + throw new PhpfastcacheDriverException('Got unexpected HTTP status: ' . $response->status); |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * @return array<int, string> |
| 205 | + * @throws PhpfastcacheDriverException |
| 206 | + * @throws PhpfastcacheInvalidArgumentException |
| 207 | + */ |
| 208 | + protected function driverReadAllKeys(string $pattern = ''): iterable |
| 209 | + { |
| 210 | + if ($pattern !== '') { |
| 211 | + $this->throwUnsupportedDriverReadAllPattern(); |
| 212 | + } |
| 213 | + |
| 214 | + $response = $this->instance->allDocs(ExtendedCacheItemPoolInterface::MAX_ALL_KEYS_COUNT); |
| 215 | + |
| 216 | + if ($response->status === 404 || empty($response->body['rows'])) { |
| 217 | + return []; |
| 218 | + } |
| 219 | + |
| 220 | + if ($response->status === 200) { |
| 221 | + return array_map(static fn(array $row) => $row['doc'][self::DRIVER_KEY_WRAPPER_INDEX], $response->body['rows']); |
| 222 | + } |
| 223 | + |
| 224 | + throw new PhpfastcacheDriverException('Got unexpected HTTP status: ' . $response->status); |
| 225 | + } |
| 226 | + |
167 | 227 | /**
|
168 | 228 | * @param ExtendedCacheItemInterface $item
|
169 | 229 | * @return bool
|
170 | 230 | * @throws PhpfastcacheDriverException
|
171 |
| - * @throws PhpfastcacheInvalidArgumentException |
172 | 231 | * @throws PhpfastcacheLogicException
|
173 | 232 | */
|
174 | 233 | protected function driverWrite(ExtendedCacheItemInterface $item): bool
|
|
0 commit comments