Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
145 changes: 145 additions & 0 deletions packages/apollo-forest-run/src/__tests__/eviction.test.ts
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
Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, by design

});
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 });
});
4 changes: 3 additions & 1 deletion packages/apollo-forest-run/src/cache/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -35,6 +34,9 @@ export function createCacheEnvironment(config?: CacheConfig): CacheEnv {
},
mergePolicies: new Map(),
readPolicies: new Map(),
autoEvict: config?.autoEvict ?? true,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: Would be nice to have it sorted alphabetically someday

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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: (
Expand Down
1 change: 1 addition & 0 deletions packages/apollo-forest-run/src/cache/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export function touchOperation(

export function maybeEvictOldData(env: CacheEnv, store: Store): OperationId[] {
if (
!env.autoEvict ||
!env.maxOperationCount ||
store.dataForest.trees.size <= env.maxOperationCount * 2
) {
Expand Down
6 changes: 4 additions & 2 deletions packages/apollo-forest-run/src/cache/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export type Transaction = {
};

export type CacheConfig = InMemoryCacheConfig & {
autoEvict?: boolean;
maxOperationCount?: number;
nonEvictableQueries?: Set<string>;
apolloCompat_keepOrphanNodes?: boolean;
Expand Down Expand Up @@ -141,6 +142,7 @@ export type CacheEnv = {

mergePolicies: Map<TypeName, Map<FieldName, FieldMergeFunction>>;
readPolicies: Map<TypeName, Map<FieldName, FieldReadFunction>>;
nonEvictableQueries?: Set<string>;
maxOperationCount?: number;
autoEvict: boolean;
nonEvictableQueries: Set<string>;
maxOperationCount: number;
};