Files
defi-arbitrage/services/cache/cache.service.ts
DBIS Core Team 73e8d30190 chore: sync state and push to Gitea
Made-with: Cursor
2026-03-02 13:17:20 -08:00

165 lines
4.5 KiB
TypeScript

// Cache Service - Redis Integration
// Caches RPC responses and risk calculations
import { logger } from '@/infrastructure/monitoring/logger';
// Placeholder for Redis client
interface RedisClient {
get: (key: string) => Promise<string | null>;
set: (key: string, value: string, ttl?: number) => Promise<void>;
del: (key: string) => Promise<void>;
exists: (key: string) => Promise<number>;
}
export interface CacheConfig {
enabled: boolean;
defaultTtl: number; // seconds
priceDataTtl: number;
riskCalcTtl: number;
exchangeRateTtl: number;
}
export class CacheService {
private redis: RedisClient | null = null;
private config: CacheConfig = {
enabled: process.env.REDIS_ENABLED === 'true',
defaultTtl: 300, // 5 minutes
priceDataTtl: 60, // 1 minute
riskCalcTtl: 300, // 5 minutes
exchangeRateTtl: 30, // 30 seconds
};
constructor(redisClient?: RedisClient) {
if (redisClient) {
this.redis = redisClient;
} else if (this.config.enabled && process.env.REDIS_URL) {
try {
const Redis = require('ioredis');
this.redis = new Redis(process.env.REDIS_URL, { maxRetriesPerRequest: 2 }) as unknown as RedisClient;
} catch {
logger.warn('Redis (ioredis) not available - caching disabled. Install ioredis and set REDIS_URL for cache.');
this.config.enabled = false;
}
} else if (this.config.enabled) {
logger.warn('REDIS_URL not set - caching disabled');
this.config.enabled = false;
}
}
/**
* Get cached value
*/
async get<T>(key: string): Promise<T | null> {
if (!this.config.enabled || !this.redis) {
return null;
}
try {
const value = await this.redis.get(key);
if (value) {
return JSON.parse(value) as T;
}
return null;
} catch (error) {
logger.error('Cache get error', { key, error });
return null;
}
}
/**
* Set cached value
*/
async set(key: string, value: any, ttl?: number): Promise<void> {
if (!this.config.enabled || !this.redis) {
return;
}
try {
const serialized = JSON.stringify(value);
await this.redis.set(key, serialized, ttl || this.config.defaultTtl);
} catch (error) {
logger.error('Cache set error', { key, error });
}
}
/**
* Delete cached value
*/
async delete(key: string): Promise<void> {
if (!this.config.enabled || !this.redis) {
return;
}
try {
await this.redis.del(key);
} catch (error) {
logger.error('Cache delete error', { key, error });
}
}
/**
* Get cached price
*/
async getCachedPrice(tokenAddress: string): Promise<string | null> {
return this.get<string>(`price:${tokenAddress}`);
}
/**
* Set cached price
*/
async setCachedPrice(tokenAddress: string, price: string): Promise<void> {
await this.set(`price:${tokenAddress}`, price, this.config.priceDataTtl);
}
/**
* Get cached exchange rate
*/
async getCachedExchangeRate(from: string, to: string): Promise<string | null> {
return this.get<string>(`rate:${from}:${to}`);
}
/**
* Set cached exchange rate
*/
async setCachedExchangeRate(from: string, to: string, rate: string): Promise<void> {
await this.set(`rate:${from}:${to}`, rate, this.config.exchangeRateTtl);
}
/**
* Get cached risk calculation
*/
async getCachedRiskCalc(dealId: string, calcType: string): Promise<any | null> {
return this.get(`risk:${dealId}:${calcType}`);
}
/**
* Set cached risk calculation
*/
async setCachedRiskCalc(dealId: string, calcType: string, result: any): Promise<void> {
await this.set(`risk:${dealId}:${calcType}`, result, this.config.riskCalcTtl);
}
/**
* Invalidate deal cache
*/
async invalidateDealCache(dealId: string): Promise<void> {
if (!this.redis || !this.config.enabled) {
logger.debug('Deal cache invalidated (no Redis)', { dealId });
return;
}
const patterns = [`risk:${dealId}:*`, `deal:${dealId}:*`];
try {
const redis = this.redis as RedisClient & { keys?(pattern: string): Promise<string[]> };
for (const pattern of patterns) {
const keys = redis.keys ? await redis.keys(pattern) : [];
if (keys.length > 0) await Promise.all(keys.map((k) => this.redis!.del(k)));
}
logger.debug('Deal cache invalidated', { dealId });
} catch (err) {
logger.error('Cache invalidateDealCache error', { dealId, error: err });
}
}
}
export const cacheService = new CacheService();