-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction-cache.ts
More file actions
107 lines (93 loc) · 3.72 KB
/
Copy pathfunction-cache.ts
File metadata and controls
107 lines (93 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* Function cache example: withCache + FunctionCache.
* See: docs/function-cache.md
*
* Run:
* npm run build && tsc -p tsconfig.examples.json
* node .generated/examples-dist/examples/docs/function-cache.js
*/
import MonSQLize from 'monsqlize';
import { setupExample, teardownExample } from '../helpers/bootstrap.js';
interface UserDoc {
userId: string;
plan: string;
}
interface OrderDoc {
userId: string;
total: number;
}
interface Dashboard {
userId: string;
plan: string;
orderCount: number;
total: number;
}
async function main() {
const { msq, server } = await setupExample('example-function-cache');
try {
const cache = new MonSQLize.MemoryCache({ maxEntries: 100, defaultTtl: 60_000 });
const users = msq.collection<UserDoc>('function_cache_users');
const orders = msq.collection<OrderDoc>('function_cache_orders');
await users.insertMany([
{ userId: 'u1', plan: 'pro' },
{ userId: 'u2', plan: 'free' },
]);
await orders.insertMany([
{ userId: 'u1', total: 25 },
{ userId: 'u1', total: 40 },
{ userId: 'u2', total: 5 },
]);
let dashboardLoads = 0;
const loadDashboard = MonSQLize.withCache(async (userId: string): Promise<Dashboard> => {
dashboardLoads += 1;
const user = await users.findOne({ userId });
const rows = await orders.find({ userId });
return {
userId,
plan: user?.plan ?? 'free',
orderCount: rows.length,
total: rows.reduce((sum, order) => sum + order.total, 0),
};
}, {
cache,
namespace: 'dashboard',
ttl: 60_000,
keyBuilder: (userId: string) => `user:${userId}`,
condition: (result: Dashboard) => result.orderCount > 0,
});
const dashboardA = await loadDashboard('u1');
const dashboardB = await loadDashboard('u1');
const dashboardStats = loadDashboard.stats();
console.log('withCache dashboard loads:', dashboardLoads);
console.log('withCache dashboard total:', dashboardA.total, dashboardB.total);
console.log('withCache stats:', `hits=${dashboardStats.hits}`, `misses=${dashboardStats.misses}`);
const functionCache = new MonSQLize.FunctionCache(cache, {
namespace: 'analytics',
ttl: 60_000,
});
let totalLoads = 0;
await functionCache.register('orderTotal', async (...args: unknown[]) => {
totalLoads += 1;
const userId = args[0] as string;
const rows = await orders.find({ userId });
return rows.reduce((sum, order) => sum + order.total, 0);
}, {
keyBuilder: (userId: unknown) => `total:${String(userId)}`,
});
const totalA = await functionCache.execute('orderTotal', 'u1') as number;
const totalB = await functionCache.execute('orderTotal', 'u1') as number;
await functionCache.invalidate('orderTotal', 'u1');
const totalC = await functionCache.execute('orderTotal', 'u1') as number;
const stats = functionCache.getStats('orderTotal') as { hits: number; misses: number; calls: number };
console.log('FunctionCache totals:', totalA, totalB, totalC);
console.log('FunctionCache loads after invalidate:', totalLoads);
console.log('FunctionCache stats:', `hits=${stats.hits}`, `misses=${stats.misses}`, `calls=${stats.calls}`);
} finally {
await teardownExample(msq, server);
}
console.log('Function cache example complete');
}
main().catch((err) => {
console.error('Example failed:', err);
process.exit(1);
});