Documentation
¶
Index ¶
- Constants
- Variables
- func RegisterDriver(name string, driver Driver)
- func WithRedisExpiry(expiry time.Duration) func(*RedisOptions)
- func WithRedisHealthCheck(enabled bool, interval time.Duration) func(*RedisOptions)
- func WithRedisPool(maxRetries, poolSize, minIdleConns int) func(*RedisOptions)
- func WithRedisPrefix(prefix string) func(*RedisOptions)
- func WithRedisTagManager(tagManager TagManager) func(*RedisOptions)
- type CacheProvider
- type Config
- type Driver
- type FileConfig
- type FileDriver
- type FileStore
- func (s *FileStore) Clear(ctx context.Context) error
- func (s *FileStore) Count(ctx context.Context) int64
- func (s *FileStore) Decrement(ctx context.Context, key string, value int64) (int64, error)
- func (s *FileStore) Delete(ctx context.Context, key string) error
- func (s *FileStore) DeleteMultiple(ctx context.Context, keys []string) error
- func (s *FileStore) Flush(ctx context.Context) error
- func (s *FileStore) Get(ctx context.Context, key string) (interface{}, error)
- func (s *FileStore) GetMultiple(ctx context.Context, keys []string) (map[string]interface{}, error)
- func (s *FileStore) Has(ctx context.Context, key string) bool
- func (s *FileStore) Increment(ctx context.Context, key string, value int64) (int64, error)
- func (s *FileStore) Set(ctx context.Context, key string, value interface{}, options ...Option) error
- func (s *FileStore) SetMultiple(ctx context.Context, items map[string]interface{}, options ...Option) error
- func (s *FileStore) TaggedDelete(ctx context.Context, tag string) error
- func (s *FileStore) TaggedGet(ctx context.Context, tag string) (map[string]interface{}, error)
- type Item
- type Manager
- func (m *Manager) Clear(ctx context.Context) error
- func (m *Manager) Decrement(ctx context.Context, key string, value int64) (int64, error)
- func (m *Manager) Delete(ctx context.Context, key string) error
- func (m *Manager) DeleteMultiple(ctx context.Context, keys []string) error
- func (m *Manager) Get(ctx context.Context, key string) (interface{}, error)
- func (m *Manager) GetMultiple(ctx context.Context, keys []string) (map[string]interface{}, error)
- func (m *Manager) GetStore(name string) (Store, error)
- func (m *Manager) Has(ctx context.Context, key string) bool
- func (m *Manager) Increment(ctx context.Context, key string, value int64) (int64, error)
- func (m *Manager) Register(name string, config Config) error
- func (m *Manager) Set(ctx context.Context, key string, value interface{}, opts ...Option) error
- func (m *Manager) SetDefault(name string)
- func (m *Manager) SetMultiple(ctx context.Context, items map[string]interface{}, opts ...Option) error
- func (m *Manager) Store() (Store, error)
- func (m *Manager) TaggedDelete(ctx context.Context, tag string) error
- func (m *Manager) TaggedGet(ctx context.Context, tag string) (map[string]interface{}, error)
- func (m *Manager) WithPrefix(prefix string) *PrefixedManager
- type MemoryDriver
- type MemoryStore
- func (s *MemoryStore) Clear(ctx context.Context) error
- func (s *MemoryStore) Count(ctx context.Context) int64
- func (s *MemoryStore) Decrement(ctx context.Context, key string, value int64) (int64, error)
- func (s *MemoryStore) Delete(ctx context.Context, key string) error
- func (s *MemoryStore) DeleteMultiple(ctx context.Context, keys []string) error
- func (s *MemoryStore) Flush(ctx context.Context) error
- func (s *MemoryStore) GC(ctx context.Context) error
- func (s *MemoryStore) Get(ctx context.Context, key string) (interface{}, error)
- func (s *MemoryStore) GetMultiple(ctx context.Context, keys []string) (map[string]interface{}, error)
- func (s *MemoryStore) Has(ctx context.Context, key string) bool
- func (s *MemoryStore) Increment(ctx context.Context, key string, value int64) (int64, error)
- func (s *MemoryStore) Set(ctx context.Context, key string, value interface{}, opts ...Option) error
- func (s *MemoryStore) SetMultiple(ctx context.Context, items map[string]interface{}, opts ...Option) error
- func (s *MemoryStore) TaggedDelete(ctx context.Context, tag string) error
- func (s *MemoryStore) TaggedGet(ctx context.Context, tag string) (map[string]interface{}, error)
- type Option
- type Options
- type PrefixedManager
- func (p *PrefixedManager) Delete(ctx context.Context, key string) error
- func (p *PrefixedManager) DeleteMultiple(ctx context.Context, keys []string) error
- func (p *PrefixedManager) Get(ctx context.Context, key string) (interface{}, error)
- func (p *PrefixedManager) GetMultiple(ctx context.Context, keys []string) (map[string]interface{}, error)
- func (p *PrefixedManager) Has(ctx context.Context, key string) bool
- func (p *PrefixedManager) Set(ctx context.Context, key string, value interface{}, opts ...Option) error
- func (p *PrefixedManager) SetMultiple(ctx context.Context, items map[string]interface{}, opts ...Option) error
- type RedisDriver
- type RedisOptions
- type RedisStore
- func (r *RedisStore) Clear(ctx context.Context) error
- func (r *RedisStore) Close() error
- func (r *RedisStore) Count(ctx context.Context) int64
- func (r *RedisStore) Decrement(ctx context.Context, key string, value int64) (int64, error)
- func (r *RedisStore) DecrementFloat(ctx context.Context, key string, value float64) (float64, error)
- func (r *RedisStore) Delete(ctx context.Context, key string) error
- func (r *RedisStore) DeleteMultiple(ctx context.Context, keys []string) error
- func (r *RedisStore) Flush(ctx context.Context) error
- func (r *RedisStore) Get(ctx context.Context, key string) (interface{}, error)
- func (r *RedisStore) GetClient() *redis.Client
- func (r *RedisStore) GetDefaultExpiry() time.Duration
- func (r *RedisStore) GetHealthStatus() int
- func (r *RedisStore) GetMultiple(ctx context.Context, keys []string) (map[string]interface{}, error)
- func (r *RedisStore) GetPrefix() string
- func (r *RedisStore) GetTagManager() TagManager
- func (r *RedisStore) Has(ctx context.Context, key string) bool
- func (r *RedisStore) Increment(ctx context.Context, key string, value int64) (int64, error)
- func (r *RedisStore) IncrementFloat(ctx context.Context, key string, value float64) (float64, error)
- func (r *RedisStore) Set(ctx context.Context, key string, value interface{}, options ...Option) error
- func (r *RedisStore) SetMultiple(ctx context.Context, items map[string]interface{}, options ...Option) error
- func (r *RedisStore) TaggedDelete(ctx context.Context, tag string) error
- func (r *RedisStore) TaggedGet(ctx context.Context, tag string) (map[string]interface{}, error)
- type RedisTagManager
- func (m *RedisTagManager) AddTagsToKey(ctx context.Context, key string, tags []string) error
- func (m *RedisTagManager) GetKeyTags(ctx context.Context, key string) ([]string, error)
- func (m *RedisTagManager) GetKeysByTag(ctx context.Context, tag string) ([]string, error)
- func (m *RedisTagManager) RemoveKeyFromAllTags(ctx context.Context, key string) error
- func (m *RedisTagManager) RemoveTag(ctx context.Context, tag string) error
- func (m *RedisTagManager) RemoveTagsFromKey(ctx context.Context, key string, tags []string) error
- type StandardTagManager
- func (m *StandardTagManager) AddTagsToKey(ctx context.Context, key string, tags []string) error
- func (m *StandardTagManager) GetKeyTags(ctx context.Context, key string) ([]string, error)
- func (m *StandardTagManager) GetKeysByTag(ctx context.Context, tag string) ([]string, error)
- func (m *StandardTagManager) RemoveKeyFromAllTags(ctx context.Context, key string) error
- func (m *StandardTagManager) RemoveTag(ctx context.Context, tag string) error
- func (m *StandardTagManager) RemoveTagsFromKey(ctx context.Context, key string, tags []string) error
- type Store
- type TagManager
Constants ¶
const ( ConnStatusUnknown = iota ConnStatusConnected ConnStatusDisconnected ConnStatusError )
连接状态常量
Variables ¶
var ( ErrCacheMiss = errors.New("缓存不存在") ErrInvalidKey = errors.New("无效的缓存键") )
定义错误常量
var (
ErrInvalidValue = errors.New("无效的缓存值类型")
)
错误定义
Functions ¶
func WithRedisExpiry ¶
func WithRedisExpiry(expiry time.Duration) func(*RedisOptions)
WithRedisExpiry 设置默认过期时间
func WithRedisHealthCheck ¶
func WithRedisHealthCheck(enabled bool, interval time.Duration) func(*RedisOptions)
WithRedisHealthCheck 设置健康检查选项
func WithRedisPool ¶
func WithRedisPool(maxRetries, poolSize, minIdleConns int) func(*RedisOptions)
WithRedisPool 设置连接池选项
func WithRedisPrefix ¶
func WithRedisPrefix(prefix string) func(*RedisOptions)
WithRedisPrefix 设置缓存键前缀
func WithRedisTagManager ¶
func WithRedisTagManager(tagManager TagManager) func(*RedisOptions)
WithRedisTagManager 设置标签管理器
Types ¶
type CacheProvider ¶
type CacheProvider struct {
*app.BaseProvider
}
CacheProvider 缓存服务提供者
func (*CacheProvider) Boot ¶
func (p *CacheProvider) Boot(application *app.Application) error
Boot 启动缓存服务
func (*CacheProvider) Register ¶
func (p *CacheProvider) Register(application *app.Application) error
Register 注册缓存服务
type Config ¶
type Config struct {
Driver string // 驱动类型
Prefix string // 键前缀
TTL time.Duration // 默认过期时间
Config map[string]interface{} // 驱动特定配置
}
Config 缓存配置
type FileConfig ¶
type FileConfig struct {
Directory string // 缓存文件存储目录
Extension string // 缓存文件扩展名
GCInterval time.Duration // 垃圾回收间隔
}
文件缓存的配置选项
type FileStore ¶
type FileStore struct {
// contains filtered or unexported fields
}
FileStore 文件缓存存储
func (*FileStore) DeleteMultiple ¶
DeleteMultiple 删除多个缓存项
func (*FileStore) GetMultiple ¶
GetMultiple 获取多个缓存项
func (*FileStore) Set ¶
func (s *FileStore) Set(ctx context.Context, key string, value interface{}, options ...Option) error
Set 设置缓存
func (*FileStore) SetMultiple ¶
func (s *FileStore) SetMultiple(ctx context.Context, items map[string]interface{}, options ...Option) error
SetMultiple 设置多个缓存项
func (*FileStore) TaggedDelete ¶
TaggedDelete 删除带有标签的缓存项
type Item ¶
type Item struct {
Key string // 缓存键
Value interface{} // 缓存值
Tags []string // 关联标签
Expiration time.Duration // 过期时间
CreatedAt time.Time // 创建时间
}
Item 缓存项结构
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager 缓存管理器
func GetInstance ¶
func GetInstance(application *app.Application) (*Manager, error)
GetInstance 获取缓存管理器实例(便捷方法)
func (*Manager) DeleteMultiple ¶
DeleteMultiple 删除多个缓存项
func (*Manager) GetMultiple ¶
GetMultiple 获取多个缓存项
func (*Manager) SetMultiple ¶
func (m *Manager) SetMultiple(ctx context.Context, items map[string]interface{}, opts ...Option) error
SetMultiple 设置多个缓存项
func (*Manager) TaggedDelete ¶
TaggedDelete 删除带有标签的缓存项
func (*Manager) WithPrefix ¶
func (m *Manager) WithPrefix(prefix string) *PrefixedManager
WithPrefix 创建带有前缀的缓存管理器
type MemoryStore ¶
type MemoryStore struct {
// contains filtered or unexported fields
}
MemoryStore 内存缓存存储实现
func (*MemoryStore) Delete ¶
func (s *MemoryStore) Delete(ctx context.Context, key string) error
Delete 删除缓存项
func (*MemoryStore) DeleteMultiple ¶
func (s *MemoryStore) DeleteMultiple(ctx context.Context, keys []string) error
DeleteMultiple 删除多个缓存项
func (*MemoryStore) Get ¶
func (s *MemoryStore) Get(ctx context.Context, key string) (interface{}, error)
Get 获取缓存项
func (*MemoryStore) GetMultiple ¶
func (s *MemoryStore) GetMultiple(ctx context.Context, keys []string) (map[string]interface{}, error)
GetMultiple 获取多个缓存项
func (*MemoryStore) Has ¶
func (s *MemoryStore) Has(ctx context.Context, key string) bool
Has 检查缓存项是否存在
func (*MemoryStore) SetMultiple ¶
func (s *MemoryStore) SetMultiple(ctx context.Context, items map[string]interface{}, opts ...Option) error
SetMultiple 设置多个缓存项
func (*MemoryStore) TaggedDelete ¶
func (s *MemoryStore) TaggedDelete(ctx context.Context, tag string) error
TaggedDelete 删除带有指定标签的所有项
type PrefixedManager ¶
type PrefixedManager struct {
// contains filtered or unexported fields
}
PrefixedManager 带前缀的缓存管理器
func (*PrefixedManager) Delete ¶
func (p *PrefixedManager) Delete(ctx context.Context, key string) error
Delete 删除缓存
func (*PrefixedManager) DeleteMultiple ¶
func (p *PrefixedManager) DeleteMultiple(ctx context.Context, keys []string) error
DeleteMultiple 删除多个缓存项
func (*PrefixedManager) Get ¶
func (p *PrefixedManager) Get(ctx context.Context, key string) (interface{}, error)
Get 获取缓存
func (*PrefixedManager) GetMultiple ¶
func (p *PrefixedManager) GetMultiple(ctx context.Context, keys []string) (map[string]interface{}, error)
GetMultiple 获取多个缓存项
func (*PrefixedManager) Has ¶
func (p *PrefixedManager) Has(ctx context.Context, key string) bool
Has 检查缓存是否存在
func (*PrefixedManager) Set ¶
func (p *PrefixedManager) Set(ctx context.Context, key string, value interface{}, opts ...Option) error
Set 设置缓存
func (*PrefixedManager) SetMultiple ¶
func (p *PrefixedManager) SetMultiple(ctx context.Context, items map[string]interface{}, opts ...Option) error
SetMultiple 设置多个缓存项
type RedisOptions ¶
type RedisOptions struct {
Prefix string
DefaultExpiry time.Duration
TagManager TagManager
HealthCheck bool
HealthCheckInterval time.Duration
MaxRetries int
PoolSize int
MinIdleConns int
}
RedisOptions 用于配置Redis缓存
type RedisStore ¶
type RedisStore struct {
// contains filtered or unexported fields
}
RedisStore 实现了Store接口,使用Redis作为存储后端
func NewRedisStore ¶
func NewRedisStore(client *redis.Client, opts ...func(*RedisOptions)) *RedisStore
NewRedisStore 创建一个新的Redis缓存存储
func (*RedisStore) DecrementFloat ¶
func (r *RedisStore) DecrementFloat(ctx context.Context, key string, value float64) (float64, error)
DecrementFloat 减少缓存项的浮点值
func (*RedisStore) Delete ¶
func (r *RedisStore) Delete(ctx context.Context, key string) error
Delete 从缓存中删除一个项目
func (*RedisStore) DeleteMultiple ¶
func (r *RedisStore) DeleteMultiple(ctx context.Context, keys []string) error
DeleteMultiple 批量删除多个缓存项
func (*RedisStore) Get ¶
func (r *RedisStore) Get(ctx context.Context, key string) (interface{}, error)
Get 从缓存中获取一个项目
func (*RedisStore) GetDefaultExpiry ¶
func (r *RedisStore) GetDefaultExpiry() time.Duration
GetDefaultExpiry 获取默认过期时间
func (*RedisStore) GetHealthStatus ¶
func (r *RedisStore) GetHealthStatus() int
GetHealthStatus 获取Redis连接健康状态
func (*RedisStore) GetMultiple ¶
func (r *RedisStore) GetMultiple(ctx context.Context, keys []string) (map[string]interface{}, error)
GetMultiple 批量获取多个缓存项
func (*RedisStore) GetTagManager ¶
func (r *RedisStore) GetTagManager() TagManager
GetTagManager 获取标签管理器
func (*RedisStore) Has ¶
func (r *RedisStore) Has(ctx context.Context, key string) bool
Has 检查缓存中是否存在一个项目
func (*RedisStore) IncrementFloat ¶
func (r *RedisStore) IncrementFloat(ctx context.Context, key string, value float64) (float64, error)
IncrementFloat 增加缓存项的浮点值
func (*RedisStore) Set ¶
func (r *RedisStore) Set(ctx context.Context, key string, value interface{}, options ...Option) error
Set 将一个项目放入缓存
func (*RedisStore) SetMultiple ¶
func (r *RedisStore) SetMultiple(ctx context.Context, items map[string]interface{}, options ...Option) error
SetMultiple 批量设置多个缓存项
func (*RedisStore) TaggedDelete ¶
func (r *RedisStore) TaggedDelete(ctx context.Context, tag string) error
TaggedDelete 删除带标签的所有缓存项
type RedisTagManager ¶
type RedisTagManager struct {
// contains filtered or unexported fields
}
RedisTagManager 实现TagManager接口,使用Redis作为存储后端
func NewRedisTagManager ¶
func NewRedisTagManager(client *redis.Client, prefix string) *RedisTagManager
NewRedisTagManager 创建一个新的Redis标签管理器
func (*RedisTagManager) AddTagsToKey ¶
AddTagsToKey 为缓存键添加标签
func (*RedisTagManager) GetKeyTags ¶
GetKeyTags 获取键关联的所有标签
func (*RedisTagManager) GetKeysByTag ¶
GetKeysByTag 根据标签获取所有关联的键
func (*RedisTagManager) RemoveKeyFromAllTags ¶
func (m *RedisTagManager) RemoveKeyFromAllTags(ctx context.Context, key string) error
RemoveKeyFromAllTags 从所有标签中移除指定的键
func (*RedisTagManager) RemoveTag ¶
func (m *RedisTagManager) RemoveTag(ctx context.Context, tag string) error
RemoveTag 移除标签及其所有关联
func (*RedisTagManager) RemoveTagsFromKey ¶
RemoveTagsFromKey 从缓存键中移除标签
type StandardTagManager ¶
type StandardTagManager struct {
// contains filtered or unexported fields
}
StandardTagManager 标准标签管理器实现
func (*StandardTagManager) AddTagsToKey ¶
AddTagsToKey 为缓存键添加标签
func (*StandardTagManager) GetKeyTags ¶
GetKeyTags 获取键关联的所有标签
func (*StandardTagManager) GetKeysByTag ¶
GetKeysByTag 根据标签获取所有关联的键
func (*StandardTagManager) RemoveKeyFromAllTags ¶
func (m *StandardTagManager) RemoveKeyFromAllTags(ctx context.Context, key string) error
RemoveKeyFromAllTags 从所有标签中移除指定的键
func (*StandardTagManager) RemoveTag ¶
func (m *StandardTagManager) RemoveTag(ctx context.Context, tag string) error
RemoveTag 移除标签及其所有关联
func (*StandardTagManager) RemoveTagsFromKey ¶
func (m *StandardTagManager) RemoveTagsFromKey(ctx context.Context, key string, tags []string) error
RemoveTagsFromKey 从缓存键中移除标签
type Store ¶
type Store interface {
// 基本操作
Get(ctx context.Context, key string) (interface{}, error)
Set(ctx context.Context, key string, value interface{}, options ...Option) error
Delete(ctx context.Context, key string) error
Has(ctx context.Context, key string) bool
Clear(ctx context.Context) error
// 批量操作
GetMultiple(ctx context.Context, keys []string) (map[string]interface{}, error)
SetMultiple(ctx context.Context, items map[string]interface{}, options ...Option) error
DeleteMultiple(ctx context.Context, keys []string) error
// 计数器操作
Increment(ctx context.Context, key string, value int64) (int64, error)
Decrement(ctx context.Context, key string, value int64) (int64, error)
// 标签操作
TaggedGet(ctx context.Context, tag string) (map[string]interface{}, error)
TaggedDelete(ctx context.Context, tag string) error
// 统计操作
Count(ctx context.Context) int64
// 维护操作
Flush(ctx context.Context) error
}
Store 缓存存储接口
type TagManager ¶
type TagManager interface {
// AddTagsToKey 为缓存键添加标签
AddTagsToKey(ctx context.Context, key string, tags []string) error
// RemoveTagsFromKey 从缓存键中移除标签
RemoveTagsFromKey(ctx context.Context, key string, tags []string) error
// GetKeysByTag 根据标签获取所有关联的键
GetKeysByTag(ctx context.Context, tag string) ([]string, error)
// RemoveTag 移除标签及其所有关联
RemoveTag(ctx context.Context, tag string) error
// RemoveKeyFromAllTags 从所有标签中移除指定的键
RemoveKeyFromAllTags(ctx context.Context, key string) error
// GetKeyTags 获取键关联的所有标签
GetKeyTags(ctx context.Context, key string) ([]string, error)
}
TagManager 标签管理器接口