In memory cache layer#114
Open
gloskull wants to merge 3 commits into
Open
Conversation
…edis Add configurable Redis-backed cache layer
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.
Motivation
Provide a two-tier caching solution to keep read-heavy contract paths (meter status and usage rollups) fast and to meet the <100ms P99 target for warm reads.
Allow optional shared coherence via Redis for multi-process deployments while keeping a safe in-process hot path for local development and tests.
Expose TTLs and runtime metrics so the service can be monitored and operated safely during blue/green and canary rollouts.
Description
Adds a new cache implementation meter-simulator/src/cache-layer.js implementing an in-process memory hot cache plus an optional minimal Redis RESP client and runtime metrics (cache_hits, cache_misses, cache_sets, cache_deletes, cache_errors).
Wires the cache into ContractInterface so getMeter and getUsageData use cache.remember(...) and so submitUsageData/submitZkUsageData call _invalidateMeterCache(...) to evict meter and usage entries after writes.
Adds cache configuration to meter-simulator/src/config.js, a cache-info CLI command and cache metrics output in the status command in meter-simulator/src/index.js, and documents architecture/operational guidance in docs/REDIS_CACHE_ARCHITECTURE.md and meter-simulator/README.md.
Adds focused Jest tests meter-simulator/tests/cache-layer.test.js and meter-simulator/tests/contract-cache.test.js that cover memory hits, TTL expiry, deletion, caching behavior in ContractInterface, and cache invalidation after usage submission.
Testing
Ran focused unit tests with npm test --prefix meter-simulator -- tests/cache-layer.test.js tests/contract-cache.test.js --runInBand, which passed (both new test suites green).
Performed static checks with node --check meter-simulator/src/cache-layer.js, node --check meter-simulator/src/contract-interface.js, and node --check meter-simulator/src/index.js, which reported no syntax errors.
Ran the full simulator test run npm test --prefix meter-simulator -- --runInBand; the new cache tests pass but the pre-existing tests/meter-device.test.js failed due to unrelated expectations in that suite (invalid mock key material and time/message-length assertions), so full test suite remains failing for reasons unrelated to the cache changes.
Closes #70