Documentation
¶
Overview ¶
Package leaderboard 提供排行榜的内存排名缓存:用堆排序维护每个榜的有序结构, O(log N) 查"我的名次"、按名次取记录,避免高频读时每次 ORDER BY 击穿数据库。
设计要点:
- 每个榜 + 过期周期维护一个独立的 RankCache;
- Fill 全量加载、Insert/Delete 增量维护、Get 按主人查名次;
- 黑名单机制:某些超大或写频繁的榜可不进缓存,退化为不缓存(本包表现为 Get 返回 -1)。
适用场景:游戏排行榜、积分榜、热榜的"我的排名/TopN"高频读。
零值不可用,用 New 构造。RankCache 并发安全。
Index ¶
- type RankCache
- func (r *RankCache) Around(leaderboardID string, expiry int64, ownerID string, around int) []Record
- func (r *RankCache) Delete(leaderboardID string, expiry int64, ownerID string) bool
- func (r *RankCache) DeleteLeaderboard(leaderboardID string, expiry int64) bool
- func (r *RankCache) Fill(leaderboardID string, expiry int64, order SortOrder, records []Record, ...) int
- func (r *RankCache) Get(leaderboardID string, expiry int64, ownerID string) int64
- func (r *RankCache) GetByRank(leaderboardID string, expiry int64, rank int) (Record, bool)
- func (r *RankCache) Insert(leaderboardID string, expiry int64, order SortOrder, rec Record, enable bool) int64
- func (r *RankCache) Size(leaderboardID string, expiry int64) int
- func (r *RankCache) TopN(leaderboardID string, expiry int64, n int) []Record
- type Record
- type SortOrder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type RankCache ¶
type RankCache struct {
// contains filtered or unexported fields
}
RankCache 管理多个排行榜的内存排名缓存。
func (*RankCache) DeleteLeaderboard ¶
DeleteLeaderboard 移除整个榜的缓存(过期/重置时调用)。
func (*RankCache) Fill ¶
func (r *RankCache) Fill(leaderboardID string, expiry int64, order SortOrder, records []Record, enable bool) int
Fill 全量加载一个榜的记录,构建缓存。返回缓存的记录数。 若该榜在黑名单中,返回 0 且不缓存。 enable=false 时也跳过(用于运行时动态关闭某榜缓存)。
func (*RankCache) Insert ¶
func (r *RankCache) Insert(leaderboardID string, expiry int64, order SortOrder, rec Record, enable bool) int64
Insert 插入或更新一条记录,增量维护堆。返回该 owner 的当前名次(从 1 起)。 若该榜未缓存,返回 -1。
Click to show internal directories.
Click to hide internal directories.