Files
stats/packages/redis/redis.ts
Carl-Gerhard Lindesvärd 81a7e5d62e feat: dashboard v2, esm, upgrades (#211)
* esm

* wip

* wip

* wip

* wip

* wip

* wip

* subscription notice

* wip

* wip

* wip

* fix envs

* fix: update docker build

* fix

* esm/types

* delete dashboard :D

* add patches to dockerfiles

* update packages + catalogs + ts

* wip

* remove native libs

* ts

* improvements

* fix redirects and fetching session

* try fix favicon

* fixes

* fix

* order and resize reportds within a dashboard

* improvements

* wip

* added userjot to dashboard

* fix

* add op

* wip

* different cache key

* improve date picker

* fix table

* event details loading

* redo onboarding completely

* fix login

* fix

* fix

* extend session, billing and improve bars

* fix

* reduce price on 10M
2025-10-16 12:27:44 +02:00

126 lines
2.8 KiB
TypeScript

import { getSuperJson, setSuperJson } from '@openpanel/json';
import type { RedisOptions } from 'ioredis';
import { Redis } from 'ioredis';
const options: RedisOptions = {
connectTimeout: 10000,
};
export { Redis };
const REDIS_URL = process.env.REDIS_URL || 'redis://localhost:6379';
export interface ExtendedRedis extends Redis {
getJson: <T = any>(key: string) => Promise<T | null>;
setJson: <T = any>(
key: string,
expireInSec: number,
value: T,
) => Promise<void>;
}
const createRedisClient = (
url: string,
overrides: RedisOptions = {},
): ExtendedRedis => {
const client = new Redis(url, {
...options,
...overrides,
}) as ExtendedRedis;
client.on('error', (error) => {
console.error('Redis Client Error:', error);
});
client.getJson = async <T = any>(key: string): Promise<T | null> => {
const value = await client.get(key);
if (value) {
const res = getSuperJson(value) as T;
if (res && Array.isArray(res) && res.length === 0) {
return null;
}
if (res && typeof res === 'object' && Object.keys(res).length === 0) {
return null;
}
if (res) {
return res;
}
}
return null;
};
client.setJson = async <T = any>(
key: string,
expireInSec: number,
value: T,
): Promise<void> => {
await client.setex(key, expireInSec, setSuperJson(value));
};
return client;
};
let redisCache: ExtendedRedis;
export function getRedisCache() {
if (!redisCache) {
redisCache = createRedisClient(REDIS_URL, options);
}
return redisCache;
}
let redisSub: ExtendedRedis;
export function getRedisSub() {
if (!redisSub) {
redisSub = createRedisClient(REDIS_URL, options);
}
return redisSub;
}
let redisPub: ExtendedRedis;
export function getRedisPub() {
if (!redisPub) {
redisPub = createRedisClient(REDIS_URL, options);
}
return redisPub;
}
let redisQueue: ExtendedRedis;
export function getRedisQueue() {
if (!redisQueue) {
// Use different redis for queues (self-hosting will re-use the same redis instance)
redisQueue = createRedisClient(REDIS_URL, {
...options,
enableReadyCheck: false,
maxRetriesPerRequest: null,
enableOfflineQueue: true,
});
}
return redisQueue;
}
let redisGroupQueue: ExtendedRedis;
export function getRedisGroupQueue() {
if (!redisGroupQueue) {
// Dedicated Redis connection for GroupWorker to avoid blocking BullMQ
redisGroupQueue = createRedisClient(REDIS_URL, {
...options,
enableReadyCheck: false,
maxRetriesPerRequest: null,
enableOfflineQueue: true,
});
}
return redisGroupQueue;
}
export async function getLock(key: string, value: string, timeout: number) {
const lock = await getRedisCache().set(key, value, 'PX', timeout, 'NX');
return lock === 'OK';
}