chore: Implement short-lived redis cache for slots (#22787)

* 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 <[email protected]>
This commit is contained in:
Alex van Andelandcal.com authored and GitHub committed 2025-08-05 17:32:02 +00:00
1 parent a71d949a0c
commit b71d8baccc
19 files changed
+276 -41

No files matched your search

+1 -1
View File
@@ -1,7 +1,7 @@
export interface IRedisService {
get: <TData>(key: string) => Promise<TData | null>;
set: <TData>(key: string, value: TData) => Promise<"OK" | TData | null>;
set: <TData>(key: string, value: TData, opts?: { ttl?: number }) => Promise<"OK" | TData | null>;
expire: (key: string, seconds: number) => Promise<0 | 1>;
@@ -0,0 +1,32 @@
import type { IRedisService } from "./IRedisService";
/**
* Noop implementation of IRedisService for testing or fallback scenarios.
*/
export class NoopRedisService implements IRedisService {
async get<TData>(_key: string): Promise<TData | null> {
return null;
}
async del(_key: string): Promise<number> {
return 0;
}
async set<TData>(_key: string, _value: TData, _opts?: { ttl?: number }): Promise<"OK" | TData | null> {
return "OK";
}
async expire(_key: string, _seconds: number): Promise<0 | 1> {
// Implementation for setting expiration time for key in Redis
return 0;
}
async lrange<TResult = string>(_key: string, _start: number, _end: number): Promise<TResult[]> {
return [];
}
async lpush<TData>(_key: string, ..._elements: TData[]): Promise<number> {
return 0;
}
}
+17 -4
View File
@@ -6,7 +6,13 @@ export class RedisService implements IRedisService {
private redis: Redis;
constructor() {
this.redis = Redis.fromEnv();
// 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> {
@@ -17,9 +23,16 @@ export class RedisService implements IRedisService {
return this.redis.del(key);
}
async set<TData>(key: string, value: TData): Promise<"OK" | TData | null> {
// Implementation for setting value in Redis
return this.redis.set(key, value);
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> {
+17
View File
@@ -0,0 +1,17 @@
import { createModule } from "@evyweb/ioctopus";
import { DI_TOKENS } from "@calcom/lib/di/tokens";
import { NoopRedisService } from "../NoopRedisService";
import { RedisService } from "../RedisService";
const redisModule = createModule();
redisModule.bind(DI_TOKENS.REDIS_CLIENT).toFactory(() => {
if (process.env.UPSTASH_REDIS_REST_URL && process.env.UPSTASH_REDIS_REST_TOKEN) {
return new RedisService();
}
return new NoopRedisService();
}, "singleton");
export { redisModule };