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

Commit 84d1e60

Browse files
authored
perf(orm): more aggressive caching of validation zod schemas (#623)
* WIP: more aggressive caching of validation zod schemas * refactor: use a decorator-based approach for caching * update * update * update * update * update * remove object-type args from cache key * update cache key
1 parent 8fdd349 commit 84d1e60

17 files changed

Lines changed: 416 additions & 183 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import stableStringify from 'json-stable-stringify';
2+
3+
/**
4+
* Method decorator that caches the return value based on method name and arguments.
5+
*
6+
* Requirements:
7+
* - Class must have a `getCache(key: string)` method
8+
* - Class must have a `setCache(key: string, value: any)` method
9+
*/
10+
export function cache() {
11+
return function (_target: any, propertyKey: string, descriptor: PropertyDescriptor) {
12+
const originalMethod = descriptor.value;
13+
14+
descriptor.value = function (
15+
this: {
16+
getCache: (key: string) => unknown;
17+
setCache: (key: string, value: unknown) => void;
18+
} & Record<string, unknown>,
19+
...args: any[]
20+
) {
21+
// Build cache key object
22+
const cacheKeyObj: Record<string, unknown> = {
23+
$call: propertyKey,
24+
$args: args.map((arg) => {
25+
if (Array.isArray(arg)) {
26+
// sort array arguments for consistent cache keys
27+
return [...arg].sort();
28+
} else {
29+
return arg;
30+
}
31+
}),
32+
};
33+
34+
// Generate stable string key
35+
const cacheKey = stableStringify(cacheKeyObj)!;
36+
37+
// Check cache
38+
const cached = this.getCache(cacheKey);
39+
if (cached !== undefined) {
40+
return cached;
41+
}
42+
43+
// Execute original method
44+
const result = originalMethod.apply(this, args);
45+
46+
// Store in cache
47+
this.setCache(cacheKey, result);
48+
49+
return result;
50+
};
51+
52+
return descriptor;
53+
};
54+
}

0 commit comments

Comments
 (0)