diff options
author | Heyang Zhou <zhy20000919@hotmail.com> | 2023-08-18 17:34:16 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-18 17:34:16 +0800 |
commit | c77c836a238ce683b21c12f88cd1101b930ce042 (patch) | |
tree | b69b2318f618d1b384dc168ab6652496d63e2f2c /ext/kv/01_db.ts | |
parent | b5839eefcf02e62e9e77e8095f372ac06a523cba (diff) |
feat(ext/kv): key expiration (#20091)
Co-authored-by: Luca Casonato <hello@lcas.dev>
Diffstat (limited to 'ext/kv/01_db.ts')
-rw-r--r-- | ext/kv/01_db.ts | 37 |
1 files changed, 26 insertions, 11 deletions
diff --git a/ext/kv/01_db.ts b/ext/kv/01_db.ts index 8fd1f5997..4cd8744ba 100644 --- a/ext/kv/01_db.ts +++ b/ext/kv/01_db.ts @@ -130,12 +130,15 @@ class Kv { }); } - async set(key: Deno.KvKey, value: unknown) { + async set(key: Deno.KvKey, value: unknown, options?: { expireIn?: number }) { value = serializeValue(value); const checks: Deno.AtomicCheck[] = []; + const expireAt = typeof options?.expireIn === "number" + ? Date.now() + options.expireIn + : undefined; const mutations = [ - [key, "set", value], + [key, "set", value, expireAt], ]; const versionstamp = await core.opAsync( @@ -152,7 +155,7 @@ class Kv { async delete(key: Deno.KvKey) { const checks: Deno.AtomicCheck[] = []; const mutations = [ - [key, "delete", null], + [key, "delete", null, undefined], ]; const result = await core.opAsync( @@ -318,7 +321,7 @@ class AtomicOperation { #rid: number; #checks: [Deno.KvKey, string | null][] = []; - #mutations: [Deno.KvKey, string, RawValue | null][] = []; + #mutations: [Deno.KvKey, string, RawValue | null, number | undefined][] = []; #enqueues: [Uint8Array, number, Deno.KvKey[], number[] | null][] = []; constructor(rid: number) { @@ -337,6 +340,7 @@ class AtomicOperation { const key = mutation.key; let type: string; let value: RawValue | null; + let expireAt: number | undefined = undefined; switch (mutation.type) { case "delete": type = "delete"; @@ -345,6 +349,10 @@ class AtomicOperation { } break; case "set": + if (typeof mutation.expireIn === "number") { + expireAt = Date.now() + mutation.expireIn; + } + /* falls through */ case "sum": case "min": case "max": @@ -357,33 +365,40 @@ class AtomicOperation { default: throw new TypeError("Invalid mutation type"); } - this.#mutations.push([key, type, value]); + this.#mutations.push([key, type, value, expireAt]); } return this; } sum(key: Deno.KvKey, n: bigint): this { - this.#mutations.push([key, "sum", serializeValue(new KvU64(n))]); + this.#mutations.push([key, "sum", serializeValue(new KvU64(n)), undefined]); return this; } min(key: Deno.KvKey, n: bigint): this { - this.#mutations.push([key, "min", serializeValue(new KvU64(n))]); + this.#mutations.push([key, "min", serializeValue(new KvU64(n)), undefined]); return this; } max(key: Deno.KvKey, n: bigint): this { - this.#mutations.push([key, "max", serializeValue(new KvU64(n))]); + this.#mutations.push([key, "max", serializeValue(new KvU64(n)), undefined]); return this; } - set(key: Deno.KvKey, value: unknown): this { - this.#mutations.push([key, "set", serializeValue(value)]); + set( + key: Deno.KvKey, + value: unknown, + options?: { expireIn?: number }, + ): this { + const expireAt = typeof options?.expireIn === "number" + ? Date.now() + options.expireIn + : undefined; + this.#mutations.push([key, "set", serializeValue(value), expireAt]); return this; } delete(key: Deno.KvKey): this { - this.#mutations.push([key, "delete", null]); + this.#mutations.push([key, "delete", null, undefined]); return this; } |