-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathExpirableValue.ts
36 lines (32 loc) · 968 Bytes
/
ExpirableValue.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import type { ExpirableValueInterface } from './types';
/**
* Class to represent a value that can expire.
*
* Upon creation, the value is assigned a TTL (time to live) that is calculated
* by adding the current time with the maximum age.
*/
class ExpirableValue implements ExpirableValueInterface {
public ttl: number;
public value: string | Uint8Array | Record<string, unknown>;
/**
*
* @param value - Value to be cached
* @param maxAge - Maximum age in seconds for the value to be cached
*/
public constructor(value: string | Uint8Array | Record<string, unknown>, maxAge: number) {
this.value = value;
const timeNow = new Date();
this.ttl = timeNow.setSeconds(timeNow.getSeconds() + maxAge);
}
/**
* Check if the value has expired.
*
* @returns {boolean} - True if the value has expired, false otherwise
*/
public isExpired(): boolean {
return this.ttl < Date.now();
}
}
export {
ExpirableValue
};