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

Commit fc2f9ee

Browse files
committed
Add murmurhash.
1 parent 2f37344 commit fc2f9ee

4 files changed

Lines changed: 25 additions & 24 deletions

File tree

packages/plugins/cache/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@
4444
"dependencies": {
4545
"@zenstackhq/common-helpers": "workspace:*",
4646
"@zenstackhq/orm": "workspace:*",
47-
"zod": "catalog:",
48-
"json-stable-stringify": "^1.3.0"
47+
"json-stable-stringify": "^1.3.0",
48+
"murmurhash": "^2.0.1",
49+
"zod": "catalog:"
4950
},
5051
"devDependencies": {
5152
"@zenstackhq/eslint-config": "workspace:*",

packages/plugins/cache/src/plugin.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { lowerCaseFirst } from '@zenstackhq/common-helpers';
12
import { definePlugin } from '@zenstackhq/orm';
23
import stableStringify from 'json-stable-stringify';
4+
import murmurhash from 'murmurhash';
35
import { cacheEnvelopeSchema } from './schemas';
46
import type { CacheEnvelope, CacheInvalidationOptions, CachePluginOptions } from './types';
57

@@ -27,25 +29,19 @@ export function defineCachePlugin(pluginOptions: CachePluginOptions) {
2729

2830
onQuery: async ({ args, model, operation, proceed }) => {
2931
if (args && 'cache' in args) {
30-
const argsWithoutCache: Record<string, unknown> = {};
32+
const json = stableStringify({
33+
args,
34+
model,
35+
operation,
36+
});
3137

32-
for (const [key, value] of Object.entries(args)) {
33-
if (key !== 'cache') {
34-
argsWithoutCache[key] = value;
35-
}
38+
if (!json) {
39+
throw new Error(`Failed to serialize cache entry for ${lowerCaseFirst(model)}.${operation}`);
3640
}
3741

3842
const cache = pluginOptions.provider;
3943
const options = (args as CacheEnvelope).cache!;
40-
41-
// TODO: hash
42-
const key = stableStringify({
43-
...argsWithoutCache,
44-
options,
45-
model,
46-
operation,
47-
})!;
48-
44+
const key = murmurhash.v3(json).toString();
4945
const queryResultEntry = await cache.getQueryResult(key);
5046

5147
if (queryResultEntry) {

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class MemoryCache implements CacheProvider {
1717
private checkExpiration() {
1818
for (const [key, entry] of this.queryResultStore.entries()) {
1919
if (entryIsExpired(entry)) {
20-
this.delete(key);
20+
this.queryResultStore.delete(key);
2121
}
2222
}
2323

@@ -57,10 +57,6 @@ export class MemoryCache implements CacheProvider {
5757
return Promise.resolve();
5858
}
5959

60-
delete(key: string) {
61-
return Promise.resolve(this.queryResultStore.delete(key));
62-
}
63-
6460
invalidate(options: CacheInvalidationOptions) {
6561
if (options.tags) {
6662
for (const tag of options.tags) {

pnpm-lock.yaml

Lines changed: 11 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)