Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce StorageItem::setItems for CacheItemPoolDecorator::commit #57

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ All notable changes to this project will be documented in this file, in reverse

### Changed

- Nothing.
- [#57](https://github.com/laminas/laminas-cache/pull/57) Internal cache handling of `CacheItemPoolDecorator` is changed to use `StorageInterface::setItems` instead of `StorageInterface::setItem`. This will result in a faster store procedure when multiple deferred cache items needs to be stored.

### Deprecated

Expand Down
128 changes: 85 additions & 43 deletions src/Psr/CacheItemPool/CacheItemPoolDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,18 @@
use function array_diff_key;
use function array_flip;
use function array_keys;
use function array_map;
use function array_merge;
use function array_unique;
use function array_walk;
use function current;
use function get_class;
use function gettype;
use function in_array;
use function is_string;
use function preg_match;
use function sprintf;
use function var_export;

/**
* Decorate laminas-cache adapters as PSR-6 cache item pools.
Expand Down Expand Up @@ -82,9 +89,9 @@ public function getItem($key)
}

return new CacheItem($key, $value, $isHit);
} else {
return clone $this->deferred[$key];
}

return clone $this->deferred[$key];
}

/**
Expand Down Expand Up @@ -162,7 +169,8 @@ public function clear()
$this->deferred = [];

try {
$namespace = $this->storage->getOptions()->getNamespace();
$options = $this->storage->getOptions();
$namespace = $options->getNamespace();
if ($this->storage instanceof ClearByNamespaceInterface && $namespace) {
$cleared = $this->storage->clearByNamespace($namespace);
} else {
Expand Down Expand Up @@ -212,40 +220,7 @@ public function save(CacheItemInterface $item)
throw new InvalidArgumentException('$item must be an instance of ' . CacheItem::class);
snapshotpl marked this conversation as resolved.
Show resolved Hide resolved
}

$itemTtl = $item->getTtl();

// delete expired item
if ($itemTtl < 0) {
$this->deleteItem($item->getKey());
$item->setIsHit(false);
return false;
}

$saved = true;
$options = $this->storage->getOptions();
$ttl = $options->getTtl();

try {
// get item value and serialize, if required
$value = $item->get();

// reset TTL on adapter, if required
if ($itemTtl > 0) {
$options->setTtl($itemTtl);
}

$saved = $this->storage->setItem($item->getKey(), $value);
// saved items are a hit? see integration test CachePoolTest::testIsHit()
$item->setIsHit($saved);
} catch (Exception\InvalidArgumentException $e) {
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
} catch (Exception\ExceptionInterface $e) {
$saved = false;
} finally {
$options->setTtl($ttl);
}

return $saved;
return $this->saveMultipleItems([$item], $item->getTtl()) === [];
}

/**
Expand All @@ -269,14 +244,22 @@ public function saveDeferred(CacheItemInterface $item)
*/
public function commit()
{
$notSaved = [];
/** @var array<string,non-empty-array<string,CacheItem>> $groupedByTtl */
$groupedByTtl = [];
foreach ($this->deferred as $cacheKey => $item) {
$itemTtl = var_export($item->getTtl(), true);
$group = $groupedByTtl[$itemTtl] ?? [];
$group[$cacheKey] = $item;
$groupedByTtl[$itemTtl] = $group;
}

foreach ($this->deferred as &$item) {
if (! $this->save($item)) {
$notSaved[] = $item;
}
$notSavedItems = [];
foreach ($groupedByTtl as $keyValuePairs) {
$itemTtl = current($keyValuePairs)->getTtl();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could use groupedByTtl key here aswell.
But as null is an invalid array-key, I'd prefer not to have an additional if here.

$notSavedItems[] = $this->saveMultipleItems($keyValuePairs, $itemTtl);
}
$this->deferred = $notSaved;

$this->deferred = array_unique(array_merge([], ...$notSavedItems));

return empty($this->deferred);
}
Expand All @@ -285,6 +268,7 @@ public function commit()
* Throws exception is storage is not compatible with PSR-6
*
* @throws CacheException
* @psalm-assert-if-true StorageInterface&FlushableInterface $storage
*/
private function validateStorage(StorageInterface $storage)
{
Expand Down Expand Up @@ -373,4 +357,62 @@ private function validateKeys($keys)
$this->validateKey($key);
}
}

/**
* @param array<int,CacheItem> $items
* @psalm-param list<CacheItem> $items
* @return array<string,CacheItem>
*/
private function saveMultipleItems(array $items, ?int $itemTtl): array
{
// delete expired item
if ($itemTtl < 0) {
$this->deleteItems(array_map(static function (CacheItemInterface $item): string {
return $item->getKey();
}, $items));
array_walk($items, static function (CacheItem $cacheItem): void {
$cacheItem->setIsHit(false);
});

return $items;
}

$options = $this->storage->getOptions();
$ttl = $options->getTtl();

$keyValuePair = [];
$keyItemPair = [];
foreach ($items as $item) {
$key = $item->getKey();
$keyValuePair[$key] = $item->get();
$keyItemPair[$key] = $item;
}

$options->setTtl($itemTtl ?? 0);

$notSavedKeys = [];
try {
$notSavedKeys = $this->storage->setItems($keyValuePair);
} catch (Exception\InvalidArgumentException $e) {
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
} catch (Exception\ExceptionInterface $e) {
foreach (array_keys($keyValuePair) as $key) {
$notSavedKeys[] = $key;
}
} finally {
$options->setTtl($ttl);
}

$notSavedItems = [];
foreach ($keyItemPair as $key => $item) {
if (in_array($key, $notSavedKeys, true)) {
$notSavedItems[] = $item;
continue;
}

$item->setIsHit(true);
}

return $notSavedItems;
}
}
Loading