From f3e545057da1689eca68df3726c5b9f8ee5bd6da Mon Sep 17 00:00:00 2001 From: Namekuji Date: Fri, 4 Aug 2023 01:21:56 -0400 Subject: [PATCH] refactor: reduce one redis flight --- packages/backend/src/misc/cache.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/backend/src/misc/cache.ts b/packages/backend/src/misc/cache.ts index 2f979a78bf..26301939ef 100644 --- a/packages/backend/src/misc/cache.ts +++ b/packages/backend/src/misc/cache.ts @@ -148,12 +148,13 @@ class SetCache { protected async fetch() { // Sync from DB if nothing is cached yet or cache is expired const ttlKey = `${this.key}:fetched`; - const fetched = await redisClient.exists(ttlKey); - - if (!(await this.hasFollowing()) || fetched === 0) { + if ( + !(await this.hasFollowing()) || + (await redisClient.exists(ttlKey)) === 0 + ) { await redisClient.del(this.key); await this.follow(...(await this.fetcher())); - await redisClient.set(ttlKey, "yes", "EX", 60 * 30); + await redisClient.set(ttlKey, "yes", "EX", 60 * 30); // Expires in 30 minutes } }