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
Description
GET /api/trust/:addressrecomputes the full trust score on every request from the data store with no caching, while bond and attestation reads already usebondCacheService/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 insrc/cache/redis.tsand invalidate it from the identity-state-sync path.Requirements and context
trustnamespace cache wrapper analogous tosrc/services/bondCacheService.ts.src/cache/invalidation.tswhensrc/listeners/identityStateSync.tsapplies bond/attestation/slash changes.src/middleware/metrics.tshelpers (recordStaleCacheRead).src/config/index.ts.Suggested execution
Create branch
feature/trust-score-cache.src/services/trustCacheService.tsand tests.src/listeners/identityStateSync.ts.docs/caching.mdanddocs/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 scoresGuidelines
docs/caching.md