Skip to content

Commit b1af840

Browse files
committed
Cleanup TTL
1 parent f81b537 commit b1af840

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

src/services/in-memory-cache.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
1+
import { HOUR_MS } from "../utils/time";
12
import type { Cache } from "./cache";
23

4+
const TTL = HOUR_MS;
5+
36
export function createInMemoryCache(): Cache {
47
let cache: Record<string, any> = Object.create(null);
8+
let ttl: Record<string, number> = Object.create(null);
59

610
return {
711
get: async (key: string) => {
12+
if (ttl[key] && Date.now() > ttl[key]) {
13+
delete cache[key];
14+
delete ttl[key];
15+
}
816
return cache[key];
917
},
1018
set: async (key: string, value: any) => {
1119
cache[key] = value;
20+
ttl[key] = Date.now() + TTL;
1221
},
1322
};
1423
}

src/services/redis-cache.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { DAY_MS } from "../utils/time";
22
import type { Cache } from "./cache";
33

4-
const EXPIRATION = DAY_MS / 1000; // 24 hours in seconds
4+
const TTL = DAY_MS;
5+
const TTL_S = TTL / 1000;
56

67
export function createRedisCache(): Cache {
78
return {
@@ -14,7 +15,7 @@ export function createRedisCache(): Cache {
1415
},
1516
async set<T>(key: string, value: T): Promise<void> {
1617
await Bun.redis.set(key, JSON.stringify(value));
17-
await Bun.redis.expire(key, EXPIRATION);
18+
await Bun.redis.expire(key, TTL_S);
1819
},
1920
};
2021
}

0 commit comments

Comments
 (0)