As33
@periodic/
arsenic
cache_candidate
ℹ️ Info

Query pattern would benefit from caching

This query executes the same data access pattern repeatedly. Adding a cache layer would reduce database load.

Acting on this signal

typescript
// Arsenic detects the same query firing repeatedly without a cache hit
const monitor = createMonitor({
  emitPositiveSignals: true,
  exporter: (event) => {
    if (event.signals.includes('cache_candidate')) {
      // Log which queries to prioritise for caching
      console.log('Cache candidate:', {
        model: event.model,
        operation: event.operation,
        filter: event.filter,
      });
    }
  },
});

// Then implement cache-aside for the identified query
async function getConfig(key: string) {
  const cached = await redis.get(`config:${key}`);
  if (cached) return JSON.parse(cached);
  const config = await Config.findOne({ key }).lean();
  await redis.setex(`config:${key}`, 600, JSON.stringify(config));
  return config;
}