Skip to content

In memory cache layer#114

Open
gloskull wants to merge 3 commits into
Utility-Protocol:mainfrom
gloskull:In-memory-Cache-Layer
Open

In memory cache layer#114
gloskull wants to merge 3 commits into
Utility-Protocol:mainfrom
gloskull:In-memory-Cache-Layer

Conversation

@gloskull

Copy link
Copy Markdown
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

In-memory Cache Layer with Redis and Configurable TTL

1 participant