From 546de555e91a3a334aec4d4f1805fcea733e18af Mon Sep 17 00:00:00 2001 From: Voltra Date: Tue, 7 Jan 2025 11:42:16 +0100 Subject: [PATCH] Properly replace the URI->ID and ID->URI mappings when the new ones are empty (#204) Fixes #198 --- src/Services/PageRoutesService.php | 36 +++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/src/Services/PageRoutesService.php b/src/Services/PageRoutesService.php index 0f27f24..e914371 100644 --- a/src/Services/PageRoutesService.php +++ b/src/Services/PageRoutesService.php @@ -314,11 +314,19 @@ protected function forgetPageLocalCache(Page $page) */ protected function replaceIdToUriMapping(array $idToUriMapping): void { - // Replace the ID -> URI[] mapping with the given one. - // This is done "atomically" with regards to the cache. - // Note that concurrent read and writes can result in lost updates. - // And thus in an invalid state. - Cache::forever(static::ID_TO_URI_MAPPING, $idToUriMapping); + if (empty($idToUriMapping)) { + // If the new mapping is empty, that means we've been + // cleaning the last entries. Therefore we must + // forget the cached data to properly clear it out + // and also allow proper cache invalidation + Cache::forget(static::ID_TO_URI_MAPPING); + } else { + // Replace the ID -> URI[] mapping with the given one. + // This is done "atomically" with regards to the cache. + // Note that concurrent read and writes can result in lost updates. + // And thus in an invalid state. + Cache::forever(static::ID_TO_URI_MAPPING, $idToUriMapping); + } } /** @@ -328,10 +336,18 @@ protected function replaceIdToUriMapping(array $idToUriMapping): void */ protected function replaceUriToIdMapping(array $uriToIdMapping): void { - // Replace the URI -> ID mapping with the given one. - // This is done "atomically" with regards to the cache. - // Note that concurrent read and writes can result in lost updates. - // And thus in an invalid state. - Cache::forever(static::URI_TO_ID_MAPPING, $uriToIdMapping); + if (empty($uriToIdMapping)) { + // If the new mapping is empty, that means we've been + // cleaning the last entries. Therefore we must + // forget the cached data to properly clear it out + // and also allow proper cache invalidation + Cache::forget(static::URI_TO_ID_MAPPING); + } else { + // Replace the URI -> ID mapping with the given one. + // This is done "atomically" with regards to the cache. + // Note that concurrent read and writes can result in lost updates. + // And thus in an invalid state. + Cache::forever(static::URI_TO_ID_MAPPING, $uriToIdMapping); + } } }