Spaces:
Paused
Paused
File size: 10,409 Bytes
c904036 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | /**
* Redis Caching Service
* Provides centralized caching functionality with TTL and invalidation
*/
const { createClient } = require('redis');
const { logger } = require('../Middleware/errorHandler');
class CacheService {
constructor() {
this.client = null;
this.isConnected = false;
this.defaultTTL = 300; // 5 minutes
this.keyPrefix = 'campverse:';
}
/**
* Initialize Redis connection
*/
async connect() {
try {
this.client = createClient({
url: process.env.REDIS_URL || 'redis://127.0.0.1:6379',
retry_strategy: (options) => {
if (options.error && options.error.code === 'ECONNREFUSED') {
logger.error('Redis connection refused');
return new Error('Redis connection refused');
}
if (options.total_retry_time > 1000 * 60 * 60) {
logger.error('Redis retry time exhausted');
return new Error('Retry time exhausted');
}
if (options.attempt > 10) {
logger.error('Redis max retry attempts reached');
return undefined;
}
return Math.min(options.attempt * 100, 3000);
}
});
this.client.on('error', (err) => {
logger.error('Redis Client Error:', err);
this.isConnected = false;
});
this.client.on('connect', () => {
logger.info('Redis connected');
this.isConnected = true;
});
this.client.on('disconnect', () => {
logger.warn('Redis disconnected');
this.isConnected = false;
});
await this.client.connect();
} catch (error) {
logger.error('Failed to connect to Redis:', error);
this.isConnected = false;
}
}
/**
* Generate cache key with prefix
*/
_generateKey(key) {
return `${this.keyPrefix}${key}`;
}
/**
* Get value from cache
*/
async get(key) {
if (!this.isConnected) {
logger.warn(`Cache not connected, returning null for key: ${key}`);
return null;
}
try {
const cacheKey = this._generateKey(key);
const value = await this.client.get(cacheKey);
if (value) {
logger.debug(`Cache hit for key: ${key}`);
return JSON.parse(value);
}
logger.debug(`Cache miss for key: ${key}`);
return null;
} catch (error) {
logger.error(`Cache get error for key ${key}:`, error);
// Don't throw, just return null to allow fallback
return null;
}
}
/**
* Set value in cache with TTL
*/
async set(key, value, ttl = this.defaultTTL) {
if (!this.isConnected) {
logger.warn(`Cache not connected, cannot set key: ${key}`);
return false;
}
try {
const cacheKey = this._generateKey(key);
const serializedValue = JSON.stringify(value);
await this.client.setEx(cacheKey, ttl, serializedValue);
logger.debug(`Cache set for key: ${key}, TTL: ${ttl}s`);
return true;
} catch (error) {
logger.error(`Cache set error for key ${key}:`, error);
return false;
}
}
/**
* Delete value from cache
*/
async del(key) {
if (!this.isConnected) return false;
try {
const cacheKey = this._generateKey(key);
const result = await this.client.del(cacheKey);
logger.debug(`Cache delete for key: ${key}`);
return result > 0;
} catch (error) {
logger.error(`Cache delete error for key ${key}:`, error);
return false;
}
}
/**
* Delete multiple keys by pattern
*/
async delPattern(pattern) {
if (!this.isConnected) return false;
try {
const searchPattern = this._generateKey(pattern);
const keys = await this.client.keys(searchPattern);
if (keys.length > 0) {
await this.client.del(keys);
logger.debug(`Cache pattern delete: ${pattern}, ${keys.length} keys deleted`);
}
return true;
} catch (error) {
logger.error(`Cache pattern delete error for pattern ${pattern}:`, error);
return false;
}
}
/**
* Check if key exists
*/
async exists(key) {
if (!this.isConnected) return false;
try {
const cacheKey = this._generateKey(key);
const result = await this.client.exists(cacheKey);
return result === 1;
} catch (error) {
logger.error(`Cache exists error for key ${key}:`, error);
return false;
}
}
/**
* Increment counter
*/
async incr(key, ttl = this.defaultTTL) {
if (!this.isConnected) return null;
try {
const cacheKey = this._generateKey(key);
const value = await this.client.incr(cacheKey);
if (value === 1) {
// Set TTL only for new keys
await this.client.expire(cacheKey, ttl);
}
return value;
} catch (error) {
logger.error(`Cache incr error for key ${key}:`, error);
return null;
}
}
/**
* Get or set pattern - fetch from cache or execute function and cache result
*/
async getOrSet(key, fetchFunction, ttl = this.defaultTTL) {
// Try to get from cache first
const cachedValue = await this.get(key);
if (cachedValue !== null) {
return cachedValue;
}
try {
// Execute function to get fresh data
const freshValue = await fetchFunction();
// Cache the result
await this.set(key, freshValue, ttl);
return freshValue;
} catch (error) {
logger.error(`Cache getOrSet error for key ${key}:`, error);
throw error;
}
}
/**
* Cache invalidation patterns
*/
async invalidateUser(userId) {
await this.delPattern(`user:${userId}:*`);
await this.delPattern(`dashboard:user:${userId}`);
}
async invalidateInstitution(institutionId) {
await this.delPattern(`institution:${institutionId}:*`);
await this.delPattern(`dashboard:institution:${institutionId}`);
await this.delPattern('institutions:*');
}
async invalidateEvent(eventId) {
await this.delPattern(`event:${eventId}:*`);
await this.delPattern('events:*');
await this.delPattern('recommendations:*');
}
async invalidateGlobal() {
await this.delPattern('events:*');
await this.delPattern('institutions:*');
await this.delPattern('analytics:*');
}
/**
* Invalidate verifier-related cache (pending verifications, etc.)
*/
async invalidateVerifierCache() {
await this.delPattern('verifier:*');
await this.delPattern('pending:*');
await this.delPattern('verification:*');
await this.delPattern('institutions:pending:*');
await this.delPattern('certificates:pending:*');
}
/**
* Invalidate admin-related cache
*/
async invalidateAdminCache() {
await this.delPattern('admin:*');
await this.delPattern('platform:*');
await this.delPattern('users:*');
await this.delPattern('institutions:*');
await this.delPattern('analytics:*');
}
/**
* Invalidate certificate verification cache
*/
async invalidateCertificateVerification(eventId) {
await this.delPattern(`event:${eventId}:*`);
await this.delPattern('certificates:pending:*');
await this.delPattern('verification:certificates:*');
await this.delPattern('verifier:*');
}
/**
* Invalidate institution verification cache
*/
async invalidateInstitutionVerification(institutionId) {
await this.delPattern(`institution:${institutionId}:*`);
await this.delPattern('institutions:pending:*');
await this.delPattern('verification:institutions:*');
await this.delPattern('verifier:*');
await this.delPattern('institutions:*');
}
/**
* Health check
*/
async healthCheck() {
if (!this.isConnected) return false;
try {
await this.client.ping();
return true;
} catch (error) {
logger.error('Redis health check failed:', error);
return false;
}
}
/**
* Get cache statistics
*/
async getStats() {
if (!this.isConnected) return null;
try {
const info = await this.client.info('memory');
const keyspace = await this.client.info('keyspace');
return {
connected: this.isConnected,
memory: info,
keyspace,
timestamp: new Date().toISOString()
};
} catch (error) {
logger.error('Failed to get cache stats:', error);
return null;
}
}
/**
* Flush all cache (use with caution)
*/
async flushAll() {
if (!this.isConnected) return false;
try {
await this.client.flushAll();
logger.warn('Cache flushed - all keys deleted');
return true;
} catch (error) {
logger.error('Cache flush error:', error);
return false;
}
}
/**
* Close connection
*/
async disconnect() {
if (this.client) {
await this.client.disconnect();
this.isConnected = false;
logger.info('Redis disconnected');
}
}
}
// Cache key generators for consistency
const CacheKeys = {
user: (userId) => `user:${userId}`,
userDashboard: (userId) => `dashboard:user:${userId}`,
userEvents: (userId) => `user:${userId}:events`,
userCertificates: (userId) => `user:${userId}:certificates`,
institution: (institutionId) => `institution:${institutionId}`,
institutionDashboard: (institutionId) => `dashboard:institution:${institutionId}`,
institutionEvents: (institutionId) => `institution:${institutionId}:events`,
institutionUsers: (institutionId) => `institution:${institutionId}:users`,
event: (eventId) => `event:${eventId}`,
eventParticipants: (eventId) => `event:${eventId}:participants`,
eventAnalytics: (eventId) => `event:${eventId}:analytics`,
events: (filters = '') => `events:${filters}`,
institutions: (filters = '') => `institutions:${filters}`,
recommendations: (userId) => `recommendations:${userId}`,
analytics: (type, id = '') => `analytics:${type}:${id}`,
rateLimit: (ip, endpoint) => `ratelimit:${ip}:${endpoint}`,
session: (sessionId) => `session:${sessionId}`,
};
// TTL constants (in seconds)
const CacheTTL = {
SHORT: 60, // 1 minute
MEDIUM: 300, // 5 minutes
LONG: 1800, // 30 minutes
VERY_LONG: 3600, // 1 hour
DAY: 86400, // 24 hours
};
// Create singleton instance
const cacheService = new CacheService();
module.exports = {
cacheService,
CacheKeys,
CacheTTL
}; |