middlewarehelper

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

Documentation

Overview

middlewarehelper 用于规范中间件的模块 中间件指的是一些有特殊业务用途的模块, 这个子模块是整个redishelper的一个基本组件

Index

Constants

This section is empty.

Variables

View Source
var DefaultOptions = Options{
	Namespace: namespace.NameSpcae{"redishelper"},
}
View Source
var ErrAutoRefreshTaskHasBeenSet = errors.New("autoRefresh task has been set")

ErrAutoRefreshTaskHasBeenSet 已经启动了自动刷新任务

View Source
var ErrAutoRefreshTaskInterval = errors.New("autoRefresh task interval not set")

ErrAutoRefreshTaskInterval 未设置自动刷新任务的间隔

View Source
var ErrAutoRefreshTaskNotSetYet = errors.New("autoRefresh task not set yet")

ErrAutoRefreshTaskNotSetYet 自动刷新任务未启动

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

ErrKeyNotExists key不存在

View Source
var ErrKeyNotSetExpire = errors.New("key not set expire")

ErrKeyNotSetExpire key没有设置过期

View Source
var ErrKeyNotSetMaxTLL = errors.New("key not set MaxTLL ")

ErrKeyNotSetMaxTLL key没有设置最大tll

Functions

func DoCmdWithTTL added in v2.1.0

func DoCmdWithTTL(pipe redis.Pipeliner, ctx context.Context, cmd, key string, exp time.Duration, params ...interface{}) *redis.Cmd

func Force

func Force() optparams.Option[ForceOpt]

Force 设置force

func RefreshTTL added in v2.1.0

func RefreshTTL() optparams.Option[RefreshOpt]

RefreshTTL 设置总是刷新

func RefreshTTLAtFirstTime added in v2.1.0

func RefreshTTLAtFirstTime() optparams.Option[RefreshOpt]

RefreshTTLAtFirstTime 设置使用MaxTTL在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 WithNamespace

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

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

func WithSpecifiedKey

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

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

func WithTTL added in v2.1.0

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

func WithTTLAtFirstTime added in v2.1.0

func WithTTLAtFirstTime(t time.Duration) optparams.Option[RefreshOpt]

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

func WithTaskCron

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

WithTaskCron 设置定时器

Types

type ForceOpt

type ForceOpt struct {
	Force bool
}

type MiddleWareAbc

type MiddleWareAbc struct {
	// contains filtered or unexported fields
}

func New

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

func (*MiddleWareAbc) AutoRefresh

func (l *MiddleWareAbc) AutoRefresh() error

AutoRefresh 自动刷新key的过期时间

func (*MiddleWareAbc) Client

func (l *MiddleWareAbc) Client() redis.UniversalClient

Client 获取redis客户端

func (*MiddleWareAbc) Delete

func (l *MiddleWareAbc) Delete(ctx context.Context) error

Delete 删除key @params ctx context.Context 上下文信息,用于控制请求的结束

func (*MiddleWareAbc) DoCmdWithTTL added in v2.1.0

func (l *MiddleWareAbc) DoCmdWithTTL(ctx context.Context, cmd, key string, exp time.Duration, params ...interface{}) (interface{}, error)

func (*MiddleWareAbc) Exists

func (l *MiddleWareAbc) Exists(ctx context.Context) (bool, error)

Exists 查看key是否存在 @params ctx context.Context 上下文信息,用于控制请求的结束

func (*MiddleWareAbc) Key

func (l *MiddleWareAbc) Key() string

Key 查看组件使用的键

func (*MiddleWareAbc) Logger

func (l *MiddleWareAbc) Logger() *log.Log

func (*MiddleWareAbc) MaxTTL

func (l *MiddleWareAbc) MaxTTL() time.Duration

MaxTTL 查看组件设置的最大过期时间

func (*MiddleWareAbc) MiddlewareType

func (l *MiddleWareAbc) MiddlewareType() string

MiddlewareType 查看中间件类型

func (*MiddleWareAbc) RefreshTTL

func (l *MiddleWareAbc) RefreshTTL(ctx context.Context) error

RefreshTTL 刷新key的生存时间 @params ctx context.Context 上下文信息,用于控制请求的结束

func (*MiddleWareAbc) StopAutoRefresh

func (l *MiddleWareAbc) StopAutoRefresh(opts ...optparams.Option[ForceOpt]) error

StopAutoRefresh 取消自动更新缓存 @params force bool 强制停下整个定时任务cron对象

func (*MiddleWareAbc) TTL

TTL 查看key的剩余时间 @params ctx context.Context 上下文信息,用于控制请求的结束

func (*MiddleWareAbc) Type

func (l *MiddleWareAbc) Type(ctx context.Context) (string, error)

Type 查看key的类型 @params ctx context.Context 上下文信息,用于控制请求的结束

type MiddlewareInterface

type MiddlewareInterface interface {
	//Cli 获取中间件使用的redis客户端
	Cli() redis.UniversalClient
	//Key 查看中间件使用的redis键
	Key() string
	//MiddlewareType 查看中间件类型,有lock,limiter,cache,counter,set,disctinct_counter,producer,consumer
	MiddlewareType() string
	//Exists 查看key是否存在
	//@params ctx context.Context 上下文信息,用于控制请求的结束
	Exists(ctx context.Context) (bool, error)
	//Type 查看key的类型
	//@params ctx context.Context 上下文信息,用于控制请求的结束
	Type(ctx context.Context) (string, error)
	//MaxTTL 查看组件设置的最大过期时间
	MaxTTL() time.Duration
	//Logger 获取中间件对象的专用log
	Logger() *log.Log
	//TTL 查看key的剩余时间
	//@params ctx context.Context 上下文信息,用于控制请求的结束
	TTL(ctx context.Context) (time.Duration, error)
	//写操作
	//Delete 删除key
	//@params ctx context.Context 上下文信息,用于控制请求的结束
	Delete(ctx context.Context) error
	//RefreshTTL 刷新key的生存时间
	//@params ctx context.Context 上下文信息,用于控制请求的结束
	RefreshTTL(ctx context.Context) error
	//AutoRefresh 自动刷新key的过期时间
	AutoRefresh() error
	//StopAutoRefresh 取消自动更新缓存
	//@params opts ...optparams.Option[ForceOpt] 使用参数Force()强制停下整个定时任务cron对象
	StopAutoRefresh(opts ...optparams.Option[ForceOpt]) error

	//DoCmdWithTTL 单条命令改为pipeline执行后添加ttl
	//@params ctx context.Context 上下文信息,用于控制请求的结束
	//@params cmd []interface{} 待执行命令
	//@params exp time.Duration ttl超时
	//@returns interface{} 命令执行好后调用`.Result()`返回的值
	DoCmdWithTTL(ctx context.Context, cmd []interface{}, exp time.Duration) (interface{}, error)
}

MiddlewareInterface 一个对象满足该接口表示它是一个redis构造的中间件

type Options

type Options struct {
	Key                 string              //锁使用的key,如果为空则随机创建
	OnlyKey             bool                //只设置key不设置namespace
	Namespace           namespace.NameSpcae //指定命名空间
	MaxTTL              time.Duration
	AutoRefreshInterval string
	TaskCron            *cron.Cron
}

type RefreshOpt added in v2.1.0

type RefreshOpt struct {
	RefreshTTL RefreshTTLType
	TTL        time.Duration
}

RefreshOpt 是否刷新ttl的配置项

type RefreshTTLType added in v2.1.0

type RefreshTTLType int8
const (
	RefreshTTLType_NoRefresh RefreshTTLType = iota
	RefreshTTLType_FirstTimeRefresh
	RefreshTTLType_Refresh
)

Jump to

Keyboard shortcuts

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