Files
formbricks-formbricks/apps/web/modules/cache/lib/service.ts
Matti Nannt c1a35e2d75 chore: introduce new reliable cache for enterprise license check (#5740)
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
2025-05-12 10:41:53 +02:00

56 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import "server-only";
import KeyvRedis from "@keyv/redis";
import { type Cache, createCache } from "cache-manager";
import { Keyv } from "keyv";
import { logger } from "@formbricks/logger";
const CACHE_TTL_SECONDS = 60 * 60 * 24; // 24 hours
const CACHE_TTL_MS = CACHE_TTL_SECONDS * 1000;
let cache: Cache;
const initializeMemoryCache = (): void => {
const memoryKeyvStore = new Keyv({
ttl: CACHE_TTL_MS,
});
cache = createCache({
stores: [memoryKeyvStore],
ttl: CACHE_TTL_MS,
});
logger.info("Using in-memory cache");
};
if (process.env.REDIS_URL) {
try {
const redisStore = new KeyvRedis(process.env.REDIS_URL);
// Gracefully fall back if Redis dies later on
redisStore.on("error", (err) => {
logger.error("Redis connection lost switching to in-memory cache", { error: err });
initializeMemoryCache();
});
const redisKeyvStore = new Keyv({
store: redisStore,
ttl: CACHE_TTL_MS,
});
cache = createCache({
stores: [redisKeyvStore],
ttl: CACHE_TTL_MS,
});
logger.info("Successfully connected to Redis cache");
} catch (error) {
logger.error("Failed to connect to Redis cache:", error);
logger.warn("Falling back to in-memory cache due to Redis connection failure");
initializeMemoryCache();
}
} else {
logger.warn("REDIS_URL not found, falling back to in-memory cache.");
initializeMemoryCache();
}
export const getCache = (): Cache => {
return cache;
};