Documentation
¶
Index ¶
- Variables
- func WithRedisClient(client *redis.Client) func(*RedisOptions)
- func WithRedisKeyPrefix(prefix string) func(*RedisOptions)
- type Cache
- type CacheType
- type Factory
- func (f *Factory) Close() error
- func (f *Factory) CloseWithContext(ctx context.Context) error
- func (f *Factory) Create(name string, cacheType CacheType, opts ...Option) (Cache, error)
- func (f *Factory) CreateRedis(name string, client *redis.Client, opts ...Option) (Cache, error)
- func (f *Factory) CreateRedisWithContext(ctx context.Context, name string, client *redis.Client, opts ...Option) (Cache, error)
- func (f *Factory) CreateWithContext(ctx context.Context, name string, cacheType CacheType, opts ...Option) (Cache, error)
- func (f *Factory) Delete(name string) error
- func (f *Factory) DeleteWithContext(ctx context.Context, name string) error
- func (f *Factory) Get(name string) (Cache, error)
- func (f *Factory) GetWithContext(ctx context.Context, name string) (Cache, error)
- type MemoryCache
- func (c *MemoryCache) Clear(ctx context.Context) error
- func (c *MemoryCache) Close() error
- func (c *MemoryCache) Decr(ctx context.Context, key string, delta int64) (int64, error)
- func (c *MemoryCache) Delete(ctx context.Context, key string) error
- func (c *MemoryCache) DeleteMulti(ctx context.Context, keys []string) error
- func (c *MemoryCache) Exists(ctx context.Context, key string) (bool, error)
- func (c *MemoryCache) Expire(ctx context.Context, key string, expiration time.Duration) error
- func (c *MemoryCache) Get(ctx context.Context, key string) ([]byte, error)
- func (c *MemoryCache) GetMulti(ctx context.Context, keys []string) (map[string][]byte, error)
- func (c *MemoryCache) Incr(ctx context.Context, key string, delta int64) (int64, error)
- func (c *MemoryCache) Set(ctx context.Context, key string, value []byte, expiration time.Duration) error
- func (c *MemoryCache) SetMulti(ctx context.Context, items map[string][]byte, expiration time.Duration) error
- func (c *MemoryCache) TTL(ctx context.Context, key string) (time.Duration, error)
- type Option
- type Options
- type RedisCache
- func (c *RedisCache) Clear(ctx context.Context) error
- func (c *RedisCache) Close() error
- func (c *RedisCache) Decr(ctx context.Context, key string, delta int64) (int64, error)
- func (c *RedisCache) Delete(ctx context.Context, key string) error
- func (c *RedisCache) DeleteMulti(ctx context.Context, keys []string) error
- func (c *RedisCache) Exists(ctx context.Context, key string) (bool, error)
- func (c *RedisCache) Expire(ctx context.Context, key string, expiration time.Duration) error
- func (c *RedisCache) Get(ctx context.Context, key string) ([]byte, error)
- func (c *RedisCache) GetMulti(ctx context.Context, keys []string) (map[string][]byte, error)
- func (c *RedisCache) GetWithBloomFilter(ctx context.Context, key string, loader func(ctx context.Context) (any, error), ...) (any, error)
- func (c *RedisCache) GetWithProtection(ctx context.Context, key string, loader func(ctx context.Context) (any, error), ...) (any, error)
- func (c *RedisCache) Incr(ctx context.Context, key string, delta int64) (int64, error)
- func (c *RedisCache) IsNilValue(ctx context.Context, data []byte) bool
- func (c *RedisCache) Set(ctx context.Context, key string, value []byte, expiration time.Duration) error
- func (c *RedisCache) SetMulti(ctx context.Context, items map[string][]byte, expiration time.Duration) error
- func (c *RedisCache) SetNilValue(ctx context.Context, key string, expiration time.Duration) error
- func (c *RedisCache) TTL(ctx context.Context, key string) (time.Duration, error)
- func (c *RedisCache) WarmUp(ctx context.Context, keys []string, ...) error
- type RedisOptions
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotFound 缓存未找到错误 ErrNotFound = errors.New("cache: key not found") // ErrKeyExists 缓存键已存在错误 ErrKeyExists = errors.New("cache: key already exists") )
var DefaultOptions = Options{ Expiration: 5 * time.Minute, MaxEntries: 10000, }
DefaultOptions 默认缓存选项
Functions ¶
func WithRedisClient ¶
func WithRedisClient(client *redis.Client) func(*RedisOptions)
WithRedisClient 设置Redis客户端
func WithRedisKeyPrefix ¶
func WithRedisKeyPrefix(prefix string) func(*RedisOptions)
WithRedisKeyPrefix 设置Redis键前缀
Types ¶
type Cache ¶
type Cache interface {
// Get 获取缓存
Get(ctx context.Context, key string) ([]byte, error)
// Set 设置缓存
Set(ctx context.Context, key string, value []byte, expiration time.Duration) error
// Delete 删除缓存
Delete(ctx context.Context, key string) error
// Clear 清空缓存
Clear(ctx context.Context) error
// GetMulti 批量获取缓存
GetMulti(ctx context.Context, keys []string) (map[string][]byte, error)
// SetMulti 批量设置缓存
SetMulti(ctx context.Context, items map[string][]byte, expiration time.Duration) error
// DeleteMulti 批量删除缓存
DeleteMulti(ctx context.Context, keys []string) error
// Incr 自增
Incr(ctx context.Context, key string, delta int64) (int64, error)
// Decr 自减
Decr(ctx context.Context, key string, delta int64) (int64, error)
// Exists 检查缓存是否存在
Exists(ctx context.Context, key string) (bool, error)
// Expire 设置过期时间
Expire(ctx context.Context, key string, expiration time.Duration) error
// TTL 获取过期时间
TTL(ctx context.Context, key string) (time.Duration, error)
// Close 关闭缓存
Close() error
}
Cache 缓存接口
type Factory ¶
type Factory struct {
// contains filtered or unexported fields
}
Factory 缓存工厂
func (*Factory) CloseWithContext ¶
CloseWithContext 使用上下文关闭所有缓存
func (*Factory) CreateRedis ¶
CreateRedis 创建Redis缓存
func (*Factory) CreateRedisWithContext ¶
func (f *Factory) CreateRedisWithContext(ctx context.Context, name string, client *redis.Client, opts ...Option) (Cache, error)
CreateRedisWithContext 使用上下文创建Redis缓存
func (*Factory) CreateWithContext ¶
func (f *Factory) CreateWithContext(ctx context.Context, name string, cacheType CacheType, opts ...Option) (Cache, error)
CreateWithContext 使用上下文创建缓存
func (*Factory) DeleteWithContext ¶
DeleteWithContext 使用上下文删除缓存
type MemoryCache ¶
type MemoryCache struct {
// contains filtered or unexported fields
}
MemoryCache 内存缓存
func (*MemoryCache) Delete ¶
func (c *MemoryCache) Delete(ctx context.Context, key string) error
Delete 删除缓存
func (*MemoryCache) DeleteMulti ¶
func (c *MemoryCache) DeleteMulti(ctx context.Context, keys []string) error
DeleteMulti 批量删除缓存
func (*MemoryCache) Set ¶
func (c *MemoryCache) Set(ctx context.Context, key string, value []byte, expiration time.Duration) error
Set 设置缓存
type Option ¶
type Option func(*Options)
Option 缓存选项函数
func WithOnEvicted ¶
WithOnEvicted 设置缓存淘汰回调函数
type Options ¶
type Options struct {
// Expiration 默认过期时间
Expiration time.Duration
// MaxEntries 最大缓存条目数
MaxEntries int
// OnEvicted 缓存淘汰回调函数
OnEvicted func(key string, value []byte)
}
Options 缓存选项
type RedisCache ¶
type RedisCache struct {
// contains filtered or unexported fields
}
RedisCache Redis缓存实现
func NewRedisCache ¶
func NewRedisCache(client *redis.Client, opts ...Option) *RedisCache
NewRedisCache 创建Redis缓存
func (*RedisCache) Delete ¶
func (c *RedisCache) Delete(ctx context.Context, key string) error
Delete 删除缓存
func (*RedisCache) DeleteMulti ¶
func (c *RedisCache) DeleteMulti(ctx context.Context, keys []string) error
DeleteMulti 批量删除缓存
func (*RedisCache) GetWithBloomFilter ¶
func (c *RedisCache) GetWithBloomFilter(ctx context.Context, key string, loader func(ctx context.Context) (any, error), bloomFilterKey string, expiration time.Duration) (any, error)
GetWithBloomFilter 使用布隆过滤器防止缓存穿透 key: 缓存键 loader: 加载数据的函数,当缓存未命中时调用 bloomFilterKey: 布隆过滤器的键 expiration: 缓存过期时间,为0时使用默认过期时间
func (*RedisCache) GetWithProtection ¶
func (c *RedisCache) GetWithProtection(ctx context.Context, key string, loader func(ctx context.Context) (any, error), expiration time.Duration) (any, error)
GetWithProtection 获取缓存,带穿透保护 key: 缓存键 loader: 加载数据的函数,当缓存未命中时调用 expiration: 缓存过期时间,为0时使用默认过期时间
func (*RedisCache) IsNilValue ¶
func (c *RedisCache) IsNilValue(ctx context.Context, data []byte) bool
IsNilValue 检查是否为空值缓存
func (*RedisCache) Set ¶
func (c *RedisCache) Set(ctx context.Context, key string, value []byte, expiration time.Duration) error
Set 设置缓存
func (*RedisCache) SetMulti ¶
func (c *RedisCache) SetMulti(ctx context.Context, items map[string][]byte, expiration time.Duration) error
SetMulti 批量设置缓存
func (*RedisCache) SetNilValue ¶
SetNilValue 设置空值缓存,用于缓存穿透保护 当查询结果为空时,缓存一个特殊的空值,避免频繁查询数据库
type RedisOptions ¶
type RedisOptions struct {
Client *redis.Client // Redis客户端
KeyPrefix string // 键前缀
}
RedisOptions Redis缓存选项