Skip to content

Commit

Permalink
Properly replace the URI->ID and ID->URI mappings when the new ones a…
Browse files Browse the repository at this point in the history
…re empty (#204)

Fixes #198
  • Loading branch information
Voltra authored Jan 7, 2025
1 parent 6d66577 commit 546de55
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions src/Services/PageRoutesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
Expand All @@ -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);
}
}
}

0 comments on commit 546de55

Please sign in to comment.