ranker

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2022 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package ranker 排序器类型 ranker可以用于分布式排序操作 即多个worker分别计算更新一部分元素的权重,业务端则只负责读取结果

Index

Constants

This section is empty.

Variables

View Source
var Defaultopt = Options{
	MiddlewareOpts: []optparams.Option[middlewarehelper.Options]{},
}

Defaultopt 默认的可选配置

View Source
var ErrElementNotExist = errors.New("element not exist")

ErrElementNotExist 元素不存在

View Source
var ErrKeyNotExists = errors.New("key not exists")

ErrKeyNotExists key不存在

View Source
var ErrParamNMustBePositive = errors.New("param n must positive")

ErrParamNMustBePositive 参数n必须是大于0

View Source
var ErrRankerror = errors.New("ranker ror")

ErrRankerror 排名不存在

Functions

func Reverse added in v2.0.1

func Reverse() optparams.Option[Rangescopopts]

Reverse 控制排序从是否从大到小排序

func ScopFrom added in v2.0.1

func ScopFrom(from int64) optparams.Option[Rangescopopts]

ScopFrom 排序的下限

func ScopTo added in v2.0.1

func ScopTo(to int64) optparams.Option[Rangescopopts]

ScopTo 排序的上限

func WithAutoRefreshInterval added in v2.0.1

func WithAutoRefreshInterval(autoRefreshInterval string) optparams.Option[Options]

WithAutoRefreshInterval 设置自动刷新过期时间的设置

func WithKey added in v2.0.1

func WithKey(key string) optparams.Option[Options]

WithKey 中间件通用设置,指定使用的键,注意设置后namespace依然有效

func WithMaxTTL added in v2.0.1

func WithMaxTTL(maxTTL time.Duration) optparams.Option[Options]

WithMaxTTL 设置token消减间隔时长,单位s

func WithNamespace added in v2.0.1

func WithNamespace(ns ...string) optparams.Option[Options]

WithNamespace 中间件通用设置,指定锁的命名空间

func WithSpecifiedKey added in v2.0.1

func WithSpecifiedKey(key string) optparams.Option[Options]

WithSpecifiedKey 中间件通用设置,指定使用的键,注意设置key后namespace将失效

func WithTaskCron added in v2.0.1

func WithTaskCron(taskCron *cron.Cron) optparams.Option[Options]

WithTaskCron 设置定时器

Types

type Options added in v2.0.1

type Options struct {
	MiddlewareOpts []optparams.Option[middlewarehelper.Options] //初始化Middleware的配置
}

Options broker的配置

type Rangescopopts added in v2.0.1

type Rangescopopts struct {
	Reverse bool
	From    int64
	To      int64
}

type Ranker

type Ranker struct {
	*middlewarehelper.MiddleWareAbc
	// contains filtered or unexported fields
}

Ranker 排序工具

func New

func New(cli redis.UniversalClient, opts ...optparams.Option[Options]) (*Ranker, error)

New 新建一个排序器 @params k *key.Key redis客户端的键对象

func (*Ranker) Add

func (r *Ranker) Add(ctx context.Context, element *redis.Z) error

Add 增加一个新元素 @params ctx context.Context 上下文信息,用于控制请求的结束 @params elements *redis.Z 要添加的带权重的元素

func (*Ranker) AddM

func (r *Ranker) AddM(ctx context.Context, elements ...*redis.Z) error

AddM 增加若干个新元素 @params ctx context.Context 上下文信息,用于控制请求的结束 @params elements ...*redis.Z 要添加的带权重的元素

func (*Ranker) AddOrUpdate

func (r *Ranker) AddOrUpdate(ctx context.Context, element *redis.Z) error

AddOrUpdate 如果元素存在则更新元素权重,不存在则增加元素 @params ctx context.Context 上下文信息,用于控制请求的结束 @params element *redis.Z 要添加或更新的带权重的元素

func (*Ranker) AddOrUpdateM

func (r *Ranker) AddOrUpdateM(ctx context.Context, elements ...*redis.Z) error

AddOrUpdateM 如果元素存在则更新元素权重,不存在则增加元素 @params ctx context.Context 上下文信息,用于控制请求的结束 @params elements ...*redis.Z 要添加或更新的带权重的元素

func (*Ranker) First

func (r *Ranker) First(ctx context.Context, n int64, opts ...optparams.Option[Rangescopopts]) ([]string, error)

First 获取排名前若干位的元素,reverse为True则为从大到小否则为从小到大 @params ctx context.Context 上下文信息,用于控制请求的结束 @params n int64 前几位 @params opts ...optparams.Option[Rangescopopts] 只有Reverse()会生效

func (*Ranker) GetElementByRank

func (r *Ranker) GetElementByRank(ctx context.Context, rank int64, opts ...optparams.Option[Rangescopopts]) (string, error)

GetElementByRank 获取指定排名的元素,reverse为True则为从大到小否则为从小到大 @params ctx context.Context 上下文信息,用于控制请求的结束 @params rank int64 第几名 @params opts ...optparams.Option[Rangescopopts] 只有Reverse()会生效

func (*Ranker) GetRank

func (r *Ranker) GetRank(ctx context.Context, element string, opts ...optparams.Option[Rangescopopts]) (int64, error)

GetRank 获取指定元素的排名,reverse为True则为从大到小否则为从小到大 @params ctx context.Context 上下文信息,用于控制请求的结束 @params count int64 前几位 @params opts ...optparams.Option[Rangescopopts] 只有Reverse()会生效

func (*Ranker) GetWeight

func (r *Ranker) GetWeight(ctx context.Context, elementkey string) (float64, error)

GetWeight 查看元素的权重 @params ctx context.Context 上下文信息,用于控制请求的结束 @params elementkey string 元素的key

func (*Ranker) Head

func (r *Ranker) Head(ctx context.Context, count int64, opts ...optparams.Option[Rangescopopts]) ([]string, error)

Head first的别名 @params ctx context.Context 上下文信息,用于控制请求的结束 @params count int64 前几位 @params opts ...optparams.Option[Rangescopopts] 只有Reverse()会生效

func (*Ranker) IncrWeight

func (r *Ranker) IncrWeight(ctx context.Context, elementkey string) (float64, error)

IncrWeight 为元素累增1的权重, @params ctx context.Context 上下文信息,用于控制请求的结束 @params elementkey string 元素的key

func (*Ranker) IncrWeightM

func (r *Ranker) IncrWeightM(ctx context.Context, elementkey string, weight float64) (float64, error)

IncrWeightM 为元素累增一定数值的权重 @params ctx context.Context 上下文信息,用于控制请求的结束 @params elementkey string 元素的key @params weight float64 元素的权重

func (*Ranker) Last

func (r *Ranker) Last(ctx context.Context, n int64, opts ...optparams.Option[Rangescopopts]) ([]string, error)

Last 获取排名后若干位的元素,reverse为True则为从大到小否则为从小到大 @params ctx context.Context 上下文信息,用于控制请求的结束 @params n int64 后几位 @params opts ...optparams.Option[Rangescopopts] 只有Reverse()会生效

func (*Ranker) Len

func (r *Ranker) Len(ctx context.Context) (int64, error)

Len 获取排名器的当前长度 @params ctx context.Context 上下文信息,用于控制请求的结束

func (*Ranker) Range

func (r *Ranker) Range(ctx context.Context, opts ...optparams.Option[Rangescopopts]) ([]string, error)

Range 获取排名范围内的元素,默认全量从小到大排序. @params ctx context.Context 上下文信息,用于控制请求的结束 @params opts ...optparams.Option[Rangescopopts]

func (*Ranker) Remove

func (r *Ranker) Remove(ctx context.Context, element interface{}) error

Remove 删除元素 @params ctx context.Context 上下文信息,用于控制请求的结束 @params element interface{} 要删除的元素

func (*Ranker) RemoveM

func (r *Ranker) RemoveM(ctx context.Context, elements ...interface{}) error

RemoveM 删除元素 @params ctx context.Context 上下文信息,用于控制请求的结束 @params elements ...interface{} 要删除的元素

func (*Ranker) Reset

func (r *Ranker) Reset(ctx context.Context) error

Reset 重置排名器 @params ctx context.Context 上下文信息,用于控制请求的结束

func (*Ranker) Tail

func (r *Ranker) Tail(ctx context.Context, n int64, opts ...optparams.Option[Rangescopopts]) ([]string, error)

Tail Last的别名 @params ctx context.Context 上下文信息,用于控制请求的结束 @params n int64 后几位 @params opts ...optparams.Option[Rangescopopts] 只有Reverse()会生效

func (*Ranker) Update

func (r *Ranker) Update(ctx context.Context, element *redis.Z) error

Update 更新一个元素 @params ctx context.Context 上下文信息,用于控制请求的结束 @params elements *redis.Z 要更新的带权重的元素

func (*Ranker) UpdateM

func (r *Ranker) UpdateM(ctx context.Context, elements ...*redis.Z) error

UpdateM 更新元素的权重 @params ctx context.Context 上下文信息,用于控制请求的结束 @params elements ...*redis.Z 要更新的带权重的元素

type RankerInterface added in v2.0.1

type RankerInterface interface {
	//AddM 增加若干个新元素
	//@params ctx context.Context 上下文信息,用于控制请求的结束
	//@params elements ...*redis.Z 要添加的带权重的元素
	AddM(ctx context.Context, elements ...*redis.Z) error

	//Add 增加一个新元素
	//@params ctx context.Context 上下文信息,用于控制请求的结束
	//@params elements *redis.Z 要添加的带权重的元素
	Add(ctx context.Context, element *redis.Z) error
	//UpdateM 更新元素的权重
	//@params ctx context.Context 上下文信息,用于控制请求的结束
	//@params elements ...*redis.Z 要更新的带权重的元素
	UpdateM(ctx context.Context, elements ...*redis.Z) error

	//Update 更新一个元素
	//@params ctx context.Context 上下文信息,用于控制请求的结束
	//@params elements *redis.Z 要更新的带权重的元素
	Update(ctx context.Context, element *redis.Z) error
	//AddOrUpdateM 如果元素存在则更新元素权重,不存在则增加元素
	//@params ctx context.Context 上下文信息,用于控制请求的结束
	//@params elements ...*redis.Z 要添加或更新的带权重的元素
	AddOrUpdateM(ctx context.Context, elements ...*redis.Z) error

	//AddOrUpdate 如果元素存在则更新元素权重,不存在则增加元素
	//@params ctx context.Context 上下文信息,用于控制请求的结束
	//@params element *redis.Z 要添加或更新的带权重的元素
	AddOrUpdate(ctx context.Context, element *redis.Z) error
	//Reset 重置排名器
	//@params ctx context.Context 上下文信息,用于控制请求的结束
	Reset(ctx context.Context) error

	//IncrWeightM 为元素累增一定数值的权重
	//@params ctx context.Context 上下文信息,用于控制请求的结束
	//@params elementkey string 元素的key
	//@params weight float64 元素的权重
	IncrWeightM(ctx context.Context, elementkey string, weight float64) (float64, error)
	//IncrWeight 为元素累增1的权重,
	//@params ctx context.Context 上下文信息,用于控制请求的结束
	//@params elementkey string 元素的key
	IncrWeight(ctx context.Context, elementkey string) (float64, error)
	//GetWeight 查看元素的权重
	//@params ctx context.Context 上下文信息,用于控制请求的结束
	//@params elementkey string 元素的key
	GetWeight(ctx context.Context, elementkey string) (float64, error)
	//RemoveM 删除元素
	//@params ctx context.Context 上下文信息,用于控制请求的结束
	//@params elements ...interface{} 要删除的元素
	RemoveM(ctx context.Context, elements ...interface{}) error

	//Remove 删除元素
	//@params ctx context.Context 上下文信息,用于控制请求的结束
	//@params element interface{} 要删除的元素
	Remove(ctx context.Context, element interface{}) error

	// Len 获取排名器的当前长度
	//@params ctx context.Context 上下文信息,用于控制请求的结束
	Len(ctx context.Context) (int64, error)

	//Range 获取排名范围内的元素,默认全量从小到大排序.
	//@params ctx context.Context 上下文信息,用于控制请求的结束
	//@params opts ...optparams.Option[Rangescopopts]
	Range(ctx context.Context, opts ...optparams.Option[Rangescopopts]) ([]string, error)

	//First 获取排名前若干位的元素,reverse为True则为从大到小否则为从小到大
	//@params ctx context.Context 上下文信息,用于控制请求的结束
	//@params n int64 前几位
	//@params opts ...optparams.Option[Rangescopopts] 只有Reverse()会生效
	First(ctx context.Context, n int64, opts ...optparams.Option[Rangescopopts]) ([]string, error)
	//Head first的别名
	//@params ctx context.Context 上下文信息,用于控制请求的结束
	//@params count int64 前几位
	//@params opts ...optparams.Option[Rangescopopts] 只有Reverse()会生效
	Head(ctx context.Context, count int64, opts ...optparams.Option[Rangescopopts]) ([]string, error)
	//Last 获取排名后若干位的元素,reverse为True则为从大到小否则为从小到大
	//@params ctx context.Context 上下文信息,用于控制请求的结束
	//@params n int64 后几位
	//@params opts ...optparams.Option[Rangescopopts] 只有Reverse()会生效
	Last(ctx context.Context, n int64, opts ...optparams.Option[Rangescopopts]) ([]string, error)

	//Tail Last的别名
	//@params ctx context.Context 上下文信息,用于控制请求的结束
	//@params n int64 后几位
	//@params opts ...optparams.Option[Rangescopopts] 只有Reverse()会生效
	Tail(ctx context.Context, n int64, opts ...optparams.Option[Rangescopopts]) ([]string, error)
	//GetRank 获取指定元素的排名,reverse为True则为从大到小否则为从小到大
	//@params ctx context.Context 上下文信息,用于控制请求的结束
	//@params count int64 前几位
	//@params opts ...optparams.Option[Rangescopopts] 只有Reverse()会生效
	GetRank(ctx context.Context, element string, opts ...optparams.Option[Rangescopopts]) (int64, error)
	//GetElementByRank 获取指定排名的元素,reverse为True则为从大到小否则为从小到大
	//@params ctx context.Context 上下文信息,用于控制请求的结束
	//@params rank int64 第几名
	//@params opts ...optparams.Option[Rangescopopts] 只有Reverse()会生效
	GetElementByRank(ctx context.Context, rank int64, opts ...optparams.Option[Rangescopopts]) (string, error)
}

Jump to

Keyboard shortcuts

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