Skip to content

Commit

Permalink
Use timestamp/millisecond based math for checking expiration.
Browse files Browse the repository at this point in the history
  • Loading branch information
brnkrygs committed Mar 10, 2023
1 parent 2d80baa commit 3fea0d7
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/parameters/src/ExpirableValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@ class ExpirableValue implements ExpirableValueInterface {
public ttl: number;
public value: string | Uint8Array | Record<string, unknown>;

/**
* Creates a new cached value which will expire automatically
* @param value Parameter value to be cached
* @param maxAge Maximum number of seconds to cache the value for
*/
public constructor(value: string | Uint8Array | Record<string, unknown>, maxAge: number) {
this.value = value;
const timeNow = new Date();
this.ttl = timeNow.setSeconds(timeNow.getSeconds() + maxAge);

const maxAgeInMilliseconds = maxAge * 1000;
const nowTimestamp = Date.now();
this.ttl = nowTimestamp + maxAgeInMilliseconds;
}

public isExpired(): boolean {
Expand Down

0 comments on commit 3fea0d7

Please sign in to comment.