add better access control
This commit is contained in:
17
packages/redis/cachable.ts
Normal file
17
packages/redis/cachable.ts
Normal 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;
|
||||
};
|
||||
}
|
||||
@@ -1,10 +1,2 @@
|
||||
import type { RedisOptions } from 'ioredis';
|
||||
import Redis from 'ioredis';
|
||||
|
||||
const options: RedisOptions = {
|
||||
connectTimeout: 10000,
|
||||
};
|
||||
|
||||
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);
|
||||
export * from './redis';
|
||||
export * from './cachable';
|
||||
|
||||
10
packages/redis/redis.ts
Normal file
10
packages/redis/redis.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import type { RedisOptions } from 'ioredis';
|
||||
import Redis from 'ioredis';
|
||||
|
||||
const options: RedisOptions = {
|
||||
connectTimeout: 10000,
|
||||
};
|
||||
|
||||
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);
|
||||
Reference in New Issue
Block a user