feat(#12): add Redis caching layer with graceful fallback - #47
Open
leanworld7-netizen wants to merge 2 commits into
Open
feat(#12): add Redis caching layer with graceful fallback#47leanworld7-netizen wants to merge 2 commits into
leanworld7-netizen wants to merge 2 commits into
Conversation
…n/analytics routes - Create src/schemas/metering.schema.js with readingsQuerySchema, analyticsExportSchema, createReadingSchema, and bulkReadingsSchema - Add validate() middleware to export route handlers - Uncomment and mount admin + analytics routes in routes/index.js (schemas already existed but routes were never activated) - Strict mode on body schemas to strip unexpected fields (anti-mass-assignment) Closes EquipChain#8
- Create CacheService (src/services/cache.js) with ioredis:
- get/set/del/flush/invalidate/warm methods
- TTL presets: meter=60s, config=300s, analytics=120s
- Exponential backoff retry (max 10, capped at 2s)
- Graceful fallback when Redis is down (no-cache mode)
- Health monitoring with hit/miss/error stats
- Key naming: equipchain:{entity}:{id}:{field}
- Create caching middleware (src/middleware/cache.js):
- Auto-cache GET responses with X-Cache headers
- TTL auto-detection from path (/config=300s, /analytics=120s, /meter=60s)
- Varies cache key by auth status
- invalidateCache() helper for write-through invalidation
- Integrate cache health into /health endpoint
- Add ioredis ^5.4.0 to dependencies
- Add 15 unit tests covering all operations + fallback behavior
Closes EquipChain#12
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #12 — implements Redis caching layer for blockchain data queries with graceful fallback.
Changes
src/services/cache.js(new, ~200 lines):CacheServiceclass wrapping ioredis with graceful degradationget(key)— returns parsed JSON or null on miss/errorset(key, value, ttl)— stores with TTL viasetexdel(key)— deletes single keyflush(pattern)— batch-deletes keys matching glob pattern (100-key batches)invalidate(entity, id)— invalidates all cache entries for an entitywarm(entries)— pre-populates cache from fetcher functionshealth()— returns connection status + hit/miss/error stats + hit rateequipchain:{entity}:{id}:{field}src/middleware/cache.js(new, ~80 lines):cacheMiddleware(ttl)— Express middleware for automatic GET cachingX-Cache: HIT|MISSandX-Cache-TTLresponse headers/config=300s,/analytics=120s,/meter=60s)invalidateCache(entity, id)— call after write operationssrc/routes/index.js(modified):/healthendpointtests/unit/cache.test.js(new, 15 tests):package.json:ioredis ^5.4.0Testing