Performance: Redis integration for distributed cache
This affects server response time and reduces backend load by caching data, improving page load speed and interaction responsiveness.
const redis = require('redis'); const client = redis.createClient(); client.connect(); app.get('/data', async (req, res) => { const cacheData = await client.get('items'); if (cacheData) { return res.json(JSON.parse(cacheData)); } const data = await database.query('SELECT * FROM items'); await client.set('items', JSON.stringify(data), { EX: 60 }); res.json(data); });
app.get('/data', async (req, res) => { const data = await database.query('SELECT * FROM items'); res.json(data); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No cache, direct DB query | N/A (server-side) | N/A | N/A | [X] Bad |
| Redis cache with expiration | N/A (server-side) | N/A | N/A | [OK] Good |
app.get('/data', async (req, res) => {
const cacheKey = 'myData';
const cached = await client.get(cacheKey);
if (cached) return res.send(JSON.parse(cached));
const data = { value: 42 };
await client.set(cacheKey, JSON.stringify(data), { EX: 10 });
res.send(data);
});/data twice within 10 seconds?app.get('/count', async (req, res) => {
const count = await client.get('count');
const newCount = count + 1;
await client.set('count', newCount);
res.send({ count: newCount });
});Pass the test and this pattern moves unknown