cache

package module
v0.0.0-...-c3e81b5 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2025 License: Apache-2.0 Imports: 27 Imported by: 26

Documentation

Index

Constants

View Source
const (
	// For use with functions that take an expiration time.
	NoExpiration time.Duration = -1
	// For use with functions that take an expiration time. Equivalent to
	// passing in the same expiration duration as was given to New() or
	// NewFrom() when the cache was created (e.g. 5 minutes.)
	DefaultExpiration time.Duration = 0
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	Set(k string, x interface{}, d time.Duration) error
	SetDefault(k string, x interface{}) error
	Add(k string, x interface{}, d time.Duration) error
	Replace(k string, x interface{}, d time.Duration) error
	Get(k string, target interface{}) (bool, error)
	GetWithExpiration(k string, target interface{}) (bool, time.Time)
	Increment(k string, n int64) error
	IncrementFloat(k string, n float64) error
	Decrement(k string, n int64) error
	DecrementFloat(k string, n float64) error
	Delete(k string)
	DeleteExpired()
	OnEvicted(f func(string, interface{}))
	ItemCount() int
	Flush()
	Close()
}

Cache 统一缓存接口

func New

func New(defaultExpiration, cleanupInterval time.Duration) Cache

根据环境变量选择缓存实现

type HYYCache

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

HYYCache 两级缓存实现

func NewHYYCache

func NewHYYCache(local *MemoryCache, remote *RedisCache) *HYYCache

NewHYYCache 创建两级缓存

func (*HYYCache) Add

func (t *HYYCache) Add(k string, x interface{}, d time.Duration) error

Add 添加缓存

func (*HYYCache) Close

func (t *HYYCache) Close()

Close 关闭缓存(适配新的连接管理)

func (*HYYCache) Decrement

func (t *HYYCache) Decrement(k string, n int64) error

Decrement 减少整数值(适配新的modifyNumber方法)

func (*HYYCache) DecrementFloat

func (t *HYYCache) DecrementFloat(k string, n float64) error

DecrementFloat 减少浮点数值(适配新的modifyNumber方法)

func (*HYYCache) Delete

func (t *HYYCache) Delete(k string)

Delete 删除缓存

func (*HYYCache) DeleteExpired

func (t *HYYCache) DeleteExpired()

DeleteExpired 删除过期缓存

func (*HYYCache) Flush

func (t *HYYCache) Flush()

Flush 清空缓存

func (*HYYCache) Get

func (t *HYYCache) Get(k string, target interface{}) (bool, error)

Get 获取缓存(兼容流式压缩数据)

func (*HYYCache) GetWithExpiration

func (t *HYYCache) GetWithExpiration(k string, target interface{}) (bool, time.Time)

GetWithExpiration 获取缓存及其过期时间

func (*HYYCache) Increment

func (t *HYYCache) Increment(k string, n int64) error

Increment 增加整数值(适配新的modifyNumber方法)

func (*HYYCache) IncrementFloat

func (t *HYYCache) IncrementFloat(k string, n float64) error

IncrementFloat 增加浮点数值(适配新的modifyNumber方法)

func (*HYYCache) ItemCount

func (t *HYYCache) ItemCount() int

ItemCount 获取项目数量

func (*HYYCache) OnEvicted

func (t *HYYCache) OnEvicted(f func(string, interface{}))

OnEvicted 设置驱逐回调函数

func (*HYYCache) Replace

func (t *HYYCache) Replace(k string, x interface{}, d time.Duration) error

Replace 替换缓存

func (*HYYCache) Set

func (t *HYYCache) Set(k string, x interface{}, d time.Duration) error

Set 设置缓存

func (*HYYCache) SetDefault

func (t *HYYCache) SetDefault(k string, x interface{}) error

SetDefault 设置默认过期时间的缓存

type Item

type Item struct {
	Object     interface{} `msgpack:"o"` // 存储的值
	Expiration int64       `msgpack:"e"` // 过期时间戳(纳秒单位,0=永不过期)
}

Item 表示缓存项,包含值和过期时间(UnixNano时间戳)

func (Item) Expired

func (item Item) Expired() bool

Expired 检查缓存项是否过期 返回值:true=已过期,false=未过期

type MemoryCache

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

MemoryCache 是缓存系统的客户端接口,封装底层实现细节 提供线程安全的缓存操作接口

func NewMemoryCache

func NewMemoryCache(defaultExpiration, cleanupInterval time.Duration,
	maxItems int, shardCount int, strictTypeCheck bool) *MemoryCache

NewMemoryCache 创建内存缓存实例 参数说明:

defaultExpiration: 默认过期时长
cleanupInterval: 后台清理间隔(0=禁用自动清理)
maxItems: 最大缓存项总数(0=无限制)
shardCount: 分片数量(建议为CPU核心倍数)
strictTypeCheck: 是否启用严格类型检查

返回值:初始化好的缓存实例指针

func (MemoryCache) Add

func (c MemoryCache) Add(k string, x interface{}, d time.Duration) error

Add 仅当键不存在时添加新项目 返回值:错误信息(如键已存在或分片已满)

func (MemoryCache) Close

func (m MemoryCache) Close()

Close 安全关闭缓存实例 执行顺序:1.标记关闭 2.停止任务队列 3.等待任务完成 4.停止清理器

func (MemoryCache) Decrement

func (c MemoryCache) Decrement(k string, n int64) error

Decrement 对数值型缓存项减少整数值

func (MemoryCache) DecrementFloat

func (c MemoryCache) DecrementFloat(k string, n float64) error

DecrementFloat 对浮点型缓存项减少浮点值

func (MemoryCache) Delete

func (c MemoryCache) Delete(k string)

Delete 删除指定键的缓存项 若配置了驱逐回调,会异步触发回调函数

func (MemoryCache) DeleteExpired

func (c MemoryCache) DeleteExpired()

DeleteExpired 删除所有过期缓存项 采用分片级并行处理,避免全局锁阻塞

func (MemoryCache) Flush

func (c MemoryCache) Flush()

Flush 清空所有缓存 重置每个分片的存储映射

func (MemoryCache) Get

func (c MemoryCache) Get(k string, target interface{}) (bool, error)

Get 获取缓存值 参数target必须是指针类型,用于接收解码后的值 返回值:是否存在, 错误信息

func (MemoryCache) GetWithExpiration

func (c MemoryCache) GetWithExpiration(k string, target interface{}) (bool, time.Time)

GetWithExpiration 获取缓存值及其过期时间 返回值:是否存在, 过期时间

func (MemoryCache) HotShards

func (c MemoryCache) HotShards() []int

HotShards 检测热点分片(访问频率过高) 参数threshold为访问计数阈值 返回值:热点分片索引列表

func (MemoryCache) Increment

func (c MemoryCache) Increment(k string, n int64) error

Increment 对数值型缓存项增加整数值 支持int/int8/int16/int32/int64/uint系列/float32/float64

func (MemoryCache) IncrementFloat

func (c MemoryCache) IncrementFloat(k string, n float64) error

IncrementFloat 对浮点型缓存项增加浮点值

func (MemoryCache) ItemCount

func (c MemoryCache) ItemCount() int

ItemCount 获取当前缓存项总数 通过原子计数器累加各分片计数

func (MemoryCache) OnEvicted

func (c MemoryCache) OnEvicted(f func(string, interface{}))

OnEvicted 设置驱逐回调函数 当缓存项被删除(非主动删除)时触发

func (MemoryCache) Replace

func (c MemoryCache) Replace(k string, x interface{}, d time.Duration) error

Replace 仅当键存在时替换项目值 返回值:错误信息(如键不存在)

func (MemoryCache) Set

func (c MemoryCache) Set(k string, x interface{}, d time.Duration) error

Set 存储键值对到缓存 参数说明:

k: 键
x: 值
d: 自定义过期时间(0=使用默认过期时间)

返回值:错误信息(如分片已满)

func (MemoryCache) SetDefault

func (c MemoryCache) SetDefault(k string, x interface{}) error

SetDefault 使用默认过期时间存储键值对

func (MemoryCache) ShardStats

func (c MemoryCache) ShardStats() []int64

ShardStats 获取各分片项目数统计 用于监控分片负载均衡情况

type RedisCache

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

RedisCache 实现高性能分布式Redis缓存,核心特性:

  • 多模式连接:单节点/哨兵/集群/TLS加密
  • 智能压缩:阈值压缩+流式大值处理
  • 分布式锁:自动续约+故障安全释放
  • 生产级健壮性:指数退避重连+资源池管理

func NewRedisCache

func NewRedisCache(options RedisOptions, defaultExpiration time.Duration) (*RedisCache, error)

func NewRedisCacheFromURL

func NewRedisCacheFromURL(redisURL string, defaultExpiration time.Duration) (*RedisCache, error)

NewRedisCacheFromURL 从URL创建Redis缓存

func (*RedisCache) Add

func (c *RedisCache) Add(k string, x interface{}, d time.Duration) (err error)

func (*RedisCache) Close

func (c *RedisCache) Close()

Close 关闭Redis连接

func (*RedisCache) Decrement

func (c *RedisCache) Decrement(k string, n int64) error

func (*RedisCache) DecrementFloat

func (c *RedisCache) DecrementFloat(k string, n float64) error

func (*RedisCache) Delete

func (c *RedisCache) Delete(k string)

func (*RedisCache) DeleteExpired

func (c *RedisCache) DeleteExpired()

func (*RedisCache) Flush

func (c *RedisCache) Flush()

Flush 清空缓存

func (*RedisCache) Get

func (c *RedisCache) Get(k string, target interface{}) (bool, error)

func (*RedisCache) GetWithExpiration

func (c *RedisCache) GetWithExpiration(k string, target interface{}) (bool, time.Time)

func (*RedisCache) Increment

func (c *RedisCache) Increment(k string, n int64) error

func (*RedisCache) IncrementFloat

func (c *RedisCache) IncrementFloat(k string, n float64) error

func (*RedisCache) ItemCount

func (c *RedisCache) ItemCount() int

ItemCount 获取缓存键数量(集群感知) 集群模式:累加所有主节点的SCard结果 单机模式:使用DBSIZE或SCAN

func (*RedisCache) OnConnectionLost

func (c *RedisCache) OnConnectionLost(f func(error))

func (*RedisCache) OnEvicted

func (c *RedisCache) OnEvicted(f func(string, interface{}))

OnEvicted 设置驱逐回调 警告:必须在Set/Get等操作前调用 集群模式要求Redis配置:

CONFIG SET notify-keyspace-events "Ex"

func (*RedisCache) Replace

func (c *RedisCache) Replace(k string, x interface{}, d time.Duration) error

func (*RedisCache) Set

func (c *RedisCache) Set(k string, x interface{}, d time.Duration) error

Set 添加项目到缓存

func (*RedisCache) SetDefault

func (c *RedisCache) SetDefault(k string, x interface{}) error

type RedisOptions

type RedisOptions struct {
	Addresses               []string      // Redis地址列表
	Username                string        // 用户名
	Password                string        // 密码
	DB                      int           // 数据库编号
	MasterName              string        // Sentinel主节点名称
	ConnectTimeout          time.Duration // 连接超时
	ReadTimeout             time.Duration // 读取超时
	WriteTimeout            time.Duration // 写入超时
	MinRetryBackoff         time.Duration // 最小重试间隔
	MaxRetryBackoff         time.Duration // 最大重试间隔
	MaxRetries              int           // 最大重试次数
	PoolSize                int           // 连接池大小
	Compression             bool          // 启用压缩
	CompressionThreshold    int           // 压缩阈值(字节)
	StreamCompressThreshold int           // 新增流式压缩阈值
	Namespace               string        // 缓存命名空间
	IsSharedDB              bool          // 是否共享DB
	UseTLS                  bool          // 使用TLS
	TLSConfig               *tls.Config   // TLS配置
}

func ParseRedisURL

func ParseRedisURL(redisURL string) (RedisOptions, error)

ParseRedisURL 解析Redis连接URL

Jump to

Keyboard shortcuts

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