1
1
import { LRUCache } from "mnemonist" ;
2
2
3
+ import { Endpoint } from "./Endpoint" ;
4
+
5
+ interface EndpointWithExpiry extends Pick < Endpoint , "Address" > {
6
+ Expires : number ;
7
+ }
8
+
3
9
export class EndpointCache {
4
- // ToDo: remove `| undefined` once LRUCache.remove(key) is available.
5
- // Refs: https://github.com/Yomguithereal/mnemonist/issues/143
6
- private readonly cache : LRUCache < string , string | undefined > ;
10
+ private readonly cache : LRUCache < string , EndpointWithExpiry [ ] > ;
7
11
8
12
constructor ( capacity : number ) {
9
13
this . cache = new LRUCache ( capacity ) ;
@@ -13,14 +17,21 @@ export class EndpointCache {
13
17
return this . cache . get ( key ) ;
14
18
}
15
19
16
- set ( key : string , value : string ) {
17
- this . cache . set ( key , value ) ;
20
+ set ( key : string , endpoints : Endpoint [ ] ) {
21
+ const now = Date . now ( ) ;
22
+ this . cache . set (
23
+ key ,
24
+ endpoints . map ( ( { Address = "" , CachePeriodInMinutes = 1 } ) => ( {
25
+ Address,
26
+ Expires : now + CachePeriodInMinutes * 60 * 100 ,
27
+ } ) )
28
+ ) ;
18
29
}
19
30
20
31
remove ( key : string ) {
21
32
// Replace with remove call once support is added upstream
22
33
// Refs: https://github.com/Yomguithereal/mnemonist/issues/143
23
- this . cache . set ( key , undefined ) ;
34
+ this . cache . set ( key , [ ] ) ;
24
35
}
25
36
26
37
clear ( ) {
0 commit comments