Draft
Conversation
…s cache (L2) Adds a multiLevelCache implementation that layers an in-memory collection.Cache on top of the existing Redis-backed cache.Cache to reduce Redis round-trips for hot keys. New files: - core/stores/cache/multilevel.go: Cache interface implementation with L1 in-memory + L2 remote two-tier caching - core/stores/cache/multilevel_test.go: comprehensive tests Updated files: - core/stores/sqlc/cachedsql.go: convenience constructors NewConnWithMultiLevelCache and NewNodeConnWithMultiLevelCache Closes zeromicro#4797
…i-level cache - 4 new integration tests in sqlc covering constructor, query, exec invalidation, and not-found caching - doc/multilevel-cache.md with architecture diagram, quick start examples, and configuration guide
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.
Multi-Level Cache (sqlc + collection.Cache)
Closes #4797
Problem
Using Redis (
sqlc) alone for SQL caching incurs a network round-trip on every read. For hot keys this overhead adds up.collection.Cacheis an in-process LRU cache with zero network latency, but on its own it does not survive process restarts and cannot be shared across instances.Solution
cache.NewMultiLevelCachelayers the two together:Changes
New files
core/stores/cache/multilevel.go—cache.Cacheimplementation with two-tier L1 (in-memory) + L2 (Redis) cachingcore/stores/cache/multilevel_test.go— 18 unit tests covering all cache operations (Get, Set, Del, Take, TakeWithExpire, not-found caching, struct values, L2→L1 promotion)Modified files
core/stores/sqlc/cachedsql.go— Added two convenience constructors:NewConnWithMultiLevelCache— Redis cluster + in-memory L1NewNodeConnWithMultiLevelCache— single Redis node + in-memory L1core/stores/sqlc/cachedsql_test.go— 4 integration tests covering constructor usage, L1 hit verification, exec cache invalidation, and not-found placeholder cachingUsage
Option A — Using sqlc convenience constructors
Option B — Using the cache layer directly
Using it for queries
Once constructed, use
connexactly like a regularsqlc.CachedConn:The first call hits Redis → DB, subsequent calls hit the in-memory L1 directly.
Configuration Options
Multi-level cache options (
cache.MultiLevelCacheOption):cache.WithLocalExpire(d time.Duration)— L1 in-memory TTL (default: 5 minutes)cache.WithLocalLimit(n int)— L1 max entry count with LRU eviction (default: 10000)Standard cache options (
cache.Option) passed through to L2 (Redis):cache.WithExpiry(d time.Duration)— L2 TTL (default: 7 days)cache.WithNotFoundExpiry(d time.Duration)— TTL for not-found placeholders (default: 1 minute)How to Test
All 22 new tests pass. All existing tests unaffected:
Consistency Notes
WithLocalExpireto a short duration (e.g. 5–30 seconds) in multi-instance deployments.Del/Execinvalidate both L1 and L2 within the same process immediately.