Skip to content

Commit e9235b4

Browse files
authored
fix(cache): DistributedCache.update() now writes to Redis on cold L1 … (JhaSourav07#3045)
## Description Fixes JhaSourav07#3025 `DistributedCache.update()` was gating the Redis write behind a local L1 cache check. On cold serverless instances (common on Vercel), the L1 cache is empty, causing the method to return `false` and silently skip the Redis update entirely — leaving stale data cached across all other instances indefinitely. **Root cause:** `localCache.update()` returns `false` when the key isn't in L1. The early `if (!updated) return false` guard was preventing the Redis write entirely. **Fix:** - Removed the L1 gate — L1 update is now best-effort - Added `XX` flag to Redis `SET` command to preserve "only update existing keys" semantics - Fixed return value to reflect actual Redis response instead of hardcoded `true` - Fixed `catch` block to return `false` instead of silently swallowing errors as `true` ## Pillar - [ ] 🎨 Pillar 1 — New Theme Design - [ ] 📐 Pillar 2 — Geometric SVG Improvement - [ ] 🕐 Pillar 3 — Timezone Logic Optimization - [X] 🛠️ Other (Bug fix, refactoring, docs) ## Checklist before requesting a review: - [X] I have read the `CONTRIBUTING.md` file. - [X] I have tested these changes locally (`localhost:3000/api/streak?user=YOUR_USERNAME`). - [X] I have run `npm run format` and `npm run lint` locally and resolved all errors (CI will fail otherwise). - [X] My commits follow the Conventional Commits format (e.g., `feat(themes): ...`, `fix(calculate): ...`). - [ ] I have updated `README.md` if I added a new theme or URL parameter. - [X] I have started the repo. - [X] I have made sure that i have only one commit to merge in this PR. - [ ] The SVG output matches the CommitPulse "premium quality" aesthetic standard (no raw elements, smooth animations, correct fonts). - [X] (Recommended) I joined the CommitPulse Discord community for contributor discussions, mentorship, and faster PR support. Suggested labels: `bug`, `GSSoC 2026`, `intermediate`
2 parents 9170ff7 + 4b46e7e commit e9235b4

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

lib/cache.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,10 @@ export class DistributedCache<T> {
340340
}
341341

342342
async update(key: string, value: T): Promise<boolean> {
343-
const updated = this.localCache.update(key, value);
344-
if (!updated) return false;
343+
this.localCache.update(key, value);
345344

346345
if (!this.useRedis) {
347-
return true;
346+
return this.localCache.has(key);
348347
}
349348

350349
try {
@@ -354,16 +353,17 @@ export class DistributedCache<T> {
354353
Authorization: `Bearer ${this.redisToken}`,
355354
'Content-Type': 'application/json',
356355
},
357-
body: JSON.stringify(['SET', key, JSON.stringify(value), 'KEEPTTL']),
356+
body: JSON.stringify(['SET', key, JSON.stringify(value), 'KEEPTTL', 'XX']),
358357
});
359358

360359
if (!res.ok) {
361360
throw new Error(`Redis HTTP error: ${res.status}`);
362361
}
363-
return true;
362+
const data = await res.json();
363+
return data.result === 'OK';
364364
} catch (err) {
365365
console.error(`[DistributedCache] UPDATE failed for key "${key}":`, err);
366-
return true;
366+
return false;
367367
}
368368
}
369369

0 commit comments

Comments
 (0)