redis

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2023 License: MIT Imports: 11 Imported by: 1

README

Redis Tools

Some redis utils depend on https://github.com/go-redis/redis

import (
    gredis "github.com/Laisky/go-redis"
    "github.com/go-redis/redis"
)

func main() {
    rtils := NewRedisUtils(redis.NewClient(&redis.Options{}))
}

Features

  • getset.go: common utils of get/set
  • sync.go: distributed locks

Documentation

Index

Examples

Constants

View Source
const (
	// KeyExpImmortal nolimit
	KeyExpImmortal = 0
	// KeyExp1Day 1day
	KeyExp1Day = 24 * time.Hour
	// ScanCount how many items return by each scan
	ScanCount = 10
)

Redis Key expirations

View Source
const (
	// DefaultKeyPrefix default prefix of key in redis
	DefaultKeyPrefix = "/rtils/"
)
View Source
const (
	WaitDBKeyDuration = 1 * time.Second
)

Pop time

Variables

This section is empty.

Functions

func IsNil

func IsNil(err error) bool

IsNil is nil in redis

func SetLogger

func SetLogger(log gutils.LoggerItf)

SetLogger set go-redis logger

Types

type GetItemBlockingOptionFunc

type GetItemBlockingOptionFunc func(*getItemBlockingOption) error

GetItemBlockingOptionFunc optional arguments for GetItemBlocking

type Mutex added in v1.0.3

type Mutex interface {
	// Lock acquire a recursive lock
	//
	// if succeed acquired lock,
	//   * locked == true
	//   * lockCtx is context of lock, this context will be set to done when lock is expired
	Lock(ctx context.Context) (locked bool, lockCtx context.Context, err error)
	// Unlock release lock
	Unlock(ctx context.Context) error
}

mutexType distributed mutex

Redis keys:

`/rtils/sync/mutex/<lock_id>/<client_id>`

Implementations:

  1. generate client id(cid)
  2. set if not exists by `SETNX` with ttl: lock_name -> cid
  3. if succeeded set, auto refresh lock's ttl

type MutexOptionFunc added in v1.0.3

type MutexOptionFunc func(*mutex) error

MutexOptionFunc options for mutex

func WithMutexBlockingLock added in v1.0.3

func WithMutexBlockingLock(blocking bool) MutexOptionFunc

WithMutexBlockingLock set whether blocking lock

func WithMutexClientID added in v1.0.3

func WithMutexClientID(clientID string) MutexOptionFunc

WithMutexClientID set client id

func WithMutexLogger added in v1.0.3

func WithMutexLogger(logger *gutils.LoggerType) MutexOptionFunc

WithMutexLogger set lock's expiration

func WithMutexRefreshInterval added in v1.0.3

func WithMutexRefreshInterval(interval time.Duration) MutexOptionFunc

WithMutexRefreshInterval set lock refreshing interval

func WithMutexSpinInterval added in v1.0.3

func WithMutexSpinInterval(interval time.Duration) MutexOptionFunc

WithMutexSpinInterval set lock spin interval

func WithMutexTTL added in v1.0.3

func WithMutexTTL(ttl time.Duration) MutexOptionFunc

WithMutexTTL set lock's expiration

type Rank added in v1.1.0

type Rank interface {
	// Set set/update someone's score and snapshotID
	Set(ctx context.Context, key string, score, snapshotID int) error
	// Del delete a key
	Del(ctx context.Context, key string) error
	// List get top N scores
	List(ctx context.Context, limit uint) ([]redis.Z, error)
	// Get get someone's score and snapshotID
	Get(ctx context.Context, key string) (snapshotID int, err error)
}

Rank Use the ordered set of redis to implement dynamic ranking.

When a user's score changes, a snapshot is taken of the user's current information and status to get a snapshot ID, and then the user's key, score, and snapshot ID are stored in the ordered set. member is the key, score is the (score * 100000 + snapshot ID).

Example
rdb := redis.NewClient(&redis.Options{})
rtils := NewRedisUtils(rdb)

ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

r, _ := rtils.NewRank("test", 10000)
_ = r.Set(ctx, "user-1", 100, 1)
_ = r.Set(ctx, "user-2", 101, 1)
_ = r.Set(ctx, "user-3", 511, 1)
_ = r.Set(ctx, "user-4", 201, 1)
_ = r.Set(ctx, "user-5", 301, 1)

ret, _ := r.List(ctx, 1)
fmt.Println(ret[0].Member)
Output:

user-3

type RdbIntCmdItf

type RdbIntCmdItf interface {
	Result() (int64, error)
	Err() error
}

RdbIntCmdItf string result

type RdbItf

type RdbItf interface {
	Get(string) RdbStringCmdItf
	LPop(string) RdbStringCmdItf
	LLen(string) RdbIntCmdItf
	LTrim(string, int64, int64) RdbStatusCmdItf
	MGet(...string) RdbSliceCmdItf
	Del(...string) RdbIntCmdItf
	Set(string, interface{}, time.Duration) RdbStatusCmdItf
	Scan(uint64, string, int64) RdbScanCmdItf
	RPush(string, ...interface{}) RdbIntCmdItf
}

RdbItf redis SDK

type RdbScanCmdItf

type RdbScanCmdItf interface {
	Result() ([]string, uint64, error)
	Err() error
}

RdbScanCmdItf string result

type RdbSliceCmdItf

type RdbSliceCmdItf interface {
	Result() ([]interface{}, error)
	Err() error
}

RdbSliceCmdItf string result

type RdbStatusCmdItf

type RdbStatusCmdItf interface {
	Result() (string, error)
	Err() error
}

RdbStatusCmdItf string result

type RdbStringCmdItf

type RdbStringCmdItf interface {
	Result() (string, error)
	Err() error
}

RdbStringCmdItf string result

type Semaphore added in v1.0.3

type Semaphore interface {
	// Lock acquire a recursive lock
	//
	// if succeed acquired lock,
	//   * locked == true
	//   * lockCtx is context of lock, this context will be set to done when lock is expired
	Lock(ctx context.Context) (locked bool, lockCtx context.Context, err error)
	// Unlock release lock
	Unlock(ctx context.Context) (err error)
}

semaphore distributed fair semaphore

Redis keys:

`/rtils/sync/sema/<lock_id>/`

* cids/: client_id -> ts, all clients
* owners/: client_id -> counter, all clients acquired lock
* counter:

you can specified client_id by `WithSemaphoreClientID`. will auto generate client_id by UUID4 if not set.

Implementations:

  1. generate client id(cid)
  2. delete all expired clients in `cids`, by `ZREMRANGEBYSCORE cids -inf <now - ttl>`
  3. delete all expired clients in `owners`, by `ZINTERSTORE owners 2 owners cids WEIGHTS 1 0`
  4. increment semaphore's counter
  5. add cid:counter to `owners`, get rank (smaller is better). 5-1. delete from `owners` if the rank is over the limit of semaphore
  6. add cid:timestamp to `cids`

type SemaphoreOptionFunc added in v1.0.3

type SemaphoreOptionFunc func(*semaphore) error

SemaphoreOptionFunc options for semaphore

func WithSemaphoreBlockingLock added in v1.0.3

func WithSemaphoreBlockingLock(blocking bool) SemaphoreOptionFunc

WithSemaphoreBlockingLock set whether blocking lock

func WithSemaphoreClientID added in v1.0.3

func WithSemaphoreClientID(clientID string) SemaphoreOptionFunc

WithSemaphoreClientID set client id

func WithSemaphoreLogger added in v1.0.3

func WithSemaphoreLogger(logger *gutils.LoggerType) SemaphoreOptionFunc

WithSemaphoreLogger set lock's expiration

func WithSemaphoreRefreshInterval added in v1.0.3

func WithSemaphoreRefreshInterval(interval time.Duration) SemaphoreOptionFunc

WithSemaphoreRefreshInterval set lock refreshing interval

func WithSemaphoreSpinInterval added in v1.0.3

func WithSemaphoreSpinInterval(interval time.Duration) SemaphoreOptionFunc

WithSemaphoreSpinInterval set lock spin interval

func WithSemaphoreTTL added in v1.0.3

func WithSemaphoreTTL(ttl time.Duration) SemaphoreOptionFunc

WithSemaphoreTTL set lock's expiration

type Utils added in v1.0.3

type Utils struct {
	*redis.Client
	// contains filtered or unexported fields
}

Utils utils enhancemant for redis

func NewRedisUtils added in v1.0.3

func NewRedisUtils(rdb *redis.Client) *Utils

NewRedisUtils wrap redis client with utils

func (*Utils) GetItem added in v1.0.3

func (u *Utils) GetItem(ctx context.Context, key string) (string, error)

GetItem get item from redis

func (*Utils) GetItemBlocking added in v1.0.3

func (u *Utils) GetItemBlocking(ctx context.Context, dbkey string, opts ...GetItemBlockingOptionFunc) (data string, err error)

GetItemBlocking get key blocking

will delete key after get in default.

func (*Utils) GetItemWithPrefix added in v1.0.3

func (u *Utils) GetItemWithPrefix(ctx context.Context, keyPrefix string) (map[string]string, error)

GetItemWithPrefix get item with prefix, return `map[key]: val`

func (*Utils) LPopKeysBlocking added in v1.0.3

func (u *Utils) LPopKeysBlocking(ctx context.Context, keys ...string) (key, val string, err error)

LPopKeysBlocking LPop from mutiple keys

func (*Utils) NewMutex added in v1.0.3

func (u *Utils) NewMutex(lockName string, opts ...MutexOptionFunc) (Mutex, error)

NewMutex new mutex

func (*Utils) NewRank added in v1.1.0

func (u *Utils) NewRank(name string, maxSnapshotID int) (Rank, error)

NewRank create a new rank

func (*Utils) NewSemaphore added in v1.0.3

func (u *Utils) NewSemaphore(lockName string, limit int, opts ...SemaphoreOptionFunc) (Semaphore, error)

NewSemaphore new semaphore

func (*Utils) RPush added in v1.0.3

func (u *Utils) RPush(ctx context.Context, key string, payloads ...interface{}) (err error)

RPush rpush keys and truncate its length

default max length is 100

func (*Utils) SetItem added in v1.0.3

func (u *Utils) SetItem(ctx context.Context, key, val string, exp time.Duration) error

SetItem set item

func (*Utils) WithGetItemBlockingDel added in v1.0.3

func (u *Utils) WithGetItemBlockingDel(del bool) GetItemBlockingOptionFunc

WithGetItemBlockingDel delete after get

Jump to

Keyboard shortcuts

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