From ce1d9d6c1d33aa77c7273e22cc8a65b61ab8481a Mon Sep 17 00:00:00 2001 From: Markus Rudolph Date: Thu, 20 Jun 2024 12:02:55 +0200 Subject: [PATCH] Use a shorter cache lookup strategy --- hugo/content/docs/recipes/utilities/caches.md | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/hugo/content/docs/recipes/utilities/caches.md b/hugo/content/docs/recipes/utilities/caches.md index 77350606..93cf7edf 100644 --- a/hugo/content/docs/recipes/utilities/caches.md +++ b/hugo/content/docs/recipes/utilities/caches.md @@ -76,12 +76,9 @@ export class CachedInferPublisherService extends UncachedInferPublisherService { } override inferPublisher(person: Person): KnownPublisher|undefined { const documentUri = AstUtils.getDocument(person).uri; - if(this.cache.has(documentUri, person)) { - return this.cache.get(documentUri, person)!; - } - const publisher = super.inferPublisher(person); - this.cache.set(documentUri, person, publisher); - return publisher; + //get cache entry for the documentUri and the person + //if it does not exist, calculate the value and store it + return this.cache.get(documentUri, person, () => super.inferPublisher(person)); } } ``` @@ -210,12 +207,7 @@ export class CachedInferPublisherService extends UncachedInferPublisherService { } override inferPublisher(person: Person): KnownPublisher|undefined { const documentUri = AstUtils.getDocument(person).uri; - if(this.cache.has(documentUri, person)) { - return this.cache.get(documentUri, person)!; - } - const publisher = super.inferPublisher(person); - this.cache.set(documentUri, person, publisher); - return publisher; + return this.cache.get(documentUri, person, () => super.inferPublisher(person)); } } ```