cache persistence improvement, only update keys which have changed. More efficient, and less likely to cause race conditions.

This commit is contained in:
2026-03-12 17:15:49 -04:00
parent a0dc1a6049
commit 6ed1ab26f1
2 changed files with 69 additions and 55 deletions
+9 -5
View File
@@ -36,13 +36,17 @@ final readonly class PersistCache {
/** @throws InvalidArgumentException */
public function persist(): void {
/* we only need to persist the caches if they contain changes */
if ($this->sessionCache->isDirty()) {
/* we only need to persist the changes made to the cache (if any) */
$changes = $this->sessionCache->getChanges();
if ($changes) {
$this->sessionCache->markClean();
$items = $this->sessionCache->getItems($this->sessionCache->getKeys());
$this->sessionStorage->clear();
$items = $this->sessionCache->getItems(array_keys($changes));
foreach ($items as $item) {
$this->sessionStorage->saveDeferred($item);
if (($changes[$item->getKey()] ?? null) === MonitorCacheKeys::REMOVED) {
$this->sessionStorage->deleteItem($item->getKey());
} else {
$this->sessionStorage->saveDeferred($item);
}
}
$this->sessionStorage->commit();
}