cache

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheManager

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

CacheManager 结构体,管理内存缓存和 Redis 缓存

func NewCacheManager

func NewCacheManager(config *configs.CacheConfig) *CacheManager

NewCacheManager 创建 CacheManager 实例

func (*CacheManager) Delete

func (cm *CacheManager) Delete(key string) error

Delete 删除缓存数据

func (*CacheManager) Get

func (cm *CacheManager) Get(key string) ([]byte, bool, error)

Get 获取缓存数据

func (*CacheManager) GetStats

func (cm *CacheManager) GetStats() (memoryRequestQPS, memoryHitQPS, redisRequestQPS, redisHitQPS float64)

GetStats 获取缓存统计数据

func (*CacheManager) Keys

func (cm *CacheManager) Keys(prefix string) (keys []interface{}, err error)

Keys 获取缓存中指定前缀的key列表,如果prefix为空则返回所有key

func (*CacheManager) PrewarmCache

func (cm *CacheManager) PrewarmCache(keys []string) error

PrewarmCache 预热缓存

func (*CacheManager) PrewarmCacheFromPattern

func (cm *CacheManager) PrewarmCacheFromPattern(pattern string) error

PrewarmCacheFromPattern 从 Redis 模式匹配预热缓存

func (*CacheManager) Set

func (cm *CacheManager) Set(key string, value []byte, ttl time.Duration) error

Set 设置缓存数据

func (*CacheManager) WithErrorHandler

func (cm *CacheManager) WithErrorHandler(handler ErrorHandler) *CacheManager

WithErrorHandler 设置错误处理器

type CacheStats

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

CacheStats 结构体,用于记录缓存统计信息

func NewCacheStats

func NewCacheStats() *CacheStats

NewCacheStats 创建 CacheStats 实例

func (*CacheStats) GetStats

func (cs *CacheStats) GetStats() (requestQPS, hitQPS float64)

GetStats 获取统计信息

func (*CacheStats) IncrementHitCount

func (cs *CacheStats) IncrementHitCount()

IncrementHitCount 增加命中计数

func (*CacheStats) IncrementRequestCount

func (cs *CacheStats) IncrementRequestCount()

IncrementRequestCount 增加请求计数

type CacheStorage

type CacheStorage interface {
	Set(key string, value []byte, ttl time.Duration) error
	Get(key string) ([]byte, bool, error)
	Delete(key string) error
}

CacheStorage 定义缓存存储接口

type Config

type Config struct {
	MemoryCacheSize int    // 内存缓存大小(字节)
	RedisAddr       string // Redis 地址
	RedisPassword   string // Redis 密码
	RedisDB         int    // Redis 数据库编号
	RedisPrefix     string // Redis 键前缀
}

Config 结构体用于存储缓存系统的配置

type DistributedCache

type DistributedCache interface {
	CacheStorage
	Publish(channel string, message []byte) error
	Subscribe(channel string) (<-chan []byte, error)
}

DistributedCache 定义分布式缓存接口

type ErrorHandler

type ErrorHandler interface {
	HandleError(err error)
}

ErrorHandler 自定义错误处理接口

type FreeCache

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

FreeCache 结构体,实现内存缓存

func NewFreeCache

func NewFreeCache(size int) *FreeCache

NewFreeCache 创建 FreeCache 实例

func (*FreeCache) Delete

func (fc *FreeCache) Delete(key string) error

Delete 从内存缓存中删除数据

func (*FreeCache) Get

func (fc *FreeCache) Get(key string) ([]byte, bool)

Get 从内存缓存中获取数据

func (*FreeCache) GetStats

func (fc *FreeCache) GetStats() (float64, float64)

GetStats 获取内存缓存的统计信息

func (*FreeCache) Set

func (fc *FreeCache) Set(key string, value []byte, ttl time.Duration) error

Set 设置数据到内存缓存

type RedisCache

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

RedisCache 结构体,实现 Redis 缓存

func NewRedisCache

func NewRedisCache(config *configs.CacheConfig) *RedisCache

NewRedisCache 创建 RedisCache 实例

func (*RedisCache) Delete

func (rc *RedisCache) Delete(key string) error

Delete 从 Redis 删除数据

func (*RedisCache) Get

func (rc *RedisCache) Get(key string) ([]byte, bool, error)

Get 从 Redis 获取数据

func (*RedisCache) GetStats

func (rc *RedisCache) GetStats() (float64, float64)

GetStats 获取 Redis 缓存的统计信息

func (*RedisCache) Keys

func (rc *RedisCache) Keys(prefix string) ([]string, error)

Keys 获取Redis中指定前缀的key列表

func (*RedisCache) Set

func (rc *RedisCache) Set(key string, value []byte, ttl time.Duration) error

Set 设置数据到 Redis

func (*RedisCache) SubscribeExpiryEvents

func (rc *RedisCache) SubscribeExpiryEvents(callback func(string))

SubscribeExpiryEvents 订阅 Redis 键过期事件

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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