Skip to content

Commit

Permalink
consolidate expiry logic
Browse files Browse the repository at this point in the history
  • Loading branch information
blaine-arcjet committed Dec 11, 2023
1 parent db2e5db commit 362c9a6
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions arcjet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ class Cache<T> {
}

get(key: string) {
const expiresAt = this.expires.get(key);
if (typeof expiresAt !== "undefined" && expiresAt > Date.now()) {
const ttl = this.ttl(key);
if (ttl > 0) {
return this.data.get(key);
} else {
// Cleanup if expired
Expand All @@ -76,7 +76,9 @@ class Cache<T> {
}

ttl(key: string): number {
return this.expires.get(key) ?? 0
const now = Date.now();
const expiresAt = this.expires.get(key) ?? now;
return expiresAt - now;
}
}

Expand Down

0 comments on commit 362c9a6

Please sign in to comment.