cache

package
v1.1.7 Latest Latest
Warning

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

Go to latest
Published: May 7, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ConnStatusUnknown = iota
	ConnStatusConnected
	ConnStatusDisconnected
	ConnStatusError
)

连接状态常量

Variables

View Source
var (
	ErrCacheMiss  = errors.New("缓存不存在")
	ErrInvalidKey = errors.New("无效的缓存键")
)

定义错误常量

View Source
var (
	ErrInvalidValue = errors.New("无效的缓存值类型")
)

错误定义

Functions

func RegisterDriver

func RegisterDriver(name string, driver Driver)

RegisterDriver 注册缓存驱动

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 NewCacheProvider

func NewCacheProvider() *CacheProvider

NewCacheProvider 创建缓存服务提供者

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 Driver

type Driver interface {
	New(config map[string]interface{}) (Store, error)
}

Driver 缓存驱动接口

func GetDriver

func GetDriver(name string) (Driver, bool)

GetDriver 获取缓存驱动

type FileConfig

type FileConfig struct {
	Directory  string        // 缓存文件存储目录
	Extension  string        // 缓存文件扩展名
	GCInterval time.Duration // 垃圾回收间隔
}

文件缓存的配置选项

type FileDriver

type FileDriver struct{}

FileDriver 文件缓存驱动

func (*FileDriver) New

func (d *FileDriver) New(config map[string]interface{}) (Store, error)

New 创建新的文件缓存存储

type FileStore

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

FileStore 文件缓存存储

func (*FileStore) Clear

func (s *FileStore) Clear(ctx context.Context) error

Clear 清空缓存

func (*FileStore) Count

func (s *FileStore) Count(ctx context.Context) int64

Count 计算缓存数量

func (*FileStore) Decrement

func (s *FileStore) Decrement(ctx context.Context, key string, value int64) (int64, error)

Decrement 减少计数器值

func (*FileStore) Delete

func (s *FileStore) Delete(ctx context.Context, key string) error

Delete 删除缓存

func (*FileStore) DeleteMultiple

func (s *FileStore) DeleteMultiple(ctx context.Context, keys []string) error

DeleteMultiple 删除多个缓存项

func (*FileStore) Flush

func (s *FileStore) Flush(ctx context.Context) error

Flush 执行一次缓存清理

func (*FileStore) Get

func (s *FileStore) Get(ctx context.Context, key string) (interface{}, error)

Get 获取缓存

func (*FileStore) GetMultiple

func (s *FileStore) GetMultiple(ctx context.Context, keys []string) (map[string]interface{}, error)

GetMultiple 获取多个缓存项

func (*FileStore) Has

func (s *FileStore) Has(ctx context.Context, key string) bool

Has 检查缓存是否存在

func (*FileStore) Increment

func (s *FileStore) Increment(ctx context.Context, key string, value int64) (int64, error)

Increment 增加计数器值

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

func (s *FileStore) TaggedDelete(ctx context.Context, tag string) error

TaggedDelete 删除带有标签的缓存项

func (*FileStore) TaggedGet

func (s *FileStore) TaggedGet(ctx context.Context, tag string) (map[string]interface{}, error)

TaggedGet 获取带有标签的缓存项

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 NewManager

func NewManager() *Manager

NewManager 创建缓存管理器

func (*Manager) Clear

func (m *Manager) Clear(ctx context.Context) error

Clear 清空默认存储

func (*Manager) Decrement

func (m *Manager) Decrement(ctx context.Context, key string, value int64) (int64, error)

Decrement 减少计数器值

func (*Manager) Delete

func (m *Manager) Delete(ctx context.Context, key string) error

Delete 从默认存储删除缓存

func (*Manager) DeleteMultiple

func (m *Manager) DeleteMultiple(ctx context.Context, keys []string) error

DeleteMultiple 删除多个缓存项

func (*Manager) Get

func (m *Manager) Get(ctx context.Context, key string) (interface{}, error)

Get 从默认存储获取缓存

func (*Manager) GetMultiple

func (m *Manager) GetMultiple(ctx context.Context, keys []string) (map[string]interface{}, error)

GetMultiple 获取多个缓存项

func (*Manager) GetStore

func (m *Manager) GetStore(name string) (Store, error)

GetStore 获取指定名称的缓存存储

func (*Manager) Has

func (m *Manager) Has(ctx context.Context, key string) bool

Has 检查默认存储中是否存在缓存

func (*Manager) Increment

func (m *Manager) Increment(ctx context.Context, key string, value int64) (int64, error)

Increment 增加计数器值

func (*Manager) Register

func (m *Manager) Register(name string, config Config) error

Register 注册缓存配置

func (*Manager) Set

func (m *Manager) Set(ctx context.Context, key string, value interface{}, opts ...Option) error

Set 向默认存储设置缓存

func (*Manager) SetDefault

func (m *Manager) SetDefault(name string)

SetDefault 设置默认存储

func (*Manager) SetMultiple

func (m *Manager) SetMultiple(ctx context.Context, items map[string]interface{}, opts ...Option) error

SetMultiple 设置多个缓存项

func (*Manager) Store

func (m *Manager) Store() (Store, error)

Store 获取默认缓存存储

func (*Manager) TaggedDelete

func (m *Manager) TaggedDelete(ctx context.Context, tag string) error

TaggedDelete 删除带有标签的缓存项

func (*Manager) TaggedGet

func (m *Manager) TaggedGet(ctx context.Context, tag string) (map[string]interface{}, error)

TaggedGet 获取带有标签的缓存项

func (*Manager) WithPrefix

func (m *Manager) WithPrefix(prefix string) *PrefixedManager

WithPrefix 创建带有前缀的缓存管理器

type MemoryDriver

type MemoryDriver struct{}

MemoryDriver 内存缓存驱动

func (*MemoryDriver) New

func (d *MemoryDriver) New(config map[string]interface{}) (Store, error)

New 创建新的内存缓存实例

type MemoryStore

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

MemoryStore 内存缓存存储实现

func NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore 创建新的内存缓存存储

func (*MemoryStore) Clear

func (s *MemoryStore) Clear(ctx context.Context) error

Clear 清空所有缓存

func (*MemoryStore) Count

func (s *MemoryStore) Count(ctx context.Context) int64

Count 返回缓存项数量

func (*MemoryStore) Decrement

func (s *MemoryStore) Decrement(ctx context.Context, key string, value int64) (int64, error)

Decrement 减少计数器值

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) Flush

func (s *MemoryStore) Flush(ctx context.Context) error

Flush 清理并停止服务

func (*MemoryStore) GC

func (s *MemoryStore) GC(ctx context.Context) error

GC 垃圾回收,清理过期的缓存项

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) Increment

func (s *MemoryStore) Increment(ctx context.Context, key string, value int64) (int64, error)

Increment 增加计数器值

func (*MemoryStore) Set

func (s *MemoryStore) Set(ctx context.Context, key string, value interface{}, opts ...Option) error

Set 设置缓存项

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 删除带有指定标签的所有项

func (*MemoryStore) TaggedGet

func (s *MemoryStore) TaggedGet(ctx context.Context, tag string) (map[string]interface{}, error)

TaggedGet 获取带有指定标签的所有项

type Option

type Option func(*Options)

Option 缓存配置函数

func WithExpiration

func WithExpiration(expiration time.Duration) Option

WithExpiration 设置过期时间

func WithTags

func WithTags(tags ...string) Option

WithTags 设置标签

type Options

type Options struct {
	Expiration time.Duration // 过期时间
	Tags       []string      // 标签
}

Options 缓存选项

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 RedisDriver

type RedisDriver struct{}

RedisDriver Redis缓存驱动

func (*RedisDriver) New

func (d *RedisDriver) New(config map[string]interface{}) (Store, error)

New 创建新的Redis缓存实例

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) Clear

func (r *RedisStore) Clear(ctx context.Context) error

Clear 清空缓存

func (*RedisStore) Close

func (r *RedisStore) Close() error

Close 关闭Redis存储及相关资源

func (*RedisStore) Count

func (r *RedisStore) Count(ctx context.Context) int64

Count 返回缓存中的项目数

func (*RedisStore) Decrement

func (r *RedisStore) Decrement(ctx context.Context, key string, value int64) (int64, error)

Decrement 减少缓存项的整数值

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) Flush

func (r *RedisStore) Flush(ctx context.Context) error

Flush 刷新缓存,删除所有项目

func (*RedisStore) Get

func (r *RedisStore) Get(ctx context.Context, key string) (interface{}, error)

Get 从缓存中获取一个项目

func (*RedisStore) GetClient

func (r *RedisStore) GetClient() *redis.Client

GetClient 获取Redis客户端

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) GetPrefix

func (r *RedisStore) GetPrefix() string

GetPrefix 获取键前缀

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) Increment

func (r *RedisStore) Increment(ctx context.Context, key string, value int64) (int64, error)

Increment 增加缓存项的整数值

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 删除带标签的所有缓存项

func (*RedisStore) TaggedGet

func (r *RedisStore) TaggedGet(ctx context.Context, tag string) (map[string]interface{}, error)

TaggedGet 获取带标签的缓存项

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

func (m *RedisTagManager) AddTagsToKey(ctx context.Context, key string, tags []string) error

AddTagsToKey 为缓存键添加标签

func (*RedisTagManager) GetKeyTags

func (m *RedisTagManager) GetKeyTags(ctx context.Context, key string) ([]string, error)

GetKeyTags 获取键关联的所有标签

func (*RedisTagManager) GetKeysByTag

func (m *RedisTagManager) GetKeysByTag(ctx context.Context, tag string) ([]string, error)

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

func (m *RedisTagManager) RemoveTagsFromKey(ctx context.Context, key string, tags []string) error

RemoveTagsFromKey 从缓存键中移除标签

type StandardTagManager

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

StandardTagManager 标准标签管理器实现

func (*StandardTagManager) AddTagsToKey

func (m *StandardTagManager) AddTagsToKey(ctx context.Context, key string, tags []string) error

AddTagsToKey 为缓存键添加标签

func (*StandardTagManager) GetKeyTags

func (m *StandardTagManager) GetKeyTags(ctx context.Context, key string) ([]string, error)

GetKeyTags 获取键关联的所有标签

func (*StandardTagManager) GetKeysByTag

func (m *StandardTagManager) GetKeysByTag(ctx context.Context, tag string) ([]string, error)

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 缓存存储接口

func New

func New(driver string, config map[string]interface{}) (Store, error)

New 创建新的缓存实例

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 标签管理器接口

func NewTagManager

func NewTagManager(store Store) TagManager

NewTagManager 创建新的标签管理器

Jump to

Keyboard shortcuts

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