Documentation
¶
Overview ¶
ZSet 有序集合
基于「跳表 + 字典」双维护实现,适合实时排行榜、Top-N 统计等场景。
核心规则:
- 排序:默认降序(高分在前),可选升序;同分按插入顺序排列(FIFO,先到先得)
- 守门员:设置 maxSize 后,满员时最后一名为守门员,新成员分数必须严格优于守门员才能入榜
- 懒淘汰:超出 maxSize 的冗余数据不立即清理,累积到阈值后批量裁剪,以空间换吞吐
Index ¶
- type ZNode
- type ZSet
- func (z *ZSet) CanEnter(score int64) bool
- func (z *ZSet) ZAdd(score int64, key string) int64
- func (z *ZSet) ZCard() int64
- func (z *ZSet) ZCount(min, max int64) int64
- func (z *ZSet) ZElement(rank int64) (key string, score int64)
- func (z *ZSet) ZIncr(score int64, key string) int64
- func (z *ZSet) ZRange(start, end int64) []ZNode
- func (z *ZSet) ZRangeByScore(min, max int64) []ZNode
- func (z *ZSet) ZRangeByScoreWithCallback(min, max int64, f func(int64, string))
- func (z *ZSet) ZRangeWithCallback(start, end int64, f func(int64, string))
- func (z *ZSet) ZRank(key string) (rank int64, score int64)
- func (z *ZSet) ZRem(key string) bool
- func (z *ZSet) ZRemRangeByRank(start, stop int64) int64
- func (z *ZSet) ZRemRangeByScore(min, max int64) int64
- func (z *ZSet) ZScore(key string) (score int64, ok bool)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ZSet ¶
type ZSet struct {
// contains filtered or unexported fields
}
ZSet 有序集合主结构
func NewWithMaxSize ¶ added in v1.8.0
NewWithMaxSize 创建带人数限制的有序集合
func (*ZSet) CanEnter ¶ added in v1.8.0
CanEnter 无锁预检:判断指定分数是否有可能入榜 使用 atomic 读取守门员分数,不获取任何锁
用途:调用方在业务层做快速过滤,避免无效的 ZAdd 写锁竞争
if z.CanEnter(score) {
z.ZAdd(score, key)
}
注意:存在极小的竞态窗口(守门员刚被更新),可能出现假阳性(返回 true 但实际被拒) 但不会出现假阴性(返回 false 但实际能入),因此是安全的预过滤
func (*ZSet) ZRangeByScore ¶
ZRangeByScore 按分数范围获取元素列表
func (*ZSet) ZRangeByScoreWithCallback ¶ added in v1.8.0
ZRangeByScoreWithCallback 按分数范围遍历元素
func (*ZSet) ZRangeWithCallback ¶ added in v1.8.0
ZRangeWithCallback 按排名范围遍历元素
func (*ZSet) ZRemRangeByRank ¶ added in v1.8.0
ZRemRangeByRank 删除排名在 [start, stop] 范围内的元素(0-based,支持负数索引)
func (*ZSet) ZRemRangeByScore ¶ added in v1.8.0
ZRemRangeByScore 删除分数在 [min, max] 范围内的元素
Click to show internal directories.
Click to hide internal directories.