Conversation
Member
appleboy
commented
Feb 24, 2026
- Refactor the cache system to use generics, enabling support for any value type instead of just int64
- Replace all cache interfaces and implementations with generic versions, updating construction and method signatures accordingly
- Change Redis cache serialization from string conversion to JSON encoding/decoding for compatibility with arbitrary types
- Update cache-aside logic to use type assertion for optimized stampede protection when available, with generic fallback
- Revise all related tests and helper functions to utilize the generic cache pattern
…ue types - Refactor the cache system to use generics, enabling support for any value type instead of just int64 - Replace all cache interfaces and implementations with generic versions, updating construction and method signatures accordingly - Change Redis cache serialization from string conversion to JSON encoding/decoding for compatibility with arbitrary types - Update cache-aside logic to use type assertion for optimized stampede protection when available, with generic fallback - Revise all related tests and helper functions to utilize the generic cache pattern Signed-off-by: appleboy <appleboy.tw@gmail.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request refactors the cache system to support generics, enabling storage of arbitrary value types instead of being limited to int64. The refactor replaces string-based serialization with JSON encoding for Redis implementations and separates the cache-aside pattern into an optional interface with a generic fallback helper.
Changes:
- Introduced generic type parameter
Tto all cache interfaces and implementations (Cache[T any],MemoryCache[T any],RueidisCache[T any],RueidisAsideCache[T any]) - Replaced string conversion (
strconv) with JSON marshaling for Redis cache serialization/deserialization - Split
GetWithFetchinto an optionalCacheWithFetch[T]interface for optimized implementations and a standalone generic helper function for basic cache-aside support
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| internal/cache/interface.go | Converted Cache interface to generic Cache[T any], added CacheWithFetch[T any] interface for optimized cache-aside, and introduced GetWithFetch helper function |
| internal/cache/memory.go | Updated MemoryCache to generic MemoryCache[T any], removed GetWithFetch method (now uses generic helper) |
| internal/cache/rueidis.go | Converted RueidisCache to generic type, replaced strconv with json.Marshal/Unmarshal, removed GetWithFetch method |
| internal/cache/rueidis_aside.go | Converted RueidisAsideCache to generic type, updated GetWithFetch to implement CacheWithFetch interface, replaced strconv with json encoding |
| internal/cache/cache_test.go | Updated all test constructors to use type parameters (e.g., NewMemoryCacheint64), renamed GetWithFetch tests to reflect standalone function |
| internal/metrics/cache.go | Updated CacheWrapper to use Cache[int64], added type assertion logic to prefer CacheWithFetch when available |
| internal/metrics/cache_test.go | Updated all test cache constructors to use type parameter [int64] |
| internal/bootstrap/cache.go | Updated cache initialization to specify type parameter [int64] for all cache constructors |
| internal/bootstrap/bootstrap.go | Updated MetricsCache field type to Cache[int64] |
| internal/bootstrap/server.go | Updated function signatures to accept Cache[int64] type |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
cache.CacheWithFetch stutters; cache.WithFetch reads naturally. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Encode the value using json.Marshal before storing, and handle possible encoding errors. Signed-off-by: appleboy <appleboy.tw@gmail.com>
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.