refactor: reduce one redis flight

This commit is contained in:
Namekuji 2023-08-04 01:21:56 -04:00
parent d89e24f796
commit f3e545057d
No known key found for this signature in database
GPG key ID: 1D62332C07FBA532

View file

@ -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
}
}