cache

package
v0.0.0-...-d68a214 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 8, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCacheMiss indicates that the key was not found in cache
	ErrCacheMiss = redis.Nil
)

ProviderSet 提供缓存相关的依赖(支持单节点、Sentinel 和集群模式)

Functions

func NewRedisCmdable

func NewRedisCmdable(cfg Redis) (redis.Cmdable, error)

NewRedisCmdable 创建 Redis 客户端(支持单节点、Sentinel 和集群模式)

func ProvideRedisCmdable

func ProvideRedisCmdable(conf Redis) (redis.Cmdable, error)

ProvideRedisCmdable 提供 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

func WithTTL

func WithTTL[T any](ttl time.Duration) CachedQueryOption[T]

WithTTL sets the cache expiration time Note: This function doesn't need type parameter but we keep it for consistency Use WithTTL[YourType] or let type inference work from NewCachedQuery context

type HashMarshalFunc

type HashMarshalFunc[T any] func(value T) map[string]interface{}

HashMarshalFunc defines a function that converts a value to hash fields

type HashUnmarshalFunc

type HashUnmarshalFunc[T any] func(hashData map[string]string) (T, error)

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

func NewRedisCache(cmdable redis.Cmdable) ICache

NewRedisCache 创建 Redis 缓存实例(支持单节点、Sentinel 和集群模式)

func ProvideICache

func ProvideICache(cmdable redis.Cmdable) ICache

ProvideICache 提供 ICache 接口实例(支持单节点、Sentinel 和集群模式)

type KeyFunc

type KeyFunc func(params ...any) string

KeyFunc defines a function that generates cache key from parameters

type QueryFunc

type QueryFunc[T any] func(ctx context.Context) (T, error)

QueryFunc defines a function that queries data from database T is the type of data to be queried

type Redis

type Redis struct {
	Mode             string
	Address          string
	Password         string
	DB               int
	PoolSize         int
	UseTLS           bool
	MasterName       string
	SentinelUsername string
	SentinelPassword string
	DialTimeout      time.Duration // 连接超时
	ReadTimeout      time.Duration // 读超时
	WriteTimeout     time.Duration // 写超时
	MaxRedirects     int           // 集群模式最大重定向次数
}

type RedisCache

type RedisCache struct {
	// contains filtered or unexported fields
}

RedisCache Redis 缓存实现(支持单节点、Sentinel 和集群模式)

func (*RedisCache) Del

func (r *RedisCache) Del(ctx context.Context, keys ...string) *redis.IntCmd

Del 删除缓存

func (*RedisCache) Expire

func (r *RedisCache) Expire(ctx context.Context, key string, expiration time.Duration) *redis.BoolCmd

Expire 设置过期时间

func (*RedisCache) Get

func (r *RedisCache) Get(ctx context.Context, key string) *redis.StringCmd

Get 获取缓存值

func (*RedisCache) GetCmdable

func (r *RedisCache) GetCmdable() redis.Cmdable

GetCmdable 获取底层的 redis.Cmdable(支持所有模式:单节点、Sentinel 和集群)

func (*RedisCache) HDel

func (r *RedisCache) HDel(ctx context.Context, key string, fields ...string) *redis.IntCmd

HDel 删除 Hash 字段

func (*RedisCache) HGetAll

func (r *RedisCache) HGetAll(ctx context.Context, key string) *redis.MapStringStringCmd

HGetAll 获取 Hash 所有字段

func (*RedisCache) HSet

func (r *RedisCache) HSet(ctx context.Context, key string, values ...any) *redis.IntCmd

HSet 设置 Hash 字段

func (*RedisCache) Pipeline

func (r *RedisCache) Pipeline() redis.Pipeliner

Pipeline 创建管道

func (*RedisCache) Set

func (r *RedisCache) Set(ctx context.Context, key string, value any, expiration time.Duration) *redis.StatusCmd

Set 设置缓存值

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL