File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import { HOUR_MS } from "../utils/time" ;
12import type { Cache } from "./cache" ;
23
4+ const TTL = HOUR_MS ;
5+
36export 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}
Original file line number Diff line number Diff line change 11import { DAY_MS } from "../utils/time" ;
22import 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
67export 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}
You can’t perform that action at this time.
0 commit comments