Skip to content

Commit 649fc6b

Browse files
authored
Optimized Couchdb document storage (#810)
* Upgraded travis dist to repair broken php8 tests * Trying to repair broken build * Trying to repair broken build * Trying to repair broken build * Fixed Couchdb tests & driver * Removed dump... * Fixed tests for php 7.x ... * Fixed tests & dropped support of Riak which is became unmaintenable and is globally unused * Refactored some tests and isolated TestHelper out of core code * Optimized Couchdb document storage * Updated github action Updated composer.lock * Updated github action * Fixed Couchdb test
1 parent 2bed296 commit 649fc6b

File tree

4 files changed

+64
-14
lines changed

4 files changed

+64
-14
lines changed

.github/workflows/tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: Github tests workflow
2-
on: [push, pull_request_target]
2+
on: [push, pull_request]
33
jobs:
44
build:
55
name: Github PHP unit tests
@@ -18,7 +18,7 @@ jobs:
1818
uses: zhulik/redis-action@v1.0.0
1919
with:
2020
redis version: '5'
21-
- uses: actions/checkout@v1
21+
- uses: actions/checkout@v2
2222

2323
- name: Validate composer.json and composer.lock
2424
run: composer validate

composer.lock

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Phpfastcache/Drivers/Couchdb/Driver.php

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,12 @@ protected function driverRead(CacheItemInterface $item)
145145
throw new PhpfastcacheDriverException('Got error while trying to get a document: ' . $e->getMessage(), 0, $e);
146146
}
147147

148-
if ($response->status === 404 || empty($response->body['data'])) {
148+
if ($response->status === 404 || empty($response->body[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX])) {
149149
return null;
150150
}
151151

152152
if ($response->status === 200) {
153-
return $this->decode($response->body['data']);
153+
return $this->decode($response->body);
154154
}
155155

156156
throw new PhpfastcacheDriverException('Got unexpected HTTP status: ' . $response->status);
@@ -170,7 +170,7 @@ protected function driverWrite(CacheItemInterface $item): bool
170170
if ($item instanceof Item) {
171171
try {
172172
$this->instance->putDocument(
173-
['data' => $this->encode($this->driverPreWrap($item))],
173+
$this->encodeDocument($this->driverPreWrap($item)),
174174
$this->getCouchDbItemKey($item),
175175
$this->getLatestDocumentRevision($this->getCouchDbItemKey($item))
176176
);
@@ -247,4 +247,50 @@ protected function driverClear(): bool
247247

248248
return true;
249249
}
250+
251+
/**
252+
* @param array $data
253+
* @return array
254+
*/
255+
protected function encodeDocument(array $data): array
256+
{
257+
$data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX] = $this->encode($data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX]);
258+
259+
return $data;
260+
}
261+
262+
/**
263+
* Specific document decoder for Couchdb
264+
* since we dont store encoded version
265+
* for performance purposes
266+
*
267+
* @param $value
268+
* @return mixed
269+
* @throws \Exception
270+
*/
271+
protected function decode($value)
272+
{
273+
$value[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX] = \unserialize($value[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX], ['allowed_classes' => true]);
274+
275+
$value[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] = new \DateTime(
276+
$value[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX]['date'],
277+
new \DateTimeZone($value[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX]['timezone'])
278+
);
279+
280+
if(isset($value[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX])){
281+
$value[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = new \DateTime(
282+
$value[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX]['date'],
283+
new \DateTimeZone($value[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX]['timezone'])
284+
);
285+
}
286+
287+
if(isset($value[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX])){
288+
$value[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = new \DateTime(
289+
$value[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX]['date'],
290+
new \DateTimeZone($value[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX]['timezone'])
291+
);
292+
}
293+
294+
return $value;
295+
}
250296
}

tests/Couchdb.test.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
try{
1919
$cacheInstance = CacheManager::getInstance('Couchdb', $config);
2020
} catch (PhpfastcacheDriverConnectException $e){
21-
$testHelper->printDebugText('Unable to connect to Couchdb as an anynymous, trying with default credential...');
22-
$config->setUsername('admin');
23-
$config->setPassword('travis');
24-
$cacheInstance = CacheManager::getInstance('Couchdb', $config);
25-
} catch(PhpfastcacheDriverConnectException $e){
26-
$testHelper->assertSkip('Couchdb server unavailable: ' . $e->getMessage());
27-
$testHelper->terminateTest();
21+
try{
22+
$testHelper->printDebugText('Unable to connect to Couchdb as an anynymous, trying with default credential...');
23+
$config->setUsername('admin');
24+
$config->setPassword('travis');
25+
$cacheInstance = CacheManager::getInstance('Couchdb', $config);
26+
} catch(PhpfastcacheDriverConnectException $e){
27+
$testHelper->assertSkip('Couchdb server unavailable: ' . $e->getMessage());
28+
$testHelper->terminateTest();
29+
}
2830
}
2931

3032
$testHelper->runCRUDTests($cacheInstance);

0 commit comments

Comments
 (0)