bloomfilterhelper

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: 10 Imported by: 0

Documentation

Overview

redisbloomhelper redis扩展[RedisBloom](https://github.com/RedisBloom/RedisBloom)帮助模块,用于处理布隆过滤器

redisbloomhelper redis扩展[RedisBloom](https://github.com/RedisBloom/RedisBloom)帮助模块,用于处理布隆过滤器

Index

Constants

This section is empty.

Variables

View Source
var Defaultopt = Options{
	Middlewaretype: "redis-ext-bloomfliter",
	MiddlewareOpts: []optparams.Option[middlewarehelper.Options]{},
}

Functions

func AddItemPipe

func AddItemPipe(pipe redis.Pipeliner, ctx context.Context, key string, item string, opts ...optparams.Option[AddOpts]) (*redisbloomhelper.BoolValPlacehold, error)

AddItemPipe 在pipeline中设置布隆过器中的物品 @params key string 使用的key @params item string 被设置的物品 @params opts ...optparams.Option[AddOpts] 设置add行为的附加参数 @returns *BoolValPlacehold 结果的占位符

func AddWithRefreshTTL

func AddWithRefreshTTL() optparams.Option[AddOpts]

AddWithhRefreshTTL 设置总是刷新,对pipeline无效

func AddWithRefreshTTLAtFirstTime

func AddWithRefreshTTLAtFirstTime() optparams.Option[AddOpts]

AddWithRefreshTTLAtFirstTime 设置第一次创建key时使用MaxTTL设置过期,对pipeline无效

func AddWithTTL

func AddWithTTL(t time.Duration) optparams.Option[AddOpts]

AddWithTTL 设置总是使用指定的ttl刷新key

func AddWithTTLAtFirstTime

func AddWithTTLAtFirstTime(t time.Duration) optparams.Option[AddOpts]

AddWithTTLAtFirstTime 设置第一次创建key时使用指定的ttl设置过期,对pipeline无效

func ExistsItemPipe

func ExistsItemPipe(pipe redis.Pipeliner, ctx context.Context, key string, item string) *redisbloomhelper.BoolValPlacehold

ExistsItemPipe 在pipeline中设置布隆过器中检查是否已经存在 @params key string 使用的key @params item string @return bool 物品是否已经存在

func InsertPipe

func InsertPipe(pipe redis.Pipeliner, ctx context.Context, key string, items []string, opts ...optparams.Option[InsertOpts]) (*redisbloomhelper.BoolMapPlacehold, error)

InsertPipe Pipeline中向布隆过滤器中插入数据,如果不存在就创建 @params key string 使用的key @params items []string 待插入数据 @params opts ...optparams.Option[InsertOpts] 可选设置项

func InsertWithCapacity

func InsertWithCapacity(capacity int64) optparams.Option[InsertOpts]

InsertWithCapacity 容量,预估物品的数量.当过滤器已经存在时用于创建过滤器.容量越大检索效率越低,但如果超出容量则会默认使用子过滤器扩容,这对检索效率的影响更大.当`NOCREATE`存在时这个设置将失效

func InsertWithErrorRate

func InsertWithErrorRate(e float64) optparams.Option[InsertOpts]

InsertWithErrorRate 碰撞率,当过滤器已经存在时用于创建过滤器.碰撞率设置的越低使用的hash函数越多,使用的空间也越大,检索效率也越低.当`NOCREATE`存在时这个设置将失效.

func InsertWithExpansion

func InsertWithExpansion(expansion int64) optparams.Option[InsertOpts]

InsertWithExpansion 当达到容量时进行扩容. bloomfilter会创建一个额外的子过滤器,新子过滤器的大小是最后一个子过滤器的大小乘以扩展设置的值. 如果要存储在过滤器中的元素数量未知我们建议您使用`2`或更多的扩展来减少子过滤器的数量; 否则我们建议您使用1的扩展来减少内存消耗.默认扩展值为 2 cuckoofilter则会扩容到2^n

func InsertWithNoCreate

func InsertWithNoCreate() optparams.Option[InsertOpts]

InsertWithNoCreate 如果过滤器不存在则不创建它.这可以用于过滤器创建和过滤器添加之间需要严格分离的地方.`NOCREATE`的优先级高于`CAPACITY`和`ERROR`

func InsertWithNonScaling

func InsertWithNonScaling() optparams.Option[InsertOpts]

InsertWithNonScaling 如果达到初始容量,防止过滤器创建额外的子过滤器.非缩放过滤器比缩放过滤器需要的内存略少,达到容量时过滤器返回错误.

func InsertWithRefreshTTL

func InsertWithRefreshTTL() optparams.Option[InsertOpts]

InsertWithhRefreshTTL 设置总是刷新

func InsertWithRefreshTTLAtFirstTime

func InsertWithRefreshTTLAtFirstTime() optparams.Option[InsertOpts]

InsertWithRefreshTTLAtFirstTime 设置第一次创建key时使用MaxTTL设置过期

func InsertWithTTL

func InsertWithTTL(t time.Duration) optparams.Option[InsertOpts]

InsertWithTTL 设置总是使用指定的ttl刷新key

func InsertWithTTLAtFirstTime

func InsertWithTTLAtFirstTime(t time.Duration) optparams.Option[InsertOpts]

InsertWithTTLAtFirstTime 设置第一次创建key时使用指定的ttl设置过期

func MAddItemPipe

func MAddItemPipe(pipe redis.Pipeliner, ctx context.Context, key string, items []string, opts ...optparams.Option[AddOpts]) (*redisbloomhelper.BoolMapPlacehold, error)

MAddItemPipe 在pipeline中设置布隆过器的多个物品 @params key string 使用的key @params items []string @params opts ...optparams.Option[AddOpts] 设置add行为的附加参数 @returns map[string]bool 设置的物品是否在设置前已经存在

func MExistsItemPipe

func MExistsItemPipe(pipe redis.Pipeliner, ctx context.Context, key string, items ...string) (*redisbloomhelper.BoolMapPlacehold, error)

MExistsItemPipe Pipeline中布隆过器中检查复数物品是否已经存在 @params key string 使用的key @params item string @return map[string]bool 检查的物品是否已经存在

func ReservePipe

func ReservePipe(pipe redis.Pipeliner, ctx context.Context, key string, capacity int64, error_rate float64, opts ...optparams.Option[ReserveOpts]) *redis.Cmd

ReservePipe Pipeline中创建一个布隆过滤器,如果有设置maxttl,则会同时为其设置一个过期 @params key string 使用的key @params capacity int64 容量,预估物品的数量,容量越大检索效率越低,但如果超出容量则会默认使用子过滤器扩容,这对检索效率的影响更大 @params error_rate float64 碰撞率,碰撞率设置的越低使用的hash函数越多,使用的空间也越大,检索效率也越低 @params opts ...optparams.Option[ReserveOpts] 可选设置项

func ReserveWithExpansion

func ReserveWithExpansion(expansion int64) optparams.Option[ReserveOpts]

ReserveWithExpansion 当达到容量时进行扩容. bloomfilter会创建一个额外的子过滤器,新子过滤器的大小是最后一个子过滤器的大小乘以扩展设置的值. 如果要存储在过滤器中的元素数量未知我们建议您使用`2`或更多的扩展来减少子过滤器的数量; 否则我们建议您使用1的扩展来减少内存消耗.默认扩展值为 2

func ReserveWithNonScaling

func ReserveWithNonScaling() optparams.Option[ReserveOpts]

ReserveWithNonScaling 如果达到初始容量,防止过滤器创建额外的子过滤器.非缩放过滤器比缩放过滤器需要的内存略少,达到容量时过滤器返回错误.

func ReserveWithRefreshTTL

func ReserveWithRefreshTTL() optparams.Option[ReserveOpts]

ReserveWithhRefreshTTL 设置使用maxttl设置key的过期,pipeline无效

func ReserveWithTTL

func ReserveWithTTL(t time.Duration) optparams.Option[ReserveOpts]

ReserveWithTTL 设置使用指定的ttl设置key的过期

func WithAutoRefreshInterval

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

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

func WithKey

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

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

func WithMaxTTL

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

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

func WithMiddlewaretype

func WithMiddlewaretype(typename string) optparams.Option[Options]

WithMiddlewaretype 设置中间件类型

func WithNamespace

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

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

func WithSpecifiedKey

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

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

func WithTaskCron

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

WithTaskCron 设置定时器

Types

type AddOpts

type AddOpts struct {
	RefreshOpts []optparams.Option[middlewarehelper.RefreshOpt]
}

AddOpt filter添加元素物品的参数

type BloomFilter

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

BloomFilter 布隆过滤器对象 布隆过滤器常用于去重场景,原理是使用多个hash函数将内容散列至对应的较短的bitmap中,检查时只要所有bitmap中对应位置都没有它即认为未出现,反之则标明已经出现过 布隆过滤器有碰撞概率,即并不能保证一定没有重复,而是根据设置的hash函数数量和bitmap长短可以控制碰撞概率的高低.因此也只适合用于并不严格的去重场景,但它可以大大的节省空间. 而多数现实场景下,相比较起实打实的花钱买空间尤其是内存空间来说,一个较小的碰撞率往往是可以接受的

func New

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

New 创建一个新的令牌桶对象 @params cli redis.UniversalClient @params opts ...optparams.Option[Options] 设置对象的可选参数

func (*BloomFilter) AddItem

func (c *BloomFilter) AddItem(ctx context.Context, item string, opts ...optparams.Option[AddOpts]) (bool, error)

AddItem 布隆过器中设置物品 @params item string 被设置的物品 @params opts ...optparams.Option[AddOpts] 设置add行为的附加参数 @returns bool 设置的物品是否已经存在

func (*BloomFilter) Clean

func (c *BloomFilter) Clean(ctx context.Context) error

Clean 清除bloomfilter的key

func (*BloomFilter) ExistsItem

func (c *BloomFilter) ExistsItem(ctx context.Context, item string) (bool, error)

ExistsItem 布隆过器中检查是否已经存在 @params item string @return bool 物品是否已经存在

func (*BloomFilter) Info

func (c *BloomFilter) Info(ctx context.Context) (*BloomFilterInfo, error)

Info 查看指定bloomfilter的状态

func (*BloomFilter) Insert

func (c *BloomFilter) Insert(ctx context.Context, items []string, opts ...optparams.Option[InsertOpts]) (map[string]bool, error)

Insert 向布隆过滤器中插入数据,如果不存在就创建 @params items []string 待插入数据 @params opts ...optparams.Option[InsertOpts] 可选设置项

func (*BloomFilter) MAddItem

func (c *BloomFilter) MAddItem(ctx context.Context, items []string, opts ...optparams.Option[AddOpts]) (map[string]bool, error)

MAddItem 布隆过器中设置多个物品 @params items []string @params opts ...optparams.Option[AddOpts] 设置add行为的附加参数 @returns map[string]bool 设置的物品是否在设置前已经存在

func (*BloomFilter) MExistsItem

func (c *BloomFilter) MExistsItem(ctx context.Context, items ...string) (map[string]bool, error)

MExistsItem 布隆过器中检查复数物品是否已经存在 @params items ...string 待检查物品 @return map[string]bool 检查的物品是否已经存在

func (*BloomFilter) Reserve

func (c *BloomFilter) Reserve(ctx context.Context, capacity int64, error_rate float64, opts ...optparams.Option[ReserveOpts]) error

Reserve 创建一个布隆过滤器,如果有设置maxttl,则会同时为其设置一个过期 @params capacity int64 容量,预估物品的数量,容量越大检索效率越低,但如果超出容量则会默认使用子过滤器扩容,这对检索效率的影响更大 @params error_rate float64 碰撞率,碰撞率设置的越低使用的hash函数越多,使用的空间也越大,检索效率也越低 @params opts ...optparams.Option[ReserveOpts] 可选设置项

type BloomFilterInfo

type BloomFilterInfo struct {
	Capacity              int64 `json:"Capacity"`
	Size                  int64 `json:"Size"`
	NumberOfFilters       int64 `json:"NumberOfFilters"`
	NumberOfItemsInserted int64 `json:"NumberOfItemsInserted"`
	ExpansionRate         int64 `json:"ExpansionRate"`
}

BloomFilterInfo 布隆过滤器状态信息

type InfoPlacehold

type InfoPlacehold struct {
	Ck  string
	Cmd *redis.Cmd
}

func InfoPipe

func InfoPipe(pipe redis.Pipeliner, ctx context.Context, key string) *InfoPlacehold

InfoPipe Pipeline中查看指定bloomfilter的状态 @params key string 使用的key

func (*InfoPlacehold) Result

func (r *InfoPlacehold) Result() (*BloomFilterInfo, error)

type InsertOpts

type InsertOpts struct {
	NoCreate    bool
	Capacity    int64
	ErrorRate   float64
	NonScaling  bool
	Expansion   int64
	RefreshOpts []optparams.Option[middlewarehelper.RefreshOpt]
}

type Options

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

Options broker的配置

type ReserveOpts

type ReserveOpts struct {
	NonScaling  bool
	Expansion   int64
	RefreshOpts []optparams.Option[middlewarehelper.RefreshOpt]
}

Jump to

Keyboard shortcuts

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