Skip to content

[Backend] Caching: add read-through Redis cache + invalidation for trust score endpoint #1000

Description

@Baskarayelu

Description

GET /api/trust/:address recomputes the full trust score on every request from the data store with no caching, while bond and attestation reads already use bondCacheService/attestationCacheService. Trust scores change only when a bond, attestation, or slash event is applied, so they are ideal for read-through caching with explicit invalidation. We should add a trust cache namespace in src/cache/redis.ts and invalidate it from the identity-state-sync path.

Requirements and context

  • Add a trust namespace cache wrapper analogous to src/services/bondCacheService.ts.
  • Invalidate via src/cache/invalidation.ts when src/listeners/identityStateSync.ts applies bond/attestation/slash changes.
  • Record cache hit/miss and stale-read metrics using existing src/middleware/metrics.ts helpers (recordStaleCacheRead).
  • TTL must be configurable through src/config/index.ts.

Suggested execution

Create branch feature/trust-score-cache.

  • Add src/services/trustCacheService.ts and tests.
  • Hook invalidation in src/listeners/identityStateSync.ts.
  • Add metrics + config entry.
  • Update docs/caching.md and docs/CACHE_INVALIDATION.md.

Test and commit

Run npm run test. Edge cases: cache miss → set, invalidation on state change, stale-read detection, Redis unavailable (fail open to DB). Security: never cache across tenants incorrectly.

Example commit message

feat: add read-through Redis cache and invalidation for trust scores

Guidelines

  • Minimum 95% test coverage
  • Clear documentation in docs/caching.md
  • Timeframe: 96 hours

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions