cacheCountServiceX

package
v2.2.1 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package cacheCountServicex 基于redis缓存计数服务【一套基于redis和本地缓存的计数服务,并且维护了一个榜单的数据,优先命中本地缓存】

Index

Constants

This section is empty.

Variables

View Source
var (
	//go:embed lua/cnt.lua
	LuaCnt string

	//go:embed lua/cntRank.lua
	LuaCntRank string // 缓存计数lua脚本

	//go:embed lua/get_rank.lua
	LuaGetRank string // 获取排名的lua脚本
)

Functions

This section is empty.

Types

type CntServiceIn

type CntServiceIn[K localCahceX.Key, V any] interface {
	SetCnt(ctx context.Context, biz string, bizId int64, num ...int64) *Count[K, V]
	DelCnt(ctx context.Context, biz string, bizId int64) error
	GetCnt(ctx context.Context, biz string, bizId int64) ([]RankItem, error)
	GetCntRank(ctx context.Context, biz string, opt ...GetCntType) ([]RankItem, error)
}

CntServiceIn 抽象计数服务接口【只用计数服务的话,此方法即可】

type Count

type Count[K localCahceX.Key, V any] struct {
	RedisCache redis.Cmdable

	LocalCache localCahceX.CacheLocalIn[string, string]
	// 缓存计数操作,true为增加,false为减少
	CntOpt bool

	// ===========本地缓存参数===========
	// 本地缓存过期时间Expiration、RankCacheExpiration【redis缓存过期时间在lua脚本中调整】
	//	- 缓存过期时间【默认为5分钟】
	Expiration time.Duration
	// 排行榜本地&redis缓存过期时间,默认1分钟,可set自由调整
	RankCacheExpiration time.Duration

	// 缓存计数字段服务名【eg: like_cnt】
	ServiceTypeName string
	// 本地缓存中数据权重【多用于缓存时间未过期,但是分配内存满了,需释放部分】
	Weight int64

	// ===========redis缓存参数===========
	LuaCnt     string // redis缓存计数的lua脚本
	LuaGetRank string // redis获取排名的lua脚本

	// ===========热榜计算开关,默认false===========
	RankCount   bool
	CntTypeConf GetCntType // 获取计数参数, 默认offset=0, Limit=100为获取的条数, 前100条数据为排行榜数据

	Error error
	Rank  []RankItem
	// contains filtered or unexported fields
}

Count 计数服务

func NewCount

func NewCount[K localCahceX.Key, V any](redisCache redis.Cmdable, localCache localCahceX.CacheLocalIn[string, string]) *Count[K, V]

NewCount 创建一个计数服务

  • 计数服务名,ServiceTypeName建议显示set设置,每个业务用不同的

func (*Count[K, V]) DelCnt

func (i *Count[K, V]) DelCnt(ctx context.Context, biz string, bizId int64) error

DelCnt 删除计数, 默认为全部删除【localCache、RedisCache】

func (*Count[K, V]) GetCnt

func (i *Count[K, V]) GetCnt(ctx context.Context, biz string, bizId int64) ([]RankItem, error)

GetCnt 获取单个某一个业务计数

  • biz: 业务名、biz_Id: 业务id

func (*Count[K, V]) GetCntRank

func (i *Count[K, V]) GetCntRank(ctx context.Context, biz string, opt GetCntType) ([]RankItem, error)

GetCntRank - opt: 查询选项,查询排行榜取值范围

func (*Count[K, V]) Key

func (i *Count[K, V]) Key(biz string, bizId int64) string

Key 生成缓存键

func (*Count[K, V]) RankKey

func (i *Count[K, V]) RankKey(biz string) string

RankKey 生成排行榜键

func (*Count[K, V]) ResErr

func (i *Count[K, V]) ResErr() error

ResErr 获取计数服务错误【普通写入直接返回err】,写入时RankCount=true实时计算排行榜数据时使用ResRank

func (*Count[K, V]) ResRank

func (i *Count[K, V]) ResRank() ([]RankItem, error)

ResRank 获取计数服务错误【普通写入直接返回err】,写入时RankCount=true实时计算排行榜数据时使用ResRank

func (*Count[K, V]) SetCnt

func (i *Count[K, V]) SetCnt(ctx context.Context, biz string, bizId int64, num ...int64) *Count[K, V]

SetCnt 操作计数方法,【biz不支持中间 : ,可以用 _ 替代,user_article而非user:article】

  • 【1、SetCnt增加计数时,接收err,2、SetCnt增加计数时,直接接收err和res榜单,榜单每分钟更新】
  • 如果开启了RankCount=true,实时计算热榜数据,调用者需自己维护redis、cache缓存过期后的回写操作 以及 热榜数据的数据库更新
  • biz: 业务名【biz文章article服务 的 biz_id文章1】
  • biz_Id: 业务id
  • num 计数的多少,不存在num参数时,默认每次计数增加/减少1,【存在num, eg: num = 10,则每次增加计数10】
  • 【增加或减少由CntOpt控制,true为增加,false为减少,默认为true】可调用SetCntOpt配置CntOpt增加或减少计数

func (*Count[K, V]) SetCntOpt

func (i *Count[K, V]) SetCntOpt(ctp bool) *Count[K, V]

SetCntOpt : true为增加,false为减少【默认为增加】

func (*Count[K, V]) SetCntTypeConf

func (i *Count[K, V]) SetCntTypeConf(setCntTypeConf GetCntType) *Count[K, V]

SetCntTypeConf : 设置获取排行榜数据时的参数

func (*Count[K, V]) SetExpiration

func (i *Count[K, V]) SetExpiration(expiration time.Duration) *Count[K, V]

SetExpiration : 设置缓存过期时间【默认为5分钟】

func (*Count[K, V]) SetGetLuaGetRank

func (i *Count[K, V]) SetGetLuaGetRank(LuaGetRank string) *Count[K, V]

SetGetLuaGetRank : 设置排行榜的 Lua 脚本, 用于获取排行榜的 Lua 脚本

func (*Count[K, V]) SetLuaCnt

func (i *Count[K, V]) SetLuaCnt(LuaCnt string) *Count[K, V]

SetLuaCnt : 设置Lua脚本,用于增减计数的 Lua 脚本

func (*Count[K, V]) SetRankCacheExpiration

func (i *Count[K, V]) SetRankCacheExpiration(expiration time.Duration) *Count[K, V]

SetRankCacheExpiration 设置排行榜缓存过期时间

func (*Count[K, V]) SetRankCount

func (i *Count[K, V]) SetRankCount(rankCount bool) *Count[K, V]

SetRankCount : 设置是否统计排行榜数据

func (*Count[K, V]) SetServiceTypeName

func (i *Count[K, V]) SetServiceTypeName(ServiceTypeName string) *Count[K, V]

SetServiceTypeName : 设置服务名eg: like_cnt 【默认为count_service】

func (*Count[K, V]) SetWeight

func (i *Count[K, V]) SetWeight(weight int64) *Count[K, V]

SetWeight : 设置本地缓存中数据权重【多用于缓存时间未过期,但是分配内存满了,需释放部分】

type GetCntType

type GetCntType struct {
	Offset int64
	Limit  int64
}

GetCntType GetCnt 获取缓存计数数据

  • 实现类似与可以获取前100的数据效果,那就是offset=1,Limit=100为获取的条数,开区间[]

type RankItem

type RankItem struct {
	// BizID 业务id, 业务的唯一标识。eg: 帖子ID、用户ID或任何你需要排名对象的唯一标识。
	BizID int64 `json:"biz_id"`

	// Score 业务计数、得分值、点赞数等等, 该业务项的得分、点赞数等等。这个值决定了在排行榜中的位置,值越高排名通常越靠前。
	Score int64 `json:"score"`

	// Rank 排名, 该业务项在排行榜中的具体名次。eg: 分数最高的项目 Rank 为 1。
	Rank int64 `json:"rank"`
}

RankItem 排行榜项

  • BizID 业务id, 业务的唯一标识。eg: 帖子ID、用户ID或任何你需要排名对象的唯一标识。
  • Score 业务计数、得分值、点赞数等等, 该业务项的得分。这个值决定了在排行榜中的位置,值越高排名通常越靠前。
  • Rank 排名, 该业务项在排行榜中的具体名次。eg: 分数最高的项目 Rank 为 1。

Jump to

Keyboard shortcuts

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