|
| 1 | +let redisClientPromise = null; |
| 2 | +let redisClientUrl = ""; |
| 3 | +let redisWarningShown = false; |
| 4 | + |
| 5 | +const cacheStatus = { |
| 6 | + provider: "memory", |
| 7 | + configured: false, |
| 8 | + connected: false, |
| 9 | + lastError: null, |
| 10 | +}; |
| 11 | + |
| 12 | +function readRedisUrl() { |
| 13 | + return process.env.REDIS_URL?.trim() || ""; |
| 14 | +} |
| 15 | + |
| 16 | +function syncCacheStatus() { |
| 17 | + const redisUrl = readRedisUrl(); |
| 18 | + cacheStatus.configured = Boolean(redisUrl); |
| 19 | + cacheStatus.provider = redisUrl ? "redis" : "memory"; |
| 20 | + |
| 21 | + if (!redisUrl) { |
| 22 | + cacheStatus.connected = false; |
| 23 | + cacheStatus.lastError = null; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +function warnRedisFallback(message, error) { |
| 28 | + cacheStatus.connected = false; |
| 29 | + cacheStatus.lastError = error instanceof Error ? error.message : null; |
| 30 | + |
| 31 | + if (redisWarningShown) { |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + redisWarningShown = true; |
| 36 | + |
| 37 | + const errorMessage = error instanceof Error ? error.message : ""; |
| 38 | + // eslint-disable-next-line no-console |
| 39 | + console.warn(`${message}${errorMessage ? ` (${errorMessage})` : ""}`); |
| 40 | +} |
| 41 | + |
| 42 | +export async function getRedisClient() { |
| 43 | + const redisUrl = readRedisUrl(); |
| 44 | + syncCacheStatus(); |
| 45 | + |
| 46 | + if (!redisUrl) { |
| 47 | + return null; |
| 48 | + } |
| 49 | + |
| 50 | + if (redisClientUrl !== redisUrl) { |
| 51 | + redisClientPromise = null; |
| 52 | + redisClientUrl = redisUrl; |
| 53 | + } |
| 54 | + |
| 55 | + if (!redisClientPromise) { |
| 56 | + redisClientPromise = import("redis") |
| 57 | + .then(async ({ createClient }) => { |
| 58 | + const client = createClient({ |
| 59 | + url: redisUrl, |
| 60 | + socket: { |
| 61 | + connectTimeout: 3_000, |
| 62 | + reconnectStrategy(retries) { |
| 63 | + if (retries >= 2) { |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + return Math.min((retries + 1) * 100, 500); |
| 68 | + }, |
| 69 | + }, |
| 70 | + }); |
| 71 | + |
| 72 | + client.on("ready", () => { |
| 73 | + cacheStatus.connected = true; |
| 74 | + cacheStatus.lastError = null; |
| 75 | + }); |
| 76 | + |
| 77 | + client.on("end", () => { |
| 78 | + cacheStatus.connected = false; |
| 79 | + }); |
| 80 | + |
| 81 | + client.on("error", (error) => { |
| 82 | + warnRedisFallback("Redis indisponivel, usando cache em memoria.", error); |
| 83 | + }); |
| 84 | + |
| 85 | + await client.connect(); |
| 86 | + return client; |
| 87 | + }) |
| 88 | + .catch((error) => { |
| 89 | + warnRedisFallback("Falha ao conectar no Redis, usando cache em memoria.", error); |
| 90 | + redisClientPromise = null; |
| 91 | + return null; |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + return redisClientPromise; |
| 96 | +} |
| 97 | + |
| 98 | +export function getCacheStatus() { |
| 99 | + syncCacheStatus(); |
| 100 | + return { |
| 101 | + ...cacheStatus, |
| 102 | + type: cache.constructor.name, |
| 103 | + }; |
| 104 | +} |
| 105 | + |
| 106 | +export async function warmupCache() { |
| 107 | + syncCacheStatus(); |
| 108 | + |
| 109 | + if (!cacheStatus.configured) { |
| 110 | + return getCacheStatus(); |
| 111 | + } |
| 112 | + |
| 113 | + const client = await getRedisClient(); |
| 114 | + if (!client) { |
| 115 | + return getCacheStatus(); |
| 116 | + } |
| 117 | + |
| 118 | + try { |
| 119 | + const pong = await client.ping(); |
| 120 | + cacheStatus.connected = pong === "PONG"; |
| 121 | + cacheStatus.lastError = cacheStatus.connected ? null : "PING sem resposta esperada"; |
| 122 | + } catch (error) { |
| 123 | + warnRedisFallback("Falha ao validar conexao com Redis.", error); |
| 124 | + } |
| 125 | + |
| 126 | + return getCacheStatus(); |
| 127 | +} |
| 128 | + |
1 | 129 | export class MemoryCache { |
2 | 130 | constructor() { |
3 | 131 | this.store = new Map(); |
@@ -38,4 +166,89 @@ export class MemoryCache { |
38 | 166 | } |
39 | 167 | } |
40 | 168 |
|
41 | | -export const cache = new MemoryCache(); |
| 169 | +export class RedisCache { |
| 170 | + constructor(options = {}) { |
| 171 | + this.prefix = options.prefix || process.env.REDIS_KEY_PREFIX?.trim() || "vagas-full"; |
| 172 | + this.memoryFallback = new MemoryCache(); |
| 173 | + } |
| 174 | + |
| 175 | + buildKey(key) { |
| 176 | + return `${this.prefix}:${key}`; |
| 177 | + } |
| 178 | + |
| 179 | + async get(key) { |
| 180 | + const client = await getRedisClient(); |
| 181 | + |
| 182 | + if (!client) { |
| 183 | + return this.memoryFallback.get(key); |
| 184 | + } |
| 185 | + |
| 186 | + try { |
| 187 | + const raw = await client.get(this.buildKey(key)); |
| 188 | + return raw ? JSON.parse(raw) : null; |
| 189 | + } catch (error) { |
| 190 | + warnRedisFallback("Erro ao ler do Redis, usando cache em memoria.", error); |
| 191 | + return this.memoryFallback.get(key); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + async set(key, value, ttlMs) { |
| 196 | + this.memoryFallback.set(key, value, ttlMs); |
| 197 | + |
| 198 | + const client = await getRedisClient(); |
| 199 | + if (!client) { |
| 200 | + return; |
| 201 | + } |
| 202 | + |
| 203 | + try { |
| 204 | + await client.set(this.buildKey(key), JSON.stringify(value), { |
| 205 | + PX: Math.max(1, Number(ttlMs) || 1), |
| 206 | + }); |
| 207 | + } catch (error) { |
| 208 | + warnRedisFallback("Erro ao salvar no Redis, usando cache em memoria.", error); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + async delete(key) { |
| 213 | + this.memoryFallback.delete(key); |
| 214 | + |
| 215 | + const client = await getRedisClient(); |
| 216 | + if (!client) { |
| 217 | + return; |
| 218 | + } |
| 219 | + |
| 220 | + try { |
| 221 | + await client.del(this.buildKey(key)); |
| 222 | + } catch (error) { |
| 223 | + warnRedisFallback("Erro ao remover chave do Redis.", error); |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + async clear() { |
| 228 | + this.memoryFallback.clear(); |
| 229 | + |
| 230 | + const client = await getRedisClient(); |
| 231 | + if (!client) { |
| 232 | + return; |
| 233 | + } |
| 234 | + |
| 235 | + try { |
| 236 | + const keys = []; |
| 237 | + for await (const key of client.scanIterator({ MATCH: `${this.prefix}:*` })) { |
| 238 | + keys.push(key); |
| 239 | + } |
| 240 | + |
| 241 | + if (keys.length > 0) { |
| 242 | + await client.del(keys); |
| 243 | + } |
| 244 | + } catch (error) { |
| 245 | + warnRedisFallback("Erro ao limpar o Redis.", error); |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + async has(key) { |
| 250 | + return (await this.get(key)) !== null; |
| 251 | + } |
| 252 | +} |
| 253 | + |
| 254 | +export const cache = process.env.REDIS_URL?.trim() ? new RedisCache() : new MemoryCache(); |
0 commit comments