-
Notifications
You must be signed in to change notification settings - Fork 602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(cache/unstable): TtlCache
#5662
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
db05ddd
feat(cache/unstable): add TTL (time-to-live) cache
lionel-rowe d051908
Merge branch 'main' into ttl-cache
lionel-rowe c1845f3
Merge branch 'main' into ttl-cache
iuioiua 2ccb813
Implement code review changes
lionel-rowe 9b86369
Merge branch 'main' into ttl-cache
lionel-rowe b8246a3
Simplify timeout logic
lionel-rowe a5880ec
Update cache/ttl_cache.ts
lionel-rowe 9870e17
Properly handle cleanup of timeouts
lionel-rowe 4150644
chore: add experimental note
kt3k a91ac18
Add test for [Symbol.toStringTag]
lionel-rowe 37572e5
tweak
iuioiua File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
import type { MemoizationCache } from "./memoize.ts"; | ||
|
||
/** | ||
* Time-to-live cache. | ||
* | ||
* @experimental **UNSTABLE**: New API, yet to be vetted. | ||
* | ||
* Automatically removes entries once the configured amount of time elapses. | ||
* | ||
* @typeParam K The type of the cache keys. | ||
* @typeParam V The type of the cache values. | ||
* | ||
* @example Usage | ||
* ```ts | ||
* import { TtlCache } from "@std/cache/ttl-cache"; | ||
* import { assertEquals } from "@std/assert/equals"; | ||
* import { delay } from "@std/async/delay"; | ||
* | ||
* const cache = new TtlCache<string, number>(1000); | ||
* | ||
* cache.set("a", 1); | ||
* assertEquals(cache.size, 1); | ||
* await delay(2000); | ||
* assertEquals(cache.size, 0); | ||
* ``` | ||
*/ | ||
export class TtlCache<K, V> extends Map<K, V> | ||
implements MemoizationCache<K, V> { | ||
#defaultTtl: number; | ||
#timeouts = new Map<K, number>(); | ||
|
||
/** | ||
* Constructs a new instance. | ||
* | ||
* @experimental **UNSTABLE**: New API, yet to be vetted. | ||
* | ||
* @param defaultTtl The default time-to-live in milliseconds | ||
*/ | ||
constructor(defaultTtl: number) { | ||
super(); | ||
this.#defaultTtl = defaultTtl; | ||
} | ||
|
||
/** | ||
* Set a value in the cache. | ||
* | ||
* @experimental **UNSTABLE**: New API, yet to be vetted. | ||
* | ||
* @param key The cache key | ||
* @param value The value to set | ||
* @param ttl A custom time-to-live. If supplied, overrides the cache's default TTL for this entry. | ||
* @returns `this` for chaining. | ||
* | ||
* @example Usage | ||
* ```ts | ||
* import { TtlCache } from "@std/cache/ttl-cache"; | ||
* import { assertEquals } from "@std/assert/equals"; | ||
* | ||
* const cache = new TtlCache<string, number>(1000); | ||
* | ||
* cache.set("a", 1); | ||
* assertEquals(cache.get("a"), 1); | ||
* ``` | ||
*/ | ||
override set(key: K, value: V, ttl: number = this.#defaultTtl): this { | ||
clearTimeout(this.#timeouts.get(key)); | ||
super.set(key, value); | ||
this.#timeouts.set(key, setTimeout(() => this.delete(key), ttl)); | ||
return this; | ||
} | ||
|
||
/** | ||
* Deletes the value associated with the given key. | ||
* | ||
* @experimental **UNSTABLE**: New API, yet to be vetted. | ||
* | ||
* @param key The key to delete. | ||
* @returns `true` if the key was deleted, `false` otherwise. | ||
* | ||
* @example Usage | ||
* ```ts | ||
* import { TtlCache } from "@std/cache"; | ||
* import { assertEquals } from "@std/assert/equals"; | ||
* | ||
* const cache = new TtlCache<string, number>(1000); | ||
* | ||
* cache.set("a", 1); | ||
* cache.delete("a"); | ||
* assertEquals(cache.has("a"), false); | ||
* ``` | ||
*/ | ||
override delete(key: K): boolean { | ||
clearTimeout(this.#timeouts.get(key)); | ||
this.#timeouts.delete(key); | ||
return super.delete(key); | ||
} | ||
|
||
/** | ||
* Clears the cache. | ||
* | ||
* @experimental **UNSTABLE**: New API, yet to be vetted. | ||
* | ||
* @example Usage | ||
* ```ts | ||
* import { TtlCache } from "@std/cache"; | ||
* import { assertEquals } from "@std/assert/equals"; | ||
* | ||
* const cache = new TtlCache<string, number>(1000); | ||
* | ||
* cache.set("a", 1); | ||
* cache.set("b", 2); | ||
* cache.clear(); | ||
* assertEquals(cache.size, 0); | ||
* ``` | ||
*/ | ||
override clear(): void { | ||
for (const timeout of this.#timeouts.values()) { | ||
clearTimeout(timeout); | ||
} | ||
this.#timeouts.clear(); | ||
super.clear(); | ||
} | ||
|
||
/** | ||
* Automatically clears all remaining timeouts once the cache goes out of | ||
* scope if the cache is declared with `using`. | ||
* | ||
* @experimental **UNSTABLE**: New API, yet to be vetted. | ||
* | ||
* @example Usage | ||
* ```ts no-assert | ||
* import { TtlCache } from "@std/cache/ttl-cache"; | ||
* import { assertEquals } from "@std/assert/equals"; | ||
* | ||
* let c: TtlCache<string, number>; | ||
* { | ||
* using cache = new TtlCache<string, number>(1000); | ||
* cache.set("a", 1); | ||
* c = cache; | ||
* } | ||
* assertEquals(c.size, 0); | ||
* ``` | ||
*/ | ||
[Symbol.dispose](): void { | ||
this.clear(); | ||
} | ||
} |
lionel-rowe marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
import { TtlCache } from "./ttl_cache.ts"; | ||
import { assertEquals } from "@std/assert"; | ||
import { FakeTime } from "@std/testing/time"; | ||
|
||
const UNSET = Symbol("UNSET"); | ||
|
||
// check `has()`, `get()`, `forEach()` | ||
function assertEntries<K, V>( | ||
cache: TtlCache<K, V>, | ||
entries: [key: K, value: V | typeof UNSET][], | ||
) { | ||
for (const [key, value] of entries) { | ||
assertEquals(cache.has(key), value !== UNSET); | ||
assertEquals(cache.get(key), value === UNSET ? undefined : value); | ||
} | ||
|
||
cache.forEach((v, k) => assertEquals(v, entries.find(([x]) => x === k)![1])); | ||
assertContentfulEntries(cache, entries.filter(([, v]) => v !== UNSET)); | ||
} | ||
|
||
// check `size`, `entries()`, `keys()`, `values()`, `[Symbol.iterator]()` | ||
function assertContentfulEntries<K, V>( | ||
cache: TtlCache<K, V>, | ||
entries: [key: K, value: V][], | ||
) { | ||
const keys = entries.map(([key]) => key); | ||
const values = entries.map(([, value]) => value); | ||
|
||
assertEquals(cache.size, entries.length); | ||
|
||
assertEquals([...cache.entries()], entries); | ||
assertEquals([...cache.keys()], keys); | ||
assertEquals([...cache.values()], values); | ||
assertEquals([...cache], entries); | ||
} | ||
|
||
Deno.test("TtlCache deletes entries", async (t) => { | ||
await t.step("after the default TTL, passed in constructor", () => { | ||
using time = new FakeTime(0); | ||
|
||
const cache = new TtlCache<number, string>(10); | ||
|
||
cache.set(1, "one"); | ||
cache.set(2, "two"); | ||
|
||
time.now = 1; | ||
assertEntries(cache, [[1, "one"], [2, "two"]]); | ||
iuioiua marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
time.now = 5; | ||
assertEntries(cache, [[1, "one"], [2, "two"]]); | ||
// setting again resets TTL countdown for key 1 | ||
cache.set(1, "one"); | ||
|
||
time.now = 10; | ||
assertEntries(cache, [[1, "one"], [2, UNSET]]); | ||
|
||
time.now = 15; | ||
assertEntries(cache, [[1, UNSET], [2, UNSET]]); | ||
}); | ||
|
||
await t.step("after a custom TTL, passed in set()", () => { | ||
using time = new FakeTime(0); | ||
|
||
const cache = new TtlCache<number, string>(10); | ||
|
||
cache.set(1, "one"); | ||
cache.set(2, "two", 3); | ||
|
||
time.now = 1; | ||
assertEntries(cache, [[1, "one"], [2, "two"]]); | ||
|
||
time.now = 3; | ||
assertEntries(cache, [[1, "one"], [2, UNSET]]); | ||
|
||
time.now = 10; | ||
assertEntries(cache, [[1, UNSET], [2, UNSET]]); | ||
}); | ||
|
||
await t.step("after manually calling delete()", () => { | ||
const cache = new TtlCache<number, string>(10); | ||
|
||
cache.set(1, "one"); | ||
assertEntries(cache, [[1, "one"]]); | ||
assertEquals(cache.delete(1), true); | ||
assertEntries(cache, [[1, UNSET]]); | ||
assertEquals(cache.delete(1), false); | ||
assertEntries(cache, [[1, UNSET]]); | ||
}); | ||
|
||
await t.step("after manually calling clear()", () => { | ||
const cache = new TtlCache<number, string>(10); | ||
|
||
cache.set(1, "one"); | ||
assertEntries(cache, [[1, "one"]]); | ||
cache.clear(); | ||
assertEntries(cache, [[1, UNSET]]); | ||
}); | ||
|
||
// this test will fail with `error: Leaks detected` if the timeouts are not cleared | ||
await t.step("[Symbol.dispose]() clears all remaining timeouts", () => { | ||
using cache = new TtlCache<number, string>(10); | ||
cache.set(1, "one"); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be more clear to use the word "after" (this is the word used in the spec) since the spec doesn't seem to enforce that a runtime must prioritize timer tasks over all other tasks in the event loop — which essentially means that a runtime is free to delay execution of the callback for an unbounded amount of time.