Documentation
¶
Overview ¶
Package cache provides a generic, type-safe caching layer using Redis.
This package uses generics to provide compile-time type safety for cached values. Cache entries are stored as JSON in Redis with TTL support. Keys can be encoded using custom KeyEncoder implementations for different types (strings, integers, UUIDs).
Basic usage:
type User struct {
ID int
Name string
}
cache := cache.New[string, User](
redisClient,
"users", // hash key
24 * time.Hour, // TTL
cache.NewStringKeyEncoder(),
)
// Set a value
user := User{ID: 123, Name: "Alice"}
if err := cache.Set(ctx, "user:123", &user); err != nil {
return err
}
// Get a value
retrieved, err := cache.Get(ctx, "user:123")
if err != nil {
return err
}
Custom KeyEncoder implementations can be provided for non-standard key types:
cache := cache.New[CustomKeyType, MyValue](
redisClient,
"data",
time.Hour,
MyCustomKeyEncoder{},
)
The cache layer automatically handles JSON marshaling/unmarshaling and provides operations for Set, Get, Delete, and Invalidate (clear all cached values for a hash).
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrKeyNotFound = errors.New("cache: key not found") ErrCacheMarshal = errors.New("cache: failed to marshal value") ErrCacheUnmarshal = errors.New("cache: failed to unmarshal value") ErrCacheGet = errors.New("cache: failed to get") ErrCacheSet = errors.New("cache: failed to set") ErrCacheTTL = errors.New("cache: failed to set TTL") ErrCacheDelete = errors.New("cache: failed to delete") ErrCacheInvalidate = errors.New("cache: failed to invalidate") )
View Source
var ErrCacheInvalidKeyType = errors.New("cache: invalid key type")
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type IntKeyEncoder ¶
type IntKeyEncoder struct{}
func NewIntKeyEncoder ¶
func NewIntKeyEncoder() *IntKeyEncoder
type KeyEncoder ¶
type StringKeyEncoder ¶
type StringKeyEncoder struct{}
func NewStringKeyEncoder ¶
func NewStringKeyEncoder() *StringKeyEncoder
type UUIDKeyEncoder ¶
type UUIDKeyEncoder struct{}
func NewUUIDKeyEncoder ¶
func NewUUIDKeyEncoder() *UUIDKeyEncoder
Click to show internal directories.
Click to hide internal directories.