-
Notifications
You must be signed in to change notification settings - Fork 145
feat(sdk): add cache primitive #1118
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
Open
aryasaatvik
wants to merge
2
commits into
RhysSullivan:main
Choose a base branch
from
aryasaatvik:contrib/sdk-cache-primitive
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+328
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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,5 @@ | ||
| --- | ||
| "executor": patch | ||
| --- | ||
|
|
||
| Add a host-provided cache primitive to the SDK executor surface. Hosts can now pass an Effect KeyValueStore to `createExecutor`, while executors without one use an in-memory fallback. |
This file contains hidden or 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 hidden or 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,92 @@ | ||
| import { describe, expect, it } from "@effect/vitest"; | ||
| import { Effect } from "effect"; | ||
|
|
||
| import { createExecutor, type Executor } from "./executor"; | ||
| import { Tenant } from "./ids"; | ||
|
|
||
| const MEMORY_CACHE_CAPACITY = 2_048; | ||
| const MEMORY_CACHE_TTL_MS = 10 * 60 * 1000; | ||
|
|
||
| const makeExecutor = Effect.acquireRelease( | ||
| createExecutor({ | ||
| tenant: Tenant.make("test-tenant"), | ||
| onElicitation: "accept-all", | ||
| }), | ||
| (executor) => executor.close().pipe(Effect.ignore), | ||
| ); | ||
|
|
||
| const withFakeNow = <A, E, R>( | ||
| initialNow: number, | ||
| run: (clock: { readonly advance: (ms: number) => void }) => Effect.Effect<A, E, R>, | ||
| ): Effect.Effect<A, E, R> => | ||
| Effect.acquireUseRelease( | ||
| Effect.sync(() => { | ||
| const originalNow = Date.now; | ||
| let now = initialNow; | ||
| Date.now = () => now; | ||
| return { | ||
| advance: (ms: number) => { | ||
| now += ms; | ||
| }, | ||
| restore: () => { | ||
| Date.now = originalNow; | ||
| }, | ||
| }; | ||
| }), | ||
| (clock) => run({ advance: clock.advance }), | ||
| (clock) => Effect.sync(clock.restore), | ||
| ); | ||
|
|
||
| describe("executor cache", () => { | ||
| it.effect("uses an in-memory fallback when no cache is configured", () => | ||
| Effect.scoped( | ||
| Effect.gen(function* () { | ||
| const executor = yield* makeExecutor; | ||
|
|
||
| yield* executor.cache.set("a", "value"); | ||
| expect(yield* executor.cache.get("a")).toBe("value"); | ||
|
|
||
| yield* executor.cache.remove("a"); | ||
| expect(yield* executor.cache.get("a")).toBeUndefined(); | ||
| }), | ||
| ), | ||
| ); | ||
|
|
||
| it.effect("expires fallback entries by TTL on get and size", () => | ||
| withFakeNow(1_000, ({ advance }) => | ||
| Effect.scoped( | ||
| Effect.gen(function* () { | ||
| const executor = yield* makeExecutor; | ||
|
|
||
| yield* executor.cache.set("a", "value"); | ||
| expect(yield* executor.cache.size).toBe(1); | ||
|
|
||
| advance(MEMORY_CACHE_TTL_MS); | ||
|
|
||
| expect(yield* executor.cache.get("a")).toBeUndefined(); | ||
| expect(yield* executor.cache.size).toBe(0); | ||
| }), | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| it.effect("refreshes fallback LRU position when an existing key is written", () => | ||
| Effect.scoped( | ||
| Effect.gen(function* () { | ||
| const executor: Executor = yield* makeExecutor; | ||
|
|
||
| yield* executor.cache.set("a", "old"); | ||
| for (let index = 0; index < MEMORY_CACHE_CAPACITY - 1; index += 1) { | ||
| yield* executor.cache.set(`key-${index}`, String(index)); | ||
| } | ||
|
|
||
| yield* executor.cache.set("a", "new"); | ||
| yield* executor.cache.set("overflow", "value"); | ||
|
|
||
| expect(yield* executor.cache.get("a")).toBe("new"); | ||
| expect(yield* executor.cache.get("key-0")).toBeUndefined(); | ||
| expect(yield* executor.cache.get("key-1")).toBe("1"); | ||
| }), | ||
| ), | ||
| ); | ||
| }); |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,93 @@ | ||
| import { describe, expect, it } from "@effect/vitest"; | ||
| import { Effect } from "effect"; | ||
| import type { KVNamespace } from "@cloudflare/workers-types"; | ||
|
|
||
| import { makeCloudflareKeyValueStore } from "./key-value-store"; | ||
|
|
||
| const makeFakeKv = ( | ||
| pageSize: number, | ||
| ): { | ||
| readonly kv: KVNamespace; | ||
| readonly values: Map<string, string>; | ||
| readonly maxConcurrentDeletes: () => number; | ||
| } => { | ||
| const values = new Map<string, string>(); | ||
| let activeDeletes = 0; | ||
| let maxActiveDeletes = 0; | ||
|
|
||
| // oxlint-disable-next-line executor/no-double-cast -- test double: only the KV slice the adapter calls is implemented | ||
| const kv = { | ||
| get: async (key: string) => values.get(key) ?? null, | ||
| put: async (key: string, value: string) => { | ||
| values.set(key, value); | ||
| }, | ||
| delete: async (key: string) => { | ||
| activeDeletes += 1; | ||
| maxActiveDeletes = Math.max(maxActiveDeletes, activeDeletes); | ||
| await new Promise((resolve) => setTimeout(resolve, 0)); | ||
| values.delete(key); | ||
| activeDeletes -= 1; | ||
| }, | ||
| list: async (options?: { readonly cursor?: string }) => { | ||
| const offset = options?.cursor === undefined ? 0 : Number(options.cursor); | ||
| const keys = [...values.keys()].sort().slice(offset, offset + pageSize); | ||
| const nextOffset = offset + pageSize; | ||
| const listComplete = nextOffset >= values.size; | ||
|
|
||
| return { | ||
| keys: keys.map((name) => ({ name })), | ||
| list_complete: listComplete, | ||
| cursor: listComplete ? "" : String(nextOffset), | ||
| }; | ||
| }, | ||
| } as unknown as KVNamespace; | ||
|
|
||
| return { | ||
| kv, | ||
| values, | ||
| maxConcurrentDeletes: () => maxActiveDeletes, | ||
| }; | ||
| }; | ||
|
|
||
| describe("makeCloudflareKeyValueStore", () => { | ||
| it.effect("round-trips string values", () => | ||
| Effect.gen(function* () { | ||
| const { kv } = makeFakeKv(10); | ||
| const store = makeCloudflareKeyValueStore(kv); | ||
|
|
||
| yield* store.set("a", "value"); | ||
| expect(yield* store.get("a")).toBe("value"); | ||
|
|
||
| yield* store.remove("a"); | ||
| expect(yield* store.get("a")).toBeUndefined(); | ||
| }), | ||
| ); | ||
|
|
||
| it.effect("counts paginated keys", () => | ||
| Effect.gen(function* () { | ||
| const { values, kv } = makeFakeKv(2); | ||
| values.set("a", "1"); | ||
| values.set("b", "2"); | ||
| values.set("c", "3"); | ||
|
|
||
| const store = makeCloudflareKeyValueStore(kv); | ||
| expect(yield* store.size).toBe(3); | ||
| }), | ||
| ); | ||
|
|
||
| it.effect("clears paginated keys in bounded parallel batches", () => | ||
| Effect.gen(function* () { | ||
| const { values, kv, maxConcurrentDeletes } = makeFakeKv(25); | ||
| for (let index = 0; index < 75; index += 1) { | ||
| values.set(`key-${index.toString().padStart(2, "0")}`, String(index)); | ||
| } | ||
|
|
||
| const store = makeCloudflareKeyValueStore(kv); | ||
| yield* store.clear; | ||
|
|
||
| expect(values.size).toBe(0); | ||
| expect(maxConcurrentDeletes()).toBeGreaterThan(1); | ||
| expect(maxConcurrentDeletes()).toBeLessThanOrEqual(50); | ||
| }), | ||
| ); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.