Skip to content

Comments

refactor: refactor cache system to support generics and arbitrary value types#54

Open
appleboy wants to merge 3 commits intomainfrom
aside
Open

refactor: refactor cache system to support generics and arbitrary value types#54
appleboy wants to merge 3 commits intomainfrom
aside

Conversation

@appleboy
Copy link
Member

  • 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>
Copilot AI review requested due to automatic review settings February 24, 2026 14:24
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 T to 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 GetWithFetch into an optional CacheWithFetch[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.

appleboy and others added 2 commits February 24, 2026 22:31
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>
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.

1 participant