Refactor Redis client implementation in redis.ts to use Bun's RedisClient, improving connection management and error handling. Remove unused clearCache method from data-processor.ts to streamline code.

This commit is contained in:
Luke Hagar
2025-10-21 00:06:17 -05:00
parent 38aa9ae169
commit c88f419b11
2 changed files with 18 additions and 59 deletions

View File

@@ -101,7 +101,6 @@ export class DataProcessor {
// Mark processed and clear cache
await this.cache.set(processedKey, true, 60 * 60 * 24 * 14); // remember for 14 days
await this.clearCache();
console.log('ETL process completed successfully');
return results;
@@ -281,19 +280,6 @@ export class DataProcessor {
return results;
}
/**
* Clear all cache after data update
*/
private async clearCache(): Promise<void> {
console.log('Clearing cache after data update');
try {
await this.cache.flush();
console.log('Cache cleared successfully');
} catch (error) {
console.error('Error clearing cache:', error);
}
}
// Helper methods
private getYesterdayDate(): string {
const yesterday = new Date();

View File

@@ -1,39 +1,28 @@
import { env } from '$env/dynamic/private';
import { createClient } from 'redis';
import { RedisClient } from 'bun';
// Redis client instance
let redisClient: ReturnType<typeof createClient> | null = null;
let redisClient: RedisClient | null = null;
let isConnecting = false;
let isDisconnecting = false;
export function getRedisClient() {
const redisUrl = env.REDIS_URL;
if (!redisClient && !isConnecting) {
isConnecting = true;
redisClient = createClient({
url: redisUrl,
});
redisClient = new RedisClient();
redisClient.on('error', (err: any) => {
console.error('Redis Client Error:', err);
});
redisClient.on('connect', () => {
redisClient.onconnect = () => {
console.log('Redis Client Connected');
isConnecting = false;
});
}
redisClient.on('disconnect', () => {
console.log('Redis Client Disconnected');
});
redisClient.on('end', () => {
console.log('Redis Client Connection Ended');
redisClient.onclose = (error: any) => {
console.log('Redis Client Connection Ended', error);
redisClient = null;
isConnecting = false;
isDisconnecting = false;
});
}
redisClient.connect().catch((error) => {
console.error('Redis Client Connection Failed:', error);
@@ -51,7 +40,7 @@ export async function closeRedisClient(): Promise<void> {
isDisconnecting = true;
try {
console.log('Closing Redis client connection...');
await redisClient.quit();
redisClient.close();
console.log('Redis client connection closed successfully');
} catch (error) {
console.error('Error closing Redis client:', error);
@@ -70,7 +59,7 @@ export async function forceDisconnectRedis(): Promise<void> {
isDisconnecting = true;
try {
console.log('Force disconnecting Redis client...');
await redisClient.destroy();
redisClient.close();
console.log('Redis client force disconnected');
} catch (error) {
console.error('Error force disconnecting Redis client:', error);
@@ -108,7 +97,7 @@ export class CacheManager {
console.warn('Redis client not available for set operation');
return;
}
await client.setEx(key, ttl, JSON.stringify(value));
await client.setex(key, ttl, JSON.stringify(value));
} catch (error) {
console.error('Redis set error:', error);
}
@@ -135,26 +124,13 @@ export class CacheManager {
return false;
}
const result = await client.exists(key);
return result === 1;
return result
} catch (error) {
console.error('Redis exists error:', error);
return false;
}
}
async flush(): Promise<void> {
try {
const client = this.client;
if (!client) {
console.warn('Redis client not available for flush operation');
return;
}
await client.flushDb();
} catch (error) {
console.error('Redis flush error:', error);
}
}
// Cache key generators
static getPackageKey(packageName: string, type: string): string {
return `pypistats:package:${packageName}:${type}`;
@@ -190,7 +166,7 @@ export class LockManager {
return null;
}
const token = `${Date.now()}-${Math.random().toString(36).slice(2)}`;
const result = await client.set(key, token, { NX: true, EX: ttlSeconds });
const result = await client.setex(key, ttlSeconds, token);
return result === 'OK' ? token : null;
} catch (error) {
console.error('Redis acquireLock error:', error);
@@ -216,11 +192,8 @@ export class LockManager {
return 0
end
`;
const res = (await client.eval(lua, {
keys: [key],
arguments: [token]
})) as number;
return res === 1;
const res = await client.send(lua, [key, token]);
return res
} catch (error) {
console.error('Redis releaseLock error:', error);
return false;
@@ -292,7 +265,7 @@ export class SessionManager {
console.warn('Redis client not available for set session');
return;
}
await client.setEx(sessionId, this.defaultTTL, JSON.stringify(data));
await client.setex(sessionId, this.defaultTTL, JSON.stringify(data));
} catch (error) {
console.error('Set session error:', error);
}
@@ -335,7 +308,7 @@ export class SessionManager {
}
const data = await client.get(sessionId);
if (data) {
await client.setEx(sessionId, this.defaultTTL, data);
await client.setex(sessionId, this.defaultTTL, data);
}
} catch (error) {
console.error('Refresh session error:', error);