Skip to content

Commit d9e7489

Browse files
committed
feat(endpoint-cache): write setter which converts CachePeriodInMinutes to Expires
1 parent b15abd9 commit d9e7489

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export interface Endpoint {
2+
/**
3+
* <p>An endpoint address.</p>
4+
*/
5+
Address: string;
6+
7+
/**
8+
* <p>The TTL for the endpoint, in minutes.</p>
9+
*/
10+
CachePeriodInMinutes: number;
11+
}

packages/endpoint-cache/src/EndpointCache.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { LRUCache } from "mnemonist";
22

3+
import { Endpoint } from "./Endpoint";
4+
5+
interface EndpointWithExpiry extends Pick<Endpoint, "Address"> {
6+
Expires: number;
7+
}
8+
39
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[]>;
711

812
constructor(capacity: number) {
913
this.cache = new LRUCache(capacity);
@@ -13,14 +17,21 @@ export class EndpointCache {
1317
return this.cache.get(key);
1418
}
1519

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+
);
1829
}
1930

2031
remove(key: string) {
2132
// Replace with remove call once support is added upstream
2233
// Refs: https://github.com/Yomguithereal/mnemonist/issues/143
23-
this.cache.set(key, undefined);
34+
this.cache.set(key, []);
2435
}
2536

2637
clear() {

0 commit comments

Comments
 (0)