Skip to content

Commit

Permalink
9.2 (WIP):
Browse files Browse the repository at this point in the history
- __Core__
  - Internal: Implemented multiple keys delete (*if supported by the backend*) to improve the performances behind all `deleteItems()` calls. Currently only supported in some backends, but it may evolve in the future.
  • Loading branch information
Geolim4 committed Dec 19, 2023
1 parent 5f3cae8 commit 6b60a53
Show file tree
Hide file tree
Showing 41 changed files with 347 additions and 151 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ local.properties
.idea
.gitignore
.idea/
.run/

# External tool builders
.externalToolBuilders/
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Configuration methods`ConfigurationOption::isPreventCacheSlams()`, `ConfigurationOption::setPreventCacheSlams()`, `ConfigurationOption::getCacheSlamsTimeout()`, `ConfigurationOption::setCacheSlamsTimeout()` are deprecated. ([See changes](CHANGELOG_API.md)).
- Fixed #907 // Internal "driver decode()" method will now throw an if the string data looks corrupted.
- Internal: Implemented multiple keys fetch (*if supported by the backend*) to improve the performances behind all `getItems()` calls. Currently only supported in some backends, but it may evolve in the future.
- Internal: Implemented multiple keys delete (*if supported by the backend*) to improve the performances behind all `deleteItems()` calls. Currently only supported in some backends, but it may evolve in the future.
- __Misc__
- Fixed multiple code typo & updated README.md

Expand Down
22 changes: 21 additions & 1 deletion docs/EVENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ The order of execution of the events is always the following:
- *ExtendedCacheItemPoolInterface::getItems()*
- *ExtendedCacheItemPoolInterface::getItemsByTag()*
- *ExtendedCacheItemPoolInterface::getItemsAsJsonString()*

- onCacheGetItems(*Callable* **$callback**)
- **Callback arguments**
- *ExtendedCacheItemPoolInterface* **$itemPool**
Expand All @@ -102,16 +103,34 @@ The order of execution of the events is always the following:
- *ExtendedCacheItemPoolInterface::getItems()*
- *ExtendedCacheItemPoolInterface::getItemsByTag()*
- *ExtendedCacheItemPoolInterface::getItemsAsJsonString()*

- onCacheDeleteItem(*Callable* **$callback**)
- **Callback arguments**
- *ExtendedCacheItemPoolInterface* **$itemPool**
- *ExtendedCacheItemInterface* **$item**
- **Scope**
- ItemPool
- **Description**
- Allow you to manipulate an item after being deleted. :exclamation: **Caution** The provided item is in pool detached-state.
- Allow you to manipulate an item after being deleted (this event is not fired if `deleteItems()` is called). :exclamation: **Caution** The provided item is in pool detached-state.
- **Risky Circular Methods**
- *ExtendedCacheItemPoolInterface::deleteItem()*
- *ExtendedCacheItemPoolInterface::deleteItems()*
- *ExtendedCacheItemPoolInterface::getItem()*
- *ExtendedCacheItemPoolInterface::getItems()*
- *ExtendedCacheItemPoolInterface::getItemsByTag()*
- *ExtendedCacheItemPoolInterface::getItemsAsJsonString()*

- onCacheDeleteItems(*Callable* **$callback**)
- **Callback arguments**
- *ExtendedCacheItemPoolInterface* **$itemPool**
- *ExtendedCacheItemInterface[]* **$items**
- **Scope**
- ItemPool
- **Description**
- Allow you to manipulate multiple items after being deleted. :exclamation: **Caution** The provided item is in pool detached-state.
- **Risky Circular Methods**
- *ExtendedCacheItemPoolInterface::deleteItem()*
- *ExtendedCacheItemPoolInterface::deleteItems()*
- *ExtendedCacheItemPoolInterface::getItem()*
- *ExtendedCacheItemPoolInterface::getItems()*
- *ExtendedCacheItemPoolInterface::getItemsByTag()*
Expand Down Expand Up @@ -203,6 +222,7 @@ The order of execution of the events is always the following:
- *ExtendedCacheItemPoolInterface::getItems()*
- *ExtendedCacheItemPoolInterface::getItemsByTag()*
- *ExtendedCacheItemPoolInterface::getItemsAsJsonString()*

- onCacheDriverChecked(*Callable* **$callback**)
- **Callback arguments**
- *ExtendedCacheItemPoolInterface* **$itemPool**
Expand Down
5 changes: 3 additions & 2 deletions lib/Phpfastcache/Cluster/ClusterPoolTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ protected function driverWrite(ExtendedCacheItemInterface $item): bool
}

/**
* @param ExtendedCacheItemInterface $item
* @param string $key
* @param string $encodedKey
* @return bool
*/
protected function driverDelete(ExtendedCacheItemInterface $item): bool
protected function driverDelete(string $key, string $encodedKey): bool
{
return true;
}
Expand Down
61 changes: 51 additions & 10 deletions lib/Phpfastcache/Core/Pool/CacheItemPoolTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public function getItems(array $keys = []): iterable
} else {
$item->expiresAfter((int) abs($this->getConfig()->getDefaultTtl()));
}
$item->isHit() ? $this->getIO()->incReadHit() : $this->getIO()->incReadMiss();
}
}
} else {
Expand Down Expand Up @@ -316,15 +317,39 @@ public function clear(): bool
*/
public function deleteItems(array $keys): bool
{
$return = true;
foreach ($keys as $key) {
$result = $this->deleteItem($key);
if ($result !== true) {
$return = false;
if (count($keys) > 1) {
$return = true;
try {
$items = $this->getItems($keys);
$return = $this->driverDeleteMultiple($keys);
foreach ($items as $item) {
$item->setHit(false);

if (!\str_starts_with($item->getKey(), TaggableCacheItemPoolInterface::DRIVER_TAGS_KEY_PREFIX)) {
$this->cleanItemTags($item);
}
}
$this->getIO()->incWriteHit();
$this->eventManager->dispatch(Event::CACHE_DELETE_ITEMS, $this, $items);
$this->deregisterItems($keys);
} catch (PhpfastcacheUnsupportedMethodException) {
foreach ($keys as $key) {
$result = $this->deleteItem($key);
if ($result !== true) {
$return = false;
}
}
}

return $return;
}

$index = array_key_first($keys);
if ($index !== null) {
return $this->deleteItem($keys[$index]);
}

return $return;
return false;
}

/**
Expand All @@ -338,7 +363,8 @@ public function deleteItems(array $keys): bool
public function deleteItem(string $key): bool
{
$item = $this->getItem($key);
if ($item->isHit() && $this->driverDelete($item)) {
if ($item->isHit()) {
$result = $this->driverDelete($item->getKey(), $item->getEncodedKey());
$item->setHit(false);
$this->getIO()->incWriteHit();

Expand All @@ -357,7 +383,7 @@ public function deleteItem(string $key): bool
$this->cleanItemTags($item);
}

return true;
return $result;
}

return false;
Expand Down Expand Up @@ -397,8 +423,8 @@ public function commit(): bool
$return = true;
foreach ($this->deferredList as $key => $item) {
$result = $this->save($item);
unset($this->deferredList[$key]);
if ($result !== true) {
unset($this->deferredList[$key]);
$return = $result;
}
}
Expand Down Expand Up @@ -494,6 +520,21 @@ protected function deregisterItem(string $itemKey): static
return $this;
}

/**
* @param string[] $itemKeys
* @internal This method de-register multiple items from $this->itemInstances
*/
protected function deregisterItems(array $itemKeys): static
{
$this->itemInstances = array_diff_key($this->itemInstances, array_flip($itemKeys));

if (\gc_enabled()) {
\gc_collect_cycles();
}

return $this;
}

/**
* @throws PhpfastcacheLogicException
*/
Expand Down Expand Up @@ -547,7 +588,7 @@ protected function handleExpiredCacheItem(ExtendedCacheItemInterface $item): voi
* As we MUST return an item in any
* way, we do not de-register here
*/
$this->driverDelete($item);
$this->driverDelete($item->getKey(), $item->getEncodedKey());

/**
* Reset the Item
Expand Down
1 change: 0 additions & 1 deletion lib/Phpfastcache/Core/Pool/DriverBaseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ public function __construct(#[\SensitiveParameter] ConfigurationOptionInterface
throw new PhpfastcacheDriverConnectException(
sprintf(
ExtendedCacheItemPoolInterface::DRIVER_CONNECT_FAILURE,
$e::class,
$this->getDriverName(),
$e->getMessage(),
$e->getLine() ?: 'unknown line',
Expand Down
30 changes: 28 additions & 2 deletions lib/Phpfastcache/Core/Pool/DriverPoolAbstractTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,36 @@ protected function driverReadAllKeys(string $pattern = ''): iterable
abstract protected function driverWrite(ExtendedCacheItemInterface $item): bool;

/**
* @param ExtendedCacheItemInterface $item
* @param ExtendedCacheItemInterface ...$item
* @return bool
* @throws PhpfastcacheUnsupportedMethodException
*/
protected function driverWriteMultiple(ExtendedCacheItemInterface ...$item): bool
{
/**
* @todo Implement bulk writes to be for v10:
* For methods commit() and saveMultiple()
*/
throw new PhpfastcacheUnsupportedMethodException();
}


/**
* @param string $key
* @param string $encodedKey
* @return bool
*/
abstract protected function driverDelete(ExtendedCacheItemInterface $item): bool;
abstract protected function driverDelete(string $key, string $encodedKey): bool;

/**
* @param string[] $keys
* @return bool
* @throws PhpfastcacheUnsupportedMethodException
*/
protected function driverDeleteMultiple(array $keys): bool
{
throw new PhpfastcacheUnsupportedMethodException();
}

/**
* @return bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ interface ExtendedCacheItemPoolInterface extends CacheItemPoolInterface, EventMa
public const DRIVER_CHECK_FAILURE = '%s is not installed or is misconfigured, cannot continue.
Also, please verify the suggested dependencies in composer because as of the V6, 3rd party libraries are no longer required.';

public const DRIVER_CONNECT_FAILURE = '(%s) %s failed to connect with the following error message: "%s" line %d in %s';
public const DRIVER_CONNECT_FAILURE = '%s failed to connect with the following error message: "%s" line %d in %s';

public const DRIVER_KEY_WRAPPER_INDEX = 'k';

Expand Down
4 changes: 3 additions & 1 deletion lib/Phpfastcache/Core/Pool/TaggableCacheItemPoolTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,9 @@ protected function driverUnwrapTags(array $wrapper): array
*/
protected function cleanItemTags(ExtendedCacheItemInterface $item): void
{
$this->driverWriteTags($item->removeTags($item->getTags()));
if (!empty($item->getTags()) || !empty($item->getRemovedTags())) {
$this->driverWriteTags($item->removeTags($item->getTags()));
}
}

/**
Expand Down
9 changes: 4 additions & 5 deletions lib/Phpfastcache/Drivers/Apcu/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,13 @@ protected function driverRead(ExtendedCacheItemInterface $item): ?array
}

/**
* @param ExtendedCacheItemInterface $item
* @param string $key
* @param string $encodedKey
* @return bool
* @throws PhpfastcacheInvalidArgumentException
*/
protected function driverDelete(ExtendedCacheItemInterface $item): bool
protected function driverDelete(string $key, string $encodedKey): bool
{

return (bool)apcu_delete($item->getKey());
return (bool)apcu_delete($key);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions lib/Phpfastcache/Drivers/Arangodb/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,18 @@ protected function driverWrite(ExtendedCacheItemInterface $item): bool
}

/**
* @param ExtendedCacheItemInterface $item
* @param string $key
* @param string $encodedKey
* @return bool
*/
protected function driverDelete(ExtendedCacheItemInterface $item): bool
protected function driverDelete(string $key, string $encodedKey): bool
{
$options = [
'returnOld' => false
];

try {
$this->documentHandler->removeById($this->getConfig()->getCollection(), $item->getEncodedKey(), null, $options);
$this->documentHandler->removeById($this->getConfig()->getCollection(), $encodedKey, null, $options);
return true;
} catch (ArangoException) {
return false;
Expand Down
8 changes: 4 additions & 4 deletions lib/Phpfastcache/Drivers/Cassandra/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,18 +204,18 @@ protected function driverWrite(ExtendedCacheItemInterface $item): bool
}

/**
* @param ExtendedCacheItemInterface $item
* @param string $key
* @param string $encodedKey
* @return bool
* @throws PhpfastcacheInvalidArgumentException
*/
protected function driverDelete(ExtendedCacheItemInterface $item): bool
protected function driverDelete(string $key, string $encodedKey): bool
{

try {
$options = $this->getCompatibleExecutionOptionsArgument(
[
'arguments' => [
'cache_id' => $item->getKey(),
'cache_id' => $key,
],
]
);
Expand Down
8 changes: 4 additions & 4 deletions lib/Phpfastcache/Drivers/Couchbasev3/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ protected function driverWrite(ExtendedCacheItemInterface $item): bool
}

/**
* @param ExtendedCacheItemInterface $item
* @param string $key
* @param string $encodedKey
* @return bool
* @throws PhpfastcacheInvalidArgumentException
*/
protected function driverDelete(ExtendedCacheItemInterface $item): bool
protected function driverDelete(string $key, string $encodedKey): bool
{

try {
$this->getCollection()->remove($item->getEncodedKey());
$this->getCollection()->remove($encodedKey);
return true;
} catch (DocumentNotFoundException) {
return true;
Expand Down
15 changes: 10 additions & 5 deletions lib/Phpfastcache/Drivers/Couchdb/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,12 @@ protected function createDatabase(): void

protected function getCouchDbItemKey(ExtendedCacheItemInterface $item): string
{
return 'pfc_' . $item->getEncodedKey();
return $this->getCouchDbKey($item->getEncodedKey());
}

protected function getCouchDbKey(string $encodedKey): string
{
return 'pfc_' . $encodedKey;
}

/**
Expand Down Expand Up @@ -203,16 +208,16 @@ protected function getLatestDocumentRevision(string $docId): ?string
}

/**
* @param ExtendedCacheItemInterface $item
* @param string $key
* @param string $encodedKey
* @return bool
* @throws PhpfastcacheDriverException
* @throws PhpfastcacheInvalidArgumentException
*/
protected function driverDelete(ExtendedCacheItemInterface $item): bool
protected function driverDelete(string $key, string $encodedKey): bool
{

try {
$this->instance->deleteDocument($this->getCouchDbItemKey($item), $this->getLatestDocumentRevision($this->getCouchDbItemKey($item)));
$this->instance->deleteDocument($this->getCouchDbKey($encodedKey), $this->getLatestDocumentRevision($this->getCouchDbKey($encodedKey)));
} catch (CouchDBException $e) {
throw new PhpfastcacheDriverException('Got error while trying to delete a document: ' . $e->getMessage(), 0, $e);
}
Expand Down
7 changes: 3 additions & 4 deletions lib/Phpfastcache/Drivers/Devnull/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,12 @@ protected function driverRead(CacheItemInterface $item): ?array
}

/**
* @param ExtendedCacheItemInterface $item
* @param string $key
* @param string $encodedKey
* @return bool
* @throws PhpfastcacheInvalidArgumentException
*/
protected function driverDelete(ExtendedCacheItemInterface $item): bool
protected function driverDelete(string $key, string $encodedKey): bool
{

return true;
}

Expand Down
Loading

0 comments on commit 6b60a53

Please sign in to comment.