Skip to content
This repository was archived by the owner on Mar 1, 2026. It is now read-only.

Commit efbf09e

Browse files
committed
Cleanup.
1 parent 930f89f commit efbf09e

4 files changed

Lines changed: 53 additions & 47 deletions

File tree

packages/plugins/cache/src/plugin.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { entryIsFresh, entryIsStale } from './utils'
88

99
export function defineCachePlugin(pluginOptions: CachePluginOptions) {
1010
let status: CacheStatus | null = null;
11-
let revalidation: Promise<void> | null = null;
11+
let revalidation: Promise<unknown> | null = null;
1212

1313
return definePlugin({
1414
id: 'cache',
@@ -63,7 +63,7 @@ export function defineCachePlugin(pluginOptions: CachePluginOptions) {
6363
const cache = pluginOptions.provider;
6464
const options = (args as CacheEnvelope).cache!;
6565
const key = murmurhash.v3(json).toString();
66-
const queryResultEntry = await cache.getQueryResult(key);
66+
const queryResultEntry = await cache.get(key);
6767

6868
if (queryResultEntry) {
6969
if (entryIsFresh(queryResultEntry)) {
@@ -72,14 +72,17 @@ export function defineCachePlugin(pluginOptions: CachePluginOptions) {
7272
} else if (entryIsStale(queryResultEntry)) {
7373
revalidation = proceed(args).then(async (result) => {
7474
try {
75-
await cache.setQueryResult(key, {
75+
await cache.set(key, {
7676
createdAt: Date.now(),
7777
options,
7878
result,
79-
})
79+
});
80+
81+
return result;
8082
}
8183
catch (err) {
82-
console.error(`Failed to cache query result: ${err}`)
84+
console.error(`Failed to cache query result: ${err}`);
85+
return null;
8386
}
8487
});
8588

@@ -90,7 +93,7 @@ export function defineCachePlugin(pluginOptions: CachePluginOptions) {
9093

9194
const result = await proceed(args);
9295

93-
cache.setQueryResult(key, {
96+
cache.set(key, {
9497
createdAt: Date.now(),
9598
options,
9699
result,

packages/plugins/cache/src/providers/memory.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import type { CacheInvalidationOptions, CacheProvider, CacheQueryResultEntry } from '../types';
1+
import type { CacheInvalidationOptions, CacheProvider, CacheEntry } from '../types';
22
import { entryIsExpired } from '../utils';
33

4-
export class MemoryCache implements CacheProvider {
5-
private readonly queryResultStore: Map<string, CacheQueryResultEntry>;
4+
export class MemoryCacheProvider implements CacheProvider {
5+
private readonly entryStore: Map<string, CacheEntry>;
66
private readonly tagStore: Map<string, Set<string>>;
77

88
constructor(private readonly options?: MemoryCacheOptions) {
9-
this.queryResultStore = new Map<string, CacheQueryResultEntry>();
9+
this.entryStore = new Map<string, CacheEntry>();
1010
this.tagStore = new Map<string, Set<string>>;
1111

1212
setInterval(() => {
@@ -15,42 +15,42 @@ export class MemoryCache implements CacheProvider {
1515
}
1616

1717
private checkExpiration() {
18-
for (const [key, entry] of this.queryResultStore.entries()) {
18+
for (const [key, entry] of this.entryStore) {
1919
if (entryIsExpired(entry)) {
20-
this.queryResultStore.delete(key);
20+
this.entryStore.delete(key);
2121
}
2222
}
2323

24-
for (const [tag, queryKeys] of this.tagStore.entries()) {
25-
for (const queryKey of queryKeys) {
26-
if (!this.queryResultStore.has(queryKey)) {
27-
queryKeys.delete(queryKey);
24+
for (const [tag, keys] of this.tagStore) {
25+
for (const key of keys) {
26+
if (!this.entryStore.has(key)) {
27+
keys.delete(key);
2828
}
2929
}
3030

31-
if (queryKeys.size === 0) {
31+
if (keys.size === 0) {
3232
this.tagStore.delete(tag);
3333
}
3434
}
3535
}
3636

37-
getQueryResult(key: string) {
38-
return Promise.resolve(this.queryResultStore.get(key));
37+
get(key: string) {
38+
return Promise.resolve(this.entryStore.get(key));
3939
}
4040

41-
setQueryResult(key: string, entry: CacheQueryResultEntry) {
42-
this.queryResultStore.set(key, entry);
41+
set(key: string, entry: CacheEntry) {
42+
this.entryStore.set(key, entry);
4343

4444
if (entry.options.tags) {
4545
for (const tag of entry.options.tags) {
46-
let queryKeys = this.tagStore.get(tag);
46+
let keys = this.tagStore.get(tag);
4747

48-
if (!queryKeys) {
49-
queryKeys = new Set<string>();
50-
this.tagStore.set(tag, queryKeys);
48+
if (!keys) {
49+
keys = new Set<string>();
50+
this.tagStore.set(tag, keys);
5151
}
5252

53-
queryKeys.add(key);
53+
keys.add(key);
5454
}
5555
}
5656

@@ -60,11 +60,11 @@ export class MemoryCache implements CacheProvider {
6060
invalidate(options: CacheInvalidationOptions) {
6161
if (options.tags) {
6262
for (const tag of options.tags) {
63-
const queryKeys = this.tagStore.get(tag);
63+
const keys = this.tagStore.get(tag);
6464

65-
if (queryKeys) {
66-
for (const queryKey of queryKeys) {
67-
this.queryResultStore.delete(queryKey);
65+
if (keys) {
66+
for (const key of keys) {
67+
this.entryStore.delete(key);
6868
}
6969
}
7070
}
@@ -74,7 +74,7 @@ export class MemoryCache implements CacheProvider {
7474
}
7575

7676
invalidateAll() {
77-
this.queryResultStore.clear();
77+
this.entryStore.clear();
7878
this.tagStore.clear();
7979
return Promise.resolve();
8080
}

packages/plugins/cache/src/types.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import type { AllReadOperations } from '@zenstackhq/orm';
21
import type z from 'zod';
32
import type { cacheEnvelopeSchema, cacheOptionsSchema } from './schemas';
43

54
export type CacheEnvelope = z.infer<typeof cacheEnvelopeSchema>;
65
export type CacheOptions = z.infer<typeof cacheOptionsSchema>;
76

87
export interface CacheProvider {
9-
getQueryResult: (key: string) => Promise<CacheQueryResultEntry | undefined>;
10-
setQueryResult: (key: string, entry: CacheQueryResultEntry) => Promise<void>;
8+
get: (key: string) => Promise<CacheEntry | undefined>;
9+
set: (key: string, entry: CacheEntry) => Promise<void>;
1110
invalidate: (options: CacheInvalidationOptions) => Promise<void>;
1211
invalidateAll: () => Promise<void>;
1312
};
@@ -16,16 +15,20 @@ export type CacheInvalidationOptions = {
1615
tags?: string[];
1716
};
1817

19-
export type CachePluginQueryOptions = {
20-
[Op in AllReadOperations]: CacheEnvelope;
21-
};
22-
2318
export type CacheEntry = {
19+
/**
20+
* In unix epoch milliseconds.
21+
*/
2422
createdAt: number;
23+
24+
/**
25+
* The caching options that were passed to the query.
26+
*/
2527
options: CacheOptions;
26-
}
2728

28-
export type CacheQueryResultEntry = CacheEntry & {
29+
/**
30+
* The result of executing the query.
31+
*/
2932
result: unknown;
3033
};
3134

tests/e2e/orm/cache/memory.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { type ClientContract } from '@zenstackhq/orm';
22
import { createTestClient } from '@zenstackhq/testtools';
33
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
44
import { defineCachePlugin } from '@zenstackhq/plugin-cache';
5-
import { MemoryCache } from '@zenstackhq/plugin-cache/providers/memory';
5+
import { MemoryCacheProvider } from '@zenstackhq/plugin-cache/providers/memory';
66
import { schema } from '../schemas/basic';
77

88
describe('Cache plugin (memory)', () => {
@@ -20,7 +20,7 @@ describe('Cache plugin (memory)', () => {
2020

2121
it('respects ttl', async () => {
2222
const extDb = db.$use(defineCachePlugin({
23-
provider: new MemoryCache(),
23+
provider: new MemoryCacheProvider(),
2424
}));
2525

2626
const user = await extDb.user.create({
@@ -294,7 +294,7 @@ describe('Cache plugin (memory)', () => {
294294

295295
it('respects swr', async () => {
296296
const extDb = db.$use(defineCachePlugin({
297-
provider: new MemoryCache(),
297+
provider: new MemoryCacheProvider(),
298298
}));
299299

300300
const user = await extDb.user.create({
@@ -353,7 +353,7 @@ describe('Cache plugin (memory)', () => {
353353

354354
it('respects ttl and swr simultaneously', async () => {
355355
const extDb = db.$use(defineCachePlugin({
356-
provider: new MemoryCache(),
356+
provider: new MemoryCacheProvider(),
357357
}));
358358

359359
const user = await extDb.user.create({
@@ -431,7 +431,7 @@ describe('Cache plugin (memory)', () => {
431431

432432
it('supports invalidating all entries', async () => {
433433
const extDb = db.$use(defineCachePlugin({
434-
provider: new MemoryCache(),
434+
provider: new MemoryCacheProvider(),
435435
}));
436436

437437
const user = await extDb.user.create({
@@ -617,7 +617,7 @@ describe('Cache plugin (memory)', () => {
617617

618618
it('supports invalidating by tags', async () => {
619619
const extDb = db.$use(defineCachePlugin({
620-
provider: new MemoryCache(),
620+
provider: new MemoryCacheProvider(),
621621
}));
622622

623623
const user1 = await extDb.user.create({
@@ -870,7 +870,7 @@ describe('Cache plugin (memory)', () => {
870870

871871
it('handles edge cases', async () => {
872872
const extDb = db.$use(defineCachePlugin({
873-
provider: new MemoryCache(),
873+
provider: new MemoryCacheProvider(),
874874
}));
875875

876876
await expect(extDb.user.findMany({

0 commit comments

Comments
 (0)