Documentation
¶
Index ¶
- Variables
- func NewRedisCmdable(cfg Redis) (redis.Cmdable, error)
- func ProvideRedisCmdable(conf Redis) (redis.Cmdable, error)
- type CachedHashQuery
- type CachedHashQueryOption
- type CachedQuery
- type CachedQueryOption
- type HashMarshalFunc
- type HashUnmarshalFunc
- type ICache
- type KeyFunc
- type QueryFunc
- type Redis
- type RedisCache
- func (r *RedisCache) Del(ctx context.Context, keys ...string) *redis.IntCmd
- func (r *RedisCache) Expire(ctx context.Context, key string, expiration time.Duration) *redis.BoolCmd
- func (r *RedisCache) Get(ctx context.Context, key string) *redis.StringCmd
- func (r *RedisCache) GetCmdable() redis.Cmdable
- func (r *RedisCache) HDel(ctx context.Context, key string, fields ...string) *redis.IntCmd
- func (r *RedisCache) HGetAll(ctx context.Context, key string) *redis.MapStringStringCmd
- func (r *RedisCache) HSet(ctx context.Context, key string, values ...any) *redis.IntCmd
- func (r *RedisCache) Pipeline() redis.Pipeliner
- func (r *RedisCache) Set(ctx context.Context, key string, value any, expiration time.Duration) *redis.StatusCmd
Constants ¶
This section is empty.
Variables ¶
var ( // ErrCacheMiss indicates that the key was not found in cache ErrCacheMiss = redis.Nil )
var ProviderSet = wire.NewSet(ProvideRedisCmdable, ProvideICache)
ProviderSet 提供缓存相关的依赖(支持单节点、Sentinel 和集群模式)
Functions ¶
func NewRedisCmdable ¶
NewRedisCmdable 创建 Redis 客户端(支持单节点、Sentinel 和集群模式)
Types ¶
type CachedHashQuery ¶
type CachedHashQuery[T any] struct { // contains filtered or unexported fields }
CachedHashQuery provides a generic cache-aside pattern implementation using Redis Hash It queries Redis Hash first, and falls back to database if cache miss
func NewCachedHashQuery ¶
func NewCachedHashQuery[T any]( cache ICache, keyFunc KeyFunc, queryFunc QueryFunc[T], hashMarshal HashMarshalFunc[T], hashUnmarshal HashUnmarshalFunc[T], opts ...CachedHashQueryOption[T], ) *CachedHashQuery[T]
NewCachedHashQuery creates a new CachedHashQuery instance cache: Redis cache instance keyFunc: function to generate cache key from parameters queryFunc: function to query data from database hashMarshal: function to convert value to hash fields hashUnmarshal: function to convert hash fields to value opts: optional configurations
func (*CachedHashQuery[T]) Get ¶
func (cq *CachedHashQuery[T]) Get(ctx context.Context, params ...any) (T, error)
Get queries data with cache-aside pattern using Redis Hash It first checks Redis Hash cache, if miss, queries from database and caches the result as Hash params: parameters used to generate cache key
func (*CachedHashQuery[T]) Invalidate ¶
func (cq *CachedHashQuery[T]) Invalidate(ctx context.Context, params ...any) error
Invalidate removes the cached Hash data
type CachedHashQueryOption ¶
type CachedHashQueryOption[T any] func(*CachedHashQuery[T])
CachedHashQueryOption configures CachedHashQuery behavior
func WithHashLogPrefix ¶
func WithHashLogPrefix[T any](prefix string) CachedHashQueryOption[T]
WithHashLogPrefix sets the log prefix for debugging
func WithHashTTL ¶
func WithHashTTL[T any](ttl time.Duration) CachedHashQueryOption[T]
WithHashTTL sets the cache expiration time for hash
type CachedQuery ¶
type CachedQuery[T any] struct { // contains filtered or unexported fields }
CachedQuery provides a generic cache-aside pattern implementation It queries Redis first, and falls back to database if cache miss
func NewCachedQuery ¶
func NewCachedQuery[T any]( cache ICache, keyFunc KeyFunc, queryFunc QueryFunc[T], opts ...CachedQueryOption[T], ) *CachedQuery[T]
NewCachedQuery creates a new CachedQuery instance cache: Redis cache instance keyFunc: function to generate cache key from parameters queryFunc: function to query data from database opts: optional configurations
func (*CachedQuery[T]) Get ¶
func (cq *CachedQuery[T]) Get(ctx context.Context, params ...any) (T, error)
Get queries data with cache-aside pattern It first checks Redis cache, if miss, queries from database and caches the result params: parameters used to generate cache key
func (*CachedQuery[T]) GetOrSet ¶
func (cq *CachedQuery[T]) GetOrSet(ctx context.Context, setFunc func(ctx context.Context) (T, error), params ...any) (T, error)
GetOrSet queries data with cache-aside pattern, but allows setting a custom value if cache miss This is useful when you want to set a default value or handle cache miss differently
func (*CachedQuery[T]) Invalidate ¶
func (cq *CachedQuery[T]) Invalidate(ctx context.Context, params ...any) error
Invalidate removes the cached data
type CachedQueryOption ¶
type CachedQueryOption[T any] func(*CachedQuery[T])
CachedQueryOption configures CachedQuery behavior
func WithLogPrefix ¶
func WithLogPrefix[T any](prefix string) CachedQueryOption[T]
WithLogPrefix sets the log prefix for debugging Note: This function doesn't need type parameter but we keep it for consistency Use WithLogPrefix[YourType] or let type inference work from NewCachedQuery context
type HashMarshalFunc ¶
HashMarshalFunc defines a function that converts a value to hash fields
type HashUnmarshalFunc ¶
HashUnmarshalFunc defines a function that converts hash fields to a value
type ICache ¶
type ICache interface {
// Get 获取缓存值
Get(ctx context.Context, key string) *redis.StringCmd
// Set 设置缓存值
Set(ctx context.Context, key string, value any, expiration time.Duration) *redis.StatusCmd
// Del 删除缓存
Del(ctx context.Context, keys ...string) *redis.IntCmd
// Pipeline 创建管道
Pipeline() redis.Pipeliner
// HSet 设置 Hash 字段
HSet(ctx context.Context, key string, values ...any) *redis.IntCmd
// HGetAll 获取 Hash 所有字段
HGetAll(ctx context.Context, key string) *redis.MapStringStringCmd
// HDel 删除 Hash 字段
HDel(ctx context.Context, key string, fields ...string) *redis.IntCmd
// Expire 设置过期时间
Expire(ctx context.Context, key string, expiration time.Duration) *redis.BoolCmd
}
ICache 定义缓存接口(抽象)
func NewRedisCache ¶
NewRedisCache 创建 Redis 缓存实例(支持单节点、Sentinel 和集群模式)
func ProvideICache ¶
ProvideICache 提供 ICache 接口实例(支持单节点、Sentinel 和集群模式)
type QueryFunc ¶
QueryFunc defines a function that queries data from database T is the type of data to be queried
type RedisCache ¶
type RedisCache struct {
// contains filtered or unexported fields
}
RedisCache Redis 缓存实现(支持单节点、Sentinel 和集群模式)
func (*RedisCache) Expire ¶
func (r *RedisCache) Expire(ctx context.Context, key string, expiration time.Duration) *redis.BoolCmd
Expire 设置过期时间
func (*RedisCache) GetCmdable ¶
func (r *RedisCache) GetCmdable() redis.Cmdable
GetCmdable 获取底层的 redis.Cmdable(支持所有模式:单节点、Sentinel 和集群)
func (*RedisCache) HGetAll ¶
func (r *RedisCache) HGetAll(ctx context.Context, key string) *redis.MapStringStringCmd
HGetAll 获取 Hash 所有字段