-
Notifications
You must be signed in to change notification settings - Fork 46
feat: configurable auto-eviction #508
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "type": "minor", | ||
| "comment": "feat: configurable auto-eviction", | ||
| "packageName": "@graphitation/apollo-forest-run", | ||
| "email": "vladimir.razuvaev@gmail.com", | ||
| "dependentChangeType": "patch" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| import { ForestRun } from "../ForestRun"; | ||
| import { gql } from "./helpers/descriptor"; | ||
|
|
||
| it("evicts data automatically by default", () => { | ||
| const cache = new ForestRun({ | ||
| maxOperationCount: 1, | ||
| }); | ||
| const query = gql` | ||
| query ($i: Int) { | ||
| foo(i: $i) | ||
| } | ||
| `; | ||
|
|
||
| cache.write({ query, variables: { i: 0 }, result: { foo: 0 } }); | ||
| cache.write({ query, variables: { i: 1 }, result: { foo: 1 } }); | ||
| cache.write({ query, variables: { i: 2 }, result: { foo: 2 } }); | ||
|
|
||
| const result0 = cache.read({ query, variables: { i: 0 }, optimistic: true }); | ||
| const result1 = cache.read({ query, variables: { i: 1 }, optimistic: true }); | ||
| const result2 = cache.read({ query, variables: { i: 2 }, optimistic: true }); | ||
|
|
||
| expect(result0).toEqual(null); | ||
| expect(result1).toEqual(null); | ||
| expect(result2).toEqual({ foo: 2 }); | ||
| }); | ||
|
|
||
| it("allows disabling automatic eviction", () => { | ||
| const cache = new ForestRun({ | ||
| maxOperationCount: 1, | ||
| autoEvict: false, | ||
| }); | ||
| const query = gql` | ||
| query ($i: Int) { | ||
| foo(i: $i) | ||
| } | ||
| `; | ||
|
|
||
| cache.write({ query, variables: { i: 0 }, result: { foo: 0 } }); | ||
| cache.write({ query, variables: { i: 1 }, result: { foo: 1 } }); | ||
| cache.write({ query, variables: { i: 2 }, result: { foo: 2 } }); | ||
|
|
||
| const result0 = cache.read({ query, variables: { i: 0 }, optimistic: true }); | ||
| const result1 = cache.read({ query, variables: { i: 1 }, optimistic: true }); | ||
| const result2 = cache.read({ query, variables: { i: 2 }, optimistic: true }); | ||
|
|
||
| expect(result0).toEqual({ foo: 0 }); | ||
| expect(result1).toEqual({ foo: 1 }); | ||
| expect(result2).toEqual({ foo: 2 }); | ||
| }); | ||
|
|
||
| it("allows manual eviction", () => { | ||
| const cache = new ForestRun({ | ||
| maxOperationCount: 1, | ||
| autoEvict: false, | ||
| }); | ||
| const query = gql` | ||
| query ($i: Int) { | ||
| foo(i: $i) | ||
| } | ||
| `; | ||
|
|
||
| cache.write({ query, variables: { i: 0 }, result: { foo: 0 } }); | ||
| cache.write({ query, variables: { i: 1 }, result: { foo: 1 } }); | ||
| cache.write({ query, variables: { i: 2 }, result: { foo: 2 } }); | ||
|
|
||
| cache.gc(); | ||
|
|
||
| const result0 = cache.read({ query, variables: { i: 0 }, optimistic: true }); | ||
| const result1 = cache.read({ query, variables: { i: 1 }, optimistic: true }); | ||
| const result2 = cache.read({ query, variables: { i: 2 }, optimistic: true }); | ||
|
|
||
| expect(result0).toEqual(null); | ||
| expect(result1).toEqual(null); | ||
| expect(result2).toEqual({ foo: 2 }); | ||
| }); | ||
|
|
||
| it("doesn't evict watched old operations", () => { | ||
| const cache = new ForestRun({ | ||
| maxOperationCount: 1, | ||
| autoEvict: false, | ||
| }); | ||
| const query = gql` | ||
| query ($i: Int) { | ||
| foo(i: $i) | ||
| } | ||
| `; | ||
|
|
||
| cache.watch({ | ||
| query, | ||
| variables: { i: 1 }, | ||
| optimistic: true, | ||
| callback: () => {}, | ||
| }); | ||
|
|
||
| cache.write({ query, variables: { i: 0 }, result: { foo: 0 } }); | ||
| cache.write({ query, variables: { i: 1 }, result: { foo: 1 } }); | ||
| cache.write({ query, variables: { i: 2 }, result: { foo: 2 } }); | ||
|
|
||
| cache.gc(); | ||
|
|
||
| const result0 = cache.read({ query, variables: { i: 0 }, optimistic: true }); | ||
| const result1 = cache.read({ query, variables: { i: 1 }, optimistic: true }); | ||
| const result2 = cache.read({ query, variables: { i: 2 }, optimistic: true }); | ||
|
|
||
| expect(result0).toEqual(null); | ||
| expect(result1).toEqual({ foo: 1 }); | ||
| expect(result2).toEqual({ foo: 2 }); | ||
| }); | ||
|
|
||
| it("allows to opt out operations from eviction", () => { | ||
| const cache = new ForestRun({ | ||
| maxOperationCount: 1, | ||
| autoEvict: false, | ||
| nonEvictableQueries: new Set(["b"]), // root-level field | ||
| }); | ||
| const a = gql` | ||
| { | ||
| a | ||
| } | ||
| `; | ||
| const b = gql` | ||
| { | ||
| b | ||
| } | ||
| `; | ||
| const c = gql` | ||
| { | ||
| c | ||
| } | ||
| `; | ||
|
|
||
| cache.write({ query: a, result: { a: 0 } }); | ||
| cache.write({ query: b, result: { b: 1 } }); | ||
| cache.write({ query: c, result: { c: 2 } }); | ||
|
|
||
| cache.gc(); | ||
|
|
||
| const result0 = cache.read({ query: a, optimistic: true }); | ||
| const result1 = cache.read({ query: b, optimistic: true }); | ||
| const result2 = cache.read({ query: c, optimistic: true }); | ||
|
|
||
| expect(result0).toEqual(null); | ||
| expect(result1).toEqual({ b: 1 }); | ||
| expect(result2).toEqual({ c: 2 }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,7 +23,6 @@ export function createCacheEnvironment(config?: CacheConfig): CacheEnv { | |
| const env: CacheEnv = { | ||
| addTypename: config?.addTypename ?? true, | ||
| apolloCompat_keepOrphanNodes: config?.apolloCompat_keepOrphanNodes ?? false, | ||
| maxOperationCount: config?.maxOperationCount ?? 1000, | ||
| possibleTypes, | ||
| typePolicies, | ||
| dataIdFromObject: config?.dataIdFromObject, | ||
|
|
@@ -35,6 +34,9 @@ export function createCacheEnvironment(config?: CacheConfig): CacheEnv { | |
| }, | ||
| mergePolicies: new Map(), | ||
| readPolicies: new Map(), | ||
| autoEvict: config?.autoEvict ?? true, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick: Would be nice to have it sorted alphabetically someday
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe someday :) I prefer having related options grouped together - so if we sort alphabetically we will need to rename them to have a common prefix for the same "group" (or introduce another level of objects for grouping). Which is a breaking change. |
||
| nonEvictableQueries: config?.nonEvictableQueries ?? new Set(), | ||
| maxOperationCount: config?.maxOperationCount ?? 1000, | ||
| now: () => ++tick, // Logical time | ||
| genId: () => ++id, | ||
| objectKey: ( | ||
|
|
||
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.
question: maxOperationCount is actually increased by length of nonEvictableQueries, right?
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.
yes, by design