From b8f013a39248215fdcd1bdb4961595fd88dc8259 Mon Sep 17 00:00:00 2001 From: Guillaume Date: Wed, 17 Jan 2024 16:47:29 +0100 Subject: [PATCH] :sparkles: Add `TDBMService::clearBeanCache` method This method can be used to cleanup every bean so long-running process won't keep old data. --- src/NativeWeakrefObjectStorage.php | 8 ++++++++ src/ObjectStorageInterface.php | 5 +++++ src/TDBMService.php | 10 ++++++++++ 3 files changed, 23 insertions(+) diff --git a/src/NativeWeakrefObjectStorage.php b/src/NativeWeakrefObjectStorage.php index b2265d0b..2d6c1d48 100644 --- a/src/NativeWeakrefObjectStorage.php +++ b/src/NativeWeakrefObjectStorage.php @@ -94,6 +94,14 @@ public function remove(string $tableName, $id): void unset($this->objects[$tableName][$id]); } + /** + * Removes all objects from the storage. + */ + public function clear(): void + { + $this->objects = array(); + } + private function cleanupDanglingWeakRefs(): void { foreach ($this->objects as $tableName => $table) { diff --git a/src/ObjectStorageInterface.php b/src/ObjectStorageInterface.php index 53064bec..f762e604 100644 --- a/src/ObjectStorageInterface.php +++ b/src/ObjectStorageInterface.php @@ -41,4 +41,9 @@ public function get(string $tableName, $id): ?DbRow; * @param string|int $id */ public function remove(string $tableName, $id): void; + + /** + * Removes all objects from the storage. + */ + public function clear(): void; } diff --git a/src/TDBMService.php b/src/TDBMService.php index ecfa1be5..a1ff6a14 100644 --- a/src/TDBMService.php +++ b/src/TDBMService.php @@ -1559,4 +1559,14 @@ public function setLogLevel(string $level): void { $this->logger = new LevelFilter($this->rootLogger, $level); } + + /** + * Clear TDBM's bean cache + * + * This can be used in long-running processes to cleanup everything. + */ + public function clearBeanCache(): void + { + $this->objectStorage->clear(); + } }