* chore: Implement short-lived redis cache for slots * chore: adapt apiv2 redis service to match with upstash redis * chore: safer redis service and ms ttl * fixup! chore: safer redis service and ms ttl * Wrap with timeout, currently doesn't work yet * Updated @upstash/redis for better signal support * Fix type errors, remove ts value * Inject NoopRedisService for NODE_ENV test * chore: bump platform libs * chore: bump platform libs * Upstash Redis upgrade no longer resulted in expected hard crash on init, so updated factory and our Upstash Redis Adapter to mimick old behaviour * Add SLOTS_CACHE_TTL variable for configurable ttl on slots cache * Update parseInt to use right types * chore: bump platform libs * chore: bump platform libs * chore: bump platform libs * update e2e api v2 action * set SLOTS_CACHE_TTL env var api v2 e2e --------- Co-authored-by: cal.com <morgan@cal.com>
51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
import { Redis } from "@upstash/redis";
|
|
|
|
import type { IRedisService } from "./IRedisService";
|
|
|
|
export class RedisService implements IRedisService {
|
|
private redis: Redis;
|
|
|
|
constructor() {
|
|
// Ensure we throw an Error to mimick old behavior
|
|
if (!process.env.UPSTASH_REDIS_REST_URL || !process.env.UPSTASH_REDIS_REST_TOKEN) {
|
|
throw new Error("Attempted to initialize Upstash Redis client without url or token.");
|
|
}
|
|
this.redis = Redis.fromEnv({
|
|
signal: () => AbortSignal.timeout(2000),
|
|
});
|
|
}
|
|
|
|
async get<TData>(key: string): Promise<TData | null> {
|
|
return this.redis.get(key);
|
|
}
|
|
|
|
async del(key: string): Promise<number> {
|
|
return this.redis.del(key);
|
|
}
|
|
|
|
async set<TData>(key: string, value: TData, opts?: { ttl?: number }): Promise<"OK" | TData | null> {
|
|
return this.redis.set(
|
|
key,
|
|
value,
|
|
opts?.ttl
|
|
? {
|
|
px: opts.ttl,
|
|
}
|
|
: undefined
|
|
);
|
|
}
|
|
|
|
async expire(key: string, seconds: number): Promise<0 | 1> {
|
|
// Implementation for setting expiration time for key in Redis
|
|
return this.redis.expire(key, seconds);
|
|
}
|
|
|
|
async lrange<TResult = string>(key: string, start: number, end: number): Promise<TResult[]> {
|
|
return this.redis.lrange(key, start, end);
|
|
}
|
|
|
|
async lpush<TData>(key: string, ...elements: TData[]): Promise<number> {
|
|
return this.redis.lpush(key, elements);
|
|
}
|
|
}
|