redis

package
v0.0.0-...-714bc2e Latest Latest
Warning

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

Go to latest
Published: May 23, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrRedisNotReady = errors.New("redis is not ready")

ErrRedisNotReady redis is not ready

View Source
var Key = func(key string) keyword {
	return keyword(key)
}

Key keyword of key

View Source
var KeyPrefix = func(prefix string) func(key string) keyword {
	return func(key string) keyword {
		var builder strings.Builder
		builder.WriteString(prefix)
		builder.WriteString(key)
		return keyword(builder.String())
	}
}

KeyPrefix keyword of prefix

View Source
var KeySuffix = func(suffix string) func(key string) keyword {
	return func(key string) keyword {
		var builder strings.Builder
		builder.WriteString(key)
		builder.WriteString(suffix)
		return keyword(builder.String())
	}
}

KeySuffix keyword of suffix

View Source
var (

	// RandomExpiry 0-24h 随机过期时间
	RandomExpiry = func() time.Duration {
		return time.Duration(rand.Intn(24 * int(time.Hour)))
	}
)

Functions

func Client

func Client() *rkv

Client .

func Close

func Close() error

Close .

func Init

func Init(id string, opts ...func(o *Options)) error

Init init default redis

func NewClient

func NewClient(id string, opts ...func(o *Options)) (*rkv, error)

NewClient .

Types

type BoolOutput

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

BoolOutput .

func (*BoolOutput) Ctx

func (o *BoolOutput) Ctx() context.Context

Ctx .

func (*BoolOutput) Err

func (o *BoolOutput) Err() error

Err .

func (*BoolOutput) Key

func (o *BoolOutput) Key() string

Key .

func (*BoolOutput) Val

func (o *BoolOutput) Val() bool

Val .

type BytesOutput

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

BytesOutput .

func (*BytesOutput) Ctx

func (o *BytesOutput) Ctx() context.Context

Ctx .

func (*BytesOutput) Err

func (o *BytesOutput) Err() error

Err .

func (*BytesOutput) Expiry

func (o *BytesOutput) Expiry() time.Time

Expiry .

func (*BytesOutput) Key

func (o *BytesOutput) Key() string

Key .

func (*BytesOutput) TTL

func (o *BytesOutput) TTL() time.Duration

TTL .

func (*BytesOutput) Unmarshal

func (o *BytesOutput) Unmarshal(v interface{}) error

Unmarshal .

func (*BytesOutput) Val

func (o *BytesOutput) Val() []byte

Val .

type Cmdable

type Cmdable func(o *Options) (redis.Cmdable, func() error)

Cmdable redis.Cmdable

var RedisClient Cmdable = func(o *Options) (redis.Cmdable, func() error) {
	client := redis.NewClient(&redis.Options{
		Addr:         o.Addrs[0],
		Username:     o.Username,
		Password:     o.Password,
		MaxRetries:   o.MaxRetries,
		DialTimeout:  o.Timeout,
		ReadTimeout:  o.Timeout,
		WriteTimeout: o.Timeout,
	})

	return client, client.Close
}

RedisClient redis client

var RedisCluster Cmdable = func(o *Options) (redis.Cmdable, func() error) {
	cluster := redis.NewClusterClient(&redis.ClusterOptions{
		Addrs:        o.Addrs,
		Username:     o.Username,
		Password:     o.Password,
		MaxRetries:   o.MaxRetries,
		DialTimeout:  o.Timeout,
		ReadTimeout:  o.Timeout,
		WriteTimeout: o.Timeout,
	})

	return cluster, cluster.Close
}

RedisCluster redis cluster

type DelOptions

type DelOptions struct {
	Context context.Context
}

DelOptions .

type ExpireOptions

type ExpireOptions struct {
	Context context.Context
	// TTL key 过期时间
	TTL time.Duration
	// Expiry key 过期时间。TTL 和 Expiry 同时设置时,以 Expiry 为准
	Expiry time.Time
}

ExpireOptions .

type GetOptions

type GetOptions struct {
	Context context.Context
	// Marshaler value 编码方式
	Marshaler codec.Marshaler
}

GetOptions .

type Mutex

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

Mutex redis 分布式锁

func (*Mutex) Err

func (m *Mutex) Err() error

Err .

func (*Mutex) Lock

func (m *Mutex) Lock()

Lock .

func (*Mutex) Unlock

func (m *Mutex) Unlock()

Unlock .

type MutexOptions

type MutexOptions struct {
	Context context.Context
	// Interval 尝试获取锁的时间间隔
	Interval time.Duration
	// TTL 加锁后,超时自动删除锁
	TTL time.Duration
}

MutexOptions .

type Options

type Options struct {
	Context  context.Context
	Addrs    []string
	Username string
	Password string
	// Timeout second
	Timeout time.Duration
	// MaxRetries 命令执行失败时的重试次数
	MaxRetries int

	// Cmdable redis redis.Cmdable
	Cmdable Cmdable
}

Options .

type RedisMessage

type RedisMessage struct {
	Data      interface{} `json:"data"`
	CreatedBy string      `json:"created_by"`
	CreatedAt string      `json:"created_at"`
}

RedisMessage .

func JsonWrap

func JsonWrap(val interface{}) *RedisMessage

JsonWrap 将 val 包装成 RedisMessage

type SetOptions

type SetOptions struct {
	Context context.Context
	// TTL key 过期时间
	TTL time.Duration
	// Expiry key 过期时间。TTL 和 Expiry 同时设置时,以 Expiry 为准
	Expiry time.Time
	// Marshaler value 编码方式
	Marshaler codec.Marshaler
}

SetOptions .

type StatusOutput

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

StatusOutput .

func (*StatusOutput) Ctx

func (o *StatusOutput) Ctx() context.Context

Ctx .

func (*StatusOutput) Err

func (o *StatusOutput) Err() error

Err .

func (*StatusOutput) Key

func (o *StatusOutput) Key() string

Key .

Jump to

Keyboard shortcuts

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