batching events

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-07-17 17:13:07 +02:00
committed by Carl-Gerhard Lindesvärd
parent 244aa3b0d3
commit 5e225b7ae6
58 changed files with 2204 additions and 583 deletions

View File

@@ -4,14 +4,25 @@ export function cacheable<T extends (...args: any) => any>(
fn: T,
expire: number
) {
return async function (...args: Parameters<T>): Promise<ReturnType<T>> {
return async function (
...args: Parameters<T>
): Promise<Awaited<ReturnType<T>>> {
// JSON.stringify here is not bullet proof since ordering of object keys matters etc
const key = `cachable:${fn.name}:${JSON.stringify(args)}`;
const cached = await redis.get(key);
if (cached) {
return JSON.parse(cached);
try {
return JSON.parse(cached);
} catch (e) {
console.error('Failed to parse cache', e);
}
}
const result = await fn(...(args as any));
redis.setex(key, expire, JSON.stringify(result));
if (result !== undefined || result !== null) {
redis.setex(key, expire, JSON.stringify(result));
}
return result;
};
}

View File

@@ -9,7 +9,7 @@
"with-env": "dotenv -e ../../.env -c --"
},
"dependencies": {
"ioredis": "^5.3.2"
"ioredis": "^5.4.1"
},
"devDependencies": {
"@openpanel/eslint-config": "workspace:*",
@@ -28,4 +28,4 @@
]
},
"prettier": "@openpanel/prettier-config"
}
}

View File

@@ -2,9 +2,12 @@ import type { RedisOptions } from 'ioredis';
import Redis from 'ioredis';
const options: RedisOptions = {
connectTimeout: 10000,
connectTimeout: 30000,
maxRetriesPerRequest: null,
};
export { Redis };
export const redis = new Redis(process.env.REDIS_URL!, options);
export const redisSub = new Redis(process.env.REDIS_URL!, options);
export const redisPub = new Redis(process.env.REDIS_URL!, options);