Documentation
¶
Index ¶
- Constants
- type Cache
- type HYYCache
- func (t *HYYCache) Add(k string, x interface{}, d time.Duration) error
- func (t *HYYCache) Close()
- func (t *HYYCache) Decrement(k string, n int64) error
- func (t *HYYCache) DecrementFloat(k string, n float64) error
- func (t *HYYCache) Delete(k string)
- func (t *HYYCache) DeleteExpired()
- func (t *HYYCache) Flush()
- func (t *HYYCache) Get(k string, target interface{}) (bool, error)
- func (t *HYYCache) GetWithExpiration(k string, target interface{}) (bool, time.Time)
- func (t *HYYCache) Increment(k string, n int64) error
- func (t *HYYCache) IncrementFloat(k string, n float64) error
- func (t *HYYCache) ItemCount() int
- func (t *HYYCache) OnEvicted(f func(string, interface{}))
- func (t *HYYCache) Replace(k string, x interface{}, d time.Duration) error
- func (t *HYYCache) Set(k string, x interface{}, d time.Duration) error
- func (t *HYYCache) SetDefault(k string, x interface{}) error
- type Item
- type MemoryCache
- func (c MemoryCache) Add(k string, x interface{}, d time.Duration) error
- func (m MemoryCache) Close()
- func (c MemoryCache) Decrement(k string, n int64) error
- func (c MemoryCache) DecrementFloat(k string, n float64) error
- func (c MemoryCache) Delete(k string)
- func (c MemoryCache) DeleteExpired()
- func (c MemoryCache) Flush()
- func (c MemoryCache) Get(k string, target interface{}) (bool, error)
- func (c MemoryCache) GetWithExpiration(k string, target interface{}) (bool, time.Time)
- func (c MemoryCache) HotShards() []int
- func (c MemoryCache) Increment(k string, n int64) error
- func (c MemoryCache) IncrementFloat(k string, n float64) error
- func (c MemoryCache) ItemCount() int
- func (c MemoryCache) OnEvicted(f func(string, interface{}))
- func (c MemoryCache) Replace(k string, x interface{}, d time.Duration) error
- func (c MemoryCache) Set(k string, x interface{}, d time.Duration) error
- func (c MemoryCache) SetDefault(k string, x interface{}) error
- func (c MemoryCache) ShardStats() []int64
- type RedisCache
- func (c *RedisCache) Add(k string, x interface{}, d time.Duration) (err error)
- func (c *RedisCache) Close()
- func (c *RedisCache) Decrement(k string, n int64) error
- func (c *RedisCache) DecrementFloat(k string, n float64) error
- func (c *RedisCache) Delete(k string)
- func (c *RedisCache) DeleteExpired()
- func (c *RedisCache) Flush()
- func (c *RedisCache) Get(k string, target interface{}) (bool, error)
- func (c *RedisCache) GetWithExpiration(k string, target interface{}) (bool, time.Time)
- func (c *RedisCache) Increment(k string, n int64) error
- func (c *RedisCache) IncrementFloat(k string, n float64) error
- func (c *RedisCache) ItemCount() int
- func (c *RedisCache) OnConnectionLost(f func(error))
- func (c *RedisCache) OnEvicted(f func(string, interface{}))
- func (c *RedisCache) Replace(k string, x interface{}, d time.Duration) error
- func (c *RedisCache) Set(k string, x interface{}, d time.Duration) error
- func (c *RedisCache) SetDefault(k string, x interface{}) error
- type RedisOptions
Constants ¶
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 统一缓存接口
type HYYCache ¶
type HYYCache struct {
// contains filtered or unexported fields
}
HYYCache 两级缓存实现
func NewHYYCache ¶
func NewHYYCache(local *MemoryCache, remote *RedisCache) *HYYCache
NewHYYCache 创建两级缓存
func (*HYYCache) DecrementFloat ¶
DecrementFloat 减少浮点数值(适配新的modifyNumber方法)
func (*HYYCache) GetWithExpiration ¶
GetWithExpiration 获取缓存及其过期时间
func (*HYYCache) IncrementFloat ¶
IncrementFloat 增加浮点数值(适配新的modifyNumber方法)
func (*HYYCache) SetDefault ¶
SetDefault 设置默认过期时间的缓存
type Item ¶
type Item struct {
Object interface{} `msgpack:"o"` // 存储的值
Expiration int64 `msgpack:"e"` // 过期时间戳(纳秒单位,0=永不过期)
}
Item 表示缓存项,包含值和过期时间(UnixNano时间戳)
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) Close ¶
func (m MemoryCache) Close()
Close 安全关闭缓存实例 执行顺序:1.标记关闭 2.停止任务队列 3.等待任务完成 4.停止清理器
func (MemoryCache) DecrementFloat ¶
DecrementFloat 对浮点型缓存项减少浮点值
func (MemoryCache) Delete ¶
func (c MemoryCache) Delete(k string)
Delete 删除指定键的缓存项 若配置了驱逐回调,会异步触发回调函数
func (MemoryCache) DeleteExpired ¶
func (c MemoryCache) DeleteExpired()
DeleteExpired 删除所有过期缓存项 采用分片级并行处理,避免全局锁阻塞
func (MemoryCache) GetWithExpiration ¶
GetWithExpiration 获取缓存值及其过期时间 返回值:是否存在, 过期时间
func (MemoryCache) HotShards ¶
func (c MemoryCache) HotShards() []int
HotShards 检测热点分片(访问频率过高) 参数threshold为访问计数阈值 返回值:热点分片索引列表
func (MemoryCache) Increment ¶
Increment 对数值型缓存项增加整数值 支持int/int8/int16/int32/int64/uint系列/float32/float64
func (MemoryCache) IncrementFloat ¶
IncrementFloat 对浮点型缓存项增加浮点值
func (MemoryCache) ItemCount ¶
func (c MemoryCache) ItemCount() int
ItemCount 获取当前缓存项总数 通过原子计数器累加各分片计数
func (MemoryCache) OnEvicted ¶
func (c MemoryCache) OnEvicted(f func(string, interface{}))
OnEvicted 设置驱逐回调函数 当缓存项被删除(非主动删除)时触发
func (MemoryCache) SetDefault ¶
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) 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) GetWithExpiration ¶
func (c *RedisCache) GetWithExpiration(k string, target interface{}) (bool, time.Time)
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 // 缓存命名空间
UseTLS bool // 使用TLS
TLSConfig *tls.Config // TLS配置
}
func ParseRedisURL ¶
func ParseRedisURL(redisURL string) (RedisOptions, error)
ParseRedisURL 解析Redis连接URL