Skip to main content

Caching Strategy

To reduce database load and improve response times, we implement caching at multiple layers.

1. Application Cache (Redis)

We use a wrapper service CacheService to handle GET/SET operations with standardized keys.

Usage

We prefer the Get-Or-Set pattern:

const stats = await CacheService.remember(
`stats:camera:${id}`, // Key
60, // TTL (Seconds)
async () => await StatsService.compute(id), // Computation (only runs if miss)
);

2. Invalidation

When data changes (e.g., User updates profile), we must clear the stale cache. We use CacheInvalidationService which supports Wildcard Deletion (Scanning Redis keys).

// When a camera is updated
await CacheInvalidationService.clear(`stats:camera:${camera.id}:*`);

3. HTTP Caching

For static assets or highly cacheable API responses, we attach ETag or Cache-Control headers via the metrics middleware.