full upgrade

This commit is contained in:
2026-01-07 23:13:25 +01:00
parent 4cd3f60c98
commit c5efd28383
23 changed files with 693 additions and 226 deletions

View File

@@ -1,35 +1,101 @@
import { createClient } from 'redis';
let redisClient: ReturnType<typeof createClient> | null = null;
let connectionFailed = false; // Track if connection has permanently failed
// Helper to check if error is connection refused
const isConnectionRefused = (err: any): boolean => {
if (!err) return false;
// Check direct properties
if (err.code === 'ECONNREFUSED' || err.message?.includes('ECONNREFUSED')) {
return true;
}
// Check AggregateError
if (err.errors && Array.isArray(err.errors)) {
return err.errors.some((e: any) => e?.code === 'ECONNREFUSED' || e?.message?.includes('ECONNREFUSED'));
}
// Check nested error
if (err.cause) {
return isConnectionRefused(err.cause);
}
return false;
};
export const getRedisClient = async () => {
// If Redis URL is not configured, return null instead of trying to connect
if (!process.env.REDIS_URL) {
return null;
}
// If connection has already failed, don't try again
if (connectionFailed) {
return null;
}
if (!redisClient) {
const redisUrl = process.env.REDIS_URL || 'redis://localhost:6379';
const redisUrl = process.env.REDIS_URL;
redisClient = createClient({
url: redisUrl,
socket: {
reconnectStrategy: (retries) => Math.min(retries * 50, 1000)
try {
redisClient = createClient({
url: redisUrl,
socket: {
reconnectStrategy: (retries) => {
// Stop trying after 1 attempt to avoid spam
if (retries > 1) {
connectionFailed = true;
return false;
}
return false; // Don't reconnect automatically
}
}
});
redisClient.on('error', (err: any) => {
// Silently handle connection refused errors - Redis is optional
if (isConnectionRefused(err)) {
connectionFailed = true;
return; // Don't log connection refused errors
}
// Only log non-connection-refused errors
console.error('Redis Client Error:', err);
});
redisClient.on('connect', () => {
console.log('Redis Client Connected');
connectionFailed = false; // Reset on successful connection
});
redisClient.on('ready', () => {
console.log('Redis Client Ready');
connectionFailed = false; // Reset on ready
});
redisClient.on('end', () => {
console.log('Redis Client Disconnected');
});
await redisClient.connect().catch((err: any) => {
// Connection failed
if (isConnectionRefused(err)) {
connectionFailed = true;
// Silently handle connection refused - Redis is optional
} else {
// Only log non-connection-refused errors
console.error('Redis connection failed:', err);
}
redisClient = null;
});
} catch (error: any) {
// If connection fails, set to null
if (isConnectionRefused(error)) {
connectionFailed = true;
}
});
redisClient.on('error', (err) => {
console.error('Redis Client Error:', err);
});
redisClient.on('connect', () => {
console.log('Redis Client Connected');
});
redisClient.on('ready', () => {
console.log('Redis Client Ready');
});
redisClient.on('end', () => {
console.log('Redis Client Disconnected');
});
await redisClient.connect();
redisClient = null;
}
}
return redisClient;
@@ -47,10 +113,11 @@ export const cache = {
async get(key: string) {
try {
const client = await getRedisClient();
if (!client) return null;
const value = await client.get(key);
return value ? JSON.parse(value) : null;
} catch (error) {
console.error('Redis GET error:', error);
// Silently fail if Redis is not available
return null;
}
},
@@ -58,10 +125,11 @@ export const cache = {
async set(key: string, value: unknown, ttlSeconds = 3600) {
try {
const client = await getRedisClient();
if (!client) return false;
await client.setEx(key, ttlSeconds, JSON.stringify(value));
return true;
} catch (error) {
console.error('Redis SET error:', error);
// Silently fail if Redis is not available
return false;
}
},
@@ -69,10 +137,11 @@ export const cache = {
async del(key: string) {
try {
const client = await getRedisClient();
if (!client) return false;
await client.del(key);
return true;
} catch (error) {
console.error('Redis DEL error:', error);
// Silently fail if Redis is not available
return false;
}
},
@@ -80,9 +149,10 @@ export const cache = {
async exists(key: string) {
try {
const client = await getRedisClient();
if (!client) return false;
return await client.exists(key);
} catch (error) {
console.error('Redis EXISTS error:', error);
// Silently fail if Redis is not available
return false;
}
},
@@ -90,10 +160,11 @@ export const cache = {
async flush() {
try {
const client = await getRedisClient();
if (!client) return false;
await client.flushAll();
return true;
} catch (error) {
console.error('Redis FLUSH error:', error);
// Silently fail if Redis is not available
return false;
}
}
@@ -146,13 +217,14 @@ export const analyticsCache = {
async clearAll() {
try {
const client = await getRedisClient();
if (!client) return;
// Clear all analytics-related keys
const keys = await client.keys('analytics:*');
if (keys.length > 0) {
await client.del(keys);
}
} catch (error) {
console.error('Error clearing analytics cache:', error);
// Silently fail if Redis is not available
}
}
};