add better access control

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-06-05 23:47:45 +02:00
parent 68c4530ea5
commit 1e6cd0dee2
17 changed files with 309 additions and 34 deletions

View File

@@ -0,0 +1,17 @@
import { redis } from './redis';
export function cacheable<T extends (...args: any) => any>(
fn: T,
expire: number
) {
return async function (...args: Parameters<T>): Promise<ReturnType<T>> {
const key = `cachable:${fn.name}:${JSON.stringify(args)}`;
const cached = await redis.get(key);
if (cached) {
return JSON.parse(cached);
}
const result = await fn(...(args as any));
redis.setex(key, expire, JSON.stringify(result));
return result;
};
}