Skip to content

Commit

Permalink
Implemented extended methods to Ravendb
Browse files Browse the repository at this point in the history
  • Loading branch information
Geolim4 committed Jan 11, 2024
1 parent 8174a0b commit 37b2643
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 16 deletions.
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
],
"require": {
"php": ">=8.0",
"phpfastcache/phpfastcache": "^9.2",
"ravendb/ravendb-php-client": "^5.2",
"symfony/var-dumper": "^6.0"
"phpfastcache/phpfastcache": "^9.2.2",
"ravendb/ravendb-php-client": "^5.2"
},
"require-dev": {
"phpmd/phpmd": "@stable",
Expand Down
86 changes: 74 additions & 12 deletions lib/Phpfastcache/Extensions/Drivers/Ravendb/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,23 @@
namespace Phpfastcache\Extensions\Drivers\Ravendb;

use Phpfastcache\Cluster\AggregatablePoolInterface;
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
use Phpfastcache\Core\Pool\TaggableCacheItemPoolTrait;
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
use Phpfastcache\Entities\DriverStatistic;
use Phpfastcache\Exceptions\PhpfastcacheDriverConnectException;
use Phpfastcache\Exceptions\PhpfastcacheDriverException;
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
use Phpfastcache\Exceptions\PhpfastcacheUnsupportedMethodException;
use RavenDB\Documents\DocumentStore;
use RavenDB\Documents\Operations\DeleteByQueryOperation;
use RavenDB\Documents\Queries\IndexQuery;
use RavenDB\Documents\Session\DocumentSessionInterface;
use RavenDB\Documents\Session\DocumentSession;
use RavenDB\Exceptions\RavenException;

/**
* Class Driver
* @property DocumentSessionInterface $instance Instance of driver service
* @property DocumentSession $instance Instance of driver service
* @method Config getConfig()
*/
class Driver implements AggregatablePoolInterface
Expand Down Expand Up @@ -89,7 +91,7 @@ protected function driverConnect(): bool
$this->documentStorage->getConventions()->setFindCollectionName(fn () => $this->getConfig()->getCollectionName());
$this->documentStorage->getConventions()->setFindIdentityProperty(static fn () => 'key');
$this->documentStorage->initialize();
$this->instance = $this->documentStorage->openSession();
$this->instance = $this->documentStorage->openSession();// @phpstan-ignore-line
} catch (RavenException $e) {
throw new PhpfastcacheDriverConnectException('Unable to connect to Raven server: ' . $e->getMessage());
}
Expand Down Expand Up @@ -137,20 +139,53 @@ protected function getRavenDocument(ExtendedCacheItemInterface $item): ?RavenPro
* @return array<array<string, mixed>>
* @throws PhpfastcacheDriverException
*/
// protected function driverReadMultiple(ExtendedCacheItemInterface ...$items): array
// {
// return [];
// }
protected function driverReadMultiple(ExtendedCacheItemInterface ...$items): array
{
$ravenDocuments = $this->instance->load(
RavenProxy::class,
$this->getKeys($items, false, $this->documentPrefix)
);

if (is_iterable($ravenDocuments)) {
$ravenDocuments = iterator_to_array($ravenDocuments);

return array_combine(
array_map(fn(?RavenProxy $ravenProxy) => $ravenProxy?->getKey(), $ravenDocuments),
array_map(fn(?RavenProxy $ravenProxy) => $ravenProxy?->toDriverArray(), $ravenDocuments),
);
}

return [];
}

/**
* @return array<int, string>
* @throws PhpfastcacheDriverException
* @throws PhpfastcacheInvalidArgumentException
*/
// protected function driverReadAllKeys(string $pattern = ''): iterable
// {
//
// }
protected function driverReadAllKeys(string $pattern = ''): iterable
{
$keys = [];
if (empty($this->documentPrefix)) {
throw new PhpfastcacheUnsupportedMethodException('A document prefix must be configured and not empty to be able to load all items from Raven.');
}
$results = $this->instance->loadStartingWith(
RavenProxy::class,
$this->documentPrefix,
$pattern,
0,
ExtendedCacheItemPoolInterface::MAX_ALL_KEYS_COUNT
);

if (is_iterable($results)) {
/** @var RavenProxy $item */
foreach (iterator_to_array($results) as $item) {
$keys[] = $item->getKey();
}
}

return $keys;
}

/**
* @param ExtendedCacheItemInterface $item
Expand Down Expand Up @@ -190,14 +225,41 @@ protected function driverDelete(string $key, string $encodedKey): bool
return true;
}

/**
* @param string[] $keys
* @return bool
*/
protected function driverDeleteMultiple(array $keys): bool
{
$this->documentStorage->operations()->send(
new DeleteByQueryOperation(
new IndexQuery(
sprintf(
"from %s where id() IN ('%s')",
$this->getConfig()->getCollectionName(),
implode("', '", array_map(fn (string $key) => $this->documentPrefix . $key, $keys))
)
)
)
);

foreach ($keys as $key) {
$this->instance->documentsById->remove($this->documentPrefix . $key);
}

return true;
}

/**
* @return bool
*/
protected function driverClear(): bool
{
$this->documentStorage->operations()->send(
new DeleteByQueryOperation(new IndexQuery(sprintf('from %s', $this->getConfig()->getCollectionName())))
new DeleteByQueryOperation(new IndexQuery(sprintf("from %s", $this->getConfig()->getCollectionName())))
);

$this->instance->clear();
return true;
}
}
2 changes: 1 addition & 1 deletion tests/Ravendb.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
try {
$cacheInstance = CacheManager::getInstance('Ravendb', include $configFileName);
$testHelper->runCRUDTests($cacheInstance);
// $testHelper->runGetAllItemsTests($cacheInstance);
$testHelper->runGetAllItemsTests($cacheInstance);
} catch (PhpfastcacheDriverConnectException $e) {
$testHelper->assertSkip('Ravendb server unavailable: ' . $e->getMessage());
$testHelper->terminateTest();
Expand Down

0 comments on commit 37b2643

Please sign in to comment.