forked from stackwiseai/stackwise
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetMethodNameFromCache.ts
More file actions
45 lines (39 loc) · 1.41 KB
/
getMethodNameFromCache.ts
File metadata and controls
45 lines (39 loc) · 1.41 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
import getMethodName from '../stack/getMethodName';
import { combineSkeleton } from '../stack/createSkeleton';
const storage = require('node-persist');
const fs = require('fs');
const path = require('path');
export default async function getMethodNameWithCache(
briefSkeleton: string,
functionAndOutputSkeleton: string,
brief
) {
const wholeSkeleton = combineSkeleton(
briefSkeleton,
functionAndOutputSkeleton
);
await storage.init({
dir: path.join(__dirname, '.stack'),
stringify: JSON.stringify,
parse: JSON.parse,
encoding: 'utf8',
logging: false, // can also be custom logging function
ttl: false, // ttl* [NEW], can be used for expiring records
expiredInterval: 2 * 60 * 60 * 1000, // every 2 hours the process will clean-up the expired cache
forgiveParseErrors: false,
});
// The cache key is a concatenation of `wholeSkeleton` and the name of the method
const cacheKey =
wholeSkeleton + (await getMethodName({ wholeSkeleton, brief }));
console.log(`cacheKey: ${cacheKey}`);
// Try to read the cache first
let methodName = await storage.getItem(cacheKey);
console.log(`methodName: ${methodName}`);
// If it's not in the cache, call the actual function
if (!methodName) {
methodName = await getMethodName({ wholeSkeleton, brief });
// Then update the cache with the new value
await storage.setItem(cacheKey, methodName);
}
return methodName;
}