aura-glass
Version:
A comprehensive glassmorphism design system for React applications with 142+ production-ready components
114 lines (111 loc) • 3.2 kB
JavaScript
import { createClient } from 'redis';
// @ts-nocheck - Optional Redis dependency
class CacheService {
constructor(config) {
this.config = config;
this.client = null;
this.connected = false;
this.memoryCache = new Map();
}
async connect() {
if (this.connected) return;
try {
this.client = createClient({
url: this.config.url
});
this.client.on('error', err => {
console.warn('Redis client error, falling back to memory cache:', err.message);
this.connected = false;
});
await this.client.connect();
this.connected = true;
} catch (error) {
console.warn('Failed to connect to Redis, using memory cache:', error);
this.connected = false;
}
}
async get(key) {
try {
if (this.connected && this.client) {
const value = await this.client.get(key);
return value ? JSON.parse(value) : null;
} else {
const cached = this.memoryCache.get(key);
if (cached && cached.expiry > Date.now()) {
return cached.value;
}
this.memoryCache.delete(key);
return null;
}
} catch (error) {
console.error('Cache get error:', error);
return null;
}
}
async set(key, value, ttl) {
const ttlSeconds = ttl || this.config.ttl;
try {
if (this.connected && this.client) {
await this.client.setEx(key, ttlSeconds, JSON.stringify(value));
} else {
this.memoryCache.set(key, {
value,
expiry: Date.now() + ttlSeconds * 1000
});
this.cleanupMemoryCache();
}
} catch (error) {
console.error('Cache set error:', error);
this.memoryCache.set(key, {
value,
expiry: Date.now() + ttlSeconds * 1000
});
}
}
async delete(key) {
try {
if (this.connected && this.client) {
await this.client.del(key);
} else {
this.memoryCache.delete(key);
}
} catch (error) {
console.error('Cache delete error:', error);
this.memoryCache.delete(key);
}
}
async flush() {
try {
if (this.connected && this.client) {
await this.client.flushAll();
} else {
this.memoryCache.clear();
}
} catch (error) {
console.error('Cache flush error:', error);
this.memoryCache.clear();
}
}
cleanupMemoryCache() {
if (this.memoryCache.size > 1000) {
const now = Date.now();
const entries = Array.from(this.memoryCache.entries());
const expired = entries.filter(([, data]) => data.expiry <= now);
expired.forEach(([key]) => this.memoryCache.delete(key));
if (this.memoryCache.size > 500) {
const sorted = entries.filter(([, data]) => data.expiry > now).sort((a, b) => a[1].expiry - b[1].expiry);
const toRemove = sorted.slice(0, Math.floor(sorted.length / 2));
toRemove.forEach(([key]) => this.memoryCache.delete(key));
}
}
}
async disconnect() {
if (this.client) {
await this.client.quit();
this.connected = false;
this.client = null;
}
}
}
export { CacheService };
//# sourceMappingURL=cache-service.js.map