redisx

package
v0.0.0-...-4f7920e Latest Latest
Warning

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

Go to latest
Published: May 16, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Example

go test -run Example

package main

import (
	"fmt"
	"time"

	"github.com/xcltapestry/golibs/driver/redisx"
)

func connRedis() (*redisx.Client, error) {
	rdb := redisx.NewClient(&redisx.Options{
		Addr:         ":6379",
		DialTimeout:  10 * time.Second,
		ReadTimeout:  30 * time.Second,
		WriteTimeout: 30 * time.Second,
		PoolSize:     10,
		PoolTimeout:  30 * time.Second,
	})
	err := rdb.Ping()
	if err != nil {
		return nil, err
	}
	return rdb, nil
}

func main() {

	rdb, err := connRedis()
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	defer rdb.Close()

	// --
	v2, err2 := rdb.Set("key3", "aaaa", 1*time.Second)
	fmt.Println("[cmd-Set] v:", v2, " err:", err2)

	v3, err3 := rdb.SetEX("key3-ex", "aaaa", 1*time.Second)
	fmt.Println("[cmd-SetEX] v:", v3, " err:", err3)

	v4, err4 := rdb.SetNX("key3-nx", "aaaa", 1*time.Second)
	fmt.Println("[cmd-SetNX] v:", v4, " err:", err4)

	// --
	ttlv5, ttlerr5 := rdb.TTL("key3-nx")
	fmt.Println("[cmd-Expire-TTL-1] ttlv5:", ttlv5, " ttlerr5:", ttlerr5)

	v5, err5 := rdb.Expire("key3-nx", 5*time.Second)
	fmt.Println("[cmd-Expire] v:", v5, " err:", err5)

	ttlv5, ttlerr5 = rdb.TTL("key3-nx")
	fmt.Println("[cmd-Expire-TTL-2] ttlv5:", ttlv5, " ttlerr5:", ttlerr5)

	// --
	v, err := rdb.Exists("key2")
	fmt.Println("[cmd-Exists3] v:", v, " err:", err)

	v, err = rdb.Exists("key2", "key3")
	fmt.Println("[cmd-Exists1] v:", v, " err:", err)

	v, err = rdb.Exists("key3")
	fmt.Println("[cmd-Exists2] v:", v, " err:", err)

	kval, kerr := rdb.Get("key3")
	fmt.Println("[cmd-Get] kval:", kval, " err:", kerr)

	ttlv, ttlerr := rdb.TTL("key3")
	fmt.Println("[cmd-TTL] ttlv:", ttlv, " ttlerr:", ttlerr)

	// --
	crv, crerr := rdb.Incr("keyCR")
	fmt.Println("[cmd-Incr] crv:", crv, " crerr:", crerr)

	crv, crerr = rdb.Decr("keyCR")
	fmt.Println("[cmd-Decr] crv:", crv, " crerr:", crerr)

	crv, crerr = rdb.Incr("keyCR")
	fmt.Println("[cmd-Incr] crv:", crv, " crerr:", crerr)

}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

func NewClient

func NewClient(opt *Options) *Client

func (*Client) Append

func (rc *Client) Append(key, value string) (int64, error)

func (*Client) Close

func (rc *Client) Close() error

Close -

func (*Client) Decr

func (rc *Client) Decr(key string) (int64, error)

func (*Client) Del

func (rc *Client) Del(keys ...string) (int64, error)

Del -

func (*Client) Exists

func (rc *Client) Exists(keys ...string) (int64, error)

func (*Client) Expire

func (rc *Client) Expire(key string, expiration time.Duration) (bool, error)

func (*Client) Get

func (rc *Client) Get(key string) (string, error)

func (*Client) Incr

func (rc *Client) Incr(key string) (int64, error)

func (*Client) LRange

func (rc *Client) LRange(key string, start, stop int64) ([]string, error)

func (*Client) NewLocker

func (rc *Client) NewLocker() *Lock

NewLocker : 从redis连接创建一个分布式锁

func (*Client) PSubscribe

func (rc *Client) PSubscribe(channel string, message interface{}) (*redis.PubSub, error)

func (*Client) Ping

func (rc *Client) Ping() error

Ping -

func (*Client) Publish

func (rc *Client) Publish(channel string, message interface{}) (int64, error)

func (*Client) Set

func (rc *Client) Set(key string, value interface{}, expiration time.Duration) (string, error)

func (*Client) SetEX

func (rc *Client) SetEX(key string, value interface{}, expiration time.Duration) (string, error)

func (*Client) SetNX

func (rc *Client) SetNX(key string, value interface{}, expiration time.Duration) (bool, error)

func (*Client) TTL

func (rc *Client) TTL(key string) (time.Duration, error)

type Lock

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

Lock 分布式锁

func (*Lock) ExponentialBackoff

func (lk *Lock) ExponentialBackoff(min, max time.Duration)

func (*Lock) Key

func (lk *Lock) Key() string

//////////////////////////////////////////

Lock 相关函数

//////////////////////////////////////////

func (*Lock) LimitRetry

func (lk *Lock) LimitRetry(backoff time.Duration, max int)

func (*Lock) LinearBackoff

func (lk *Lock) LinearBackoff(backoff time.Duration)

func (*Lock) Metadata

func (lk *Lock) Metadata() string

func (*Lock) Obtain

func (lk *Lock) Obtain(ctx context.Context, key string, ttl time.Duration) error

func (*Lock) Refresh

func (lk *Lock) Refresh(ctx context.Context, ttl time.Duration) error

func (*Lock) Release

func (lk *Lock) Release(ctx context.Context) error

func (*Lock) TTL

func (lk *Lock) TTL(ctx context.Context) (time.Duration, error)

func (*Lock) Token

func (lk *Lock) Token() string

type Options

type Options struct {
	Network      string
	Addr         string
	Username     string
	Password     string
	DB           int
	DialTimeout  time.Duration
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
	PoolSize     int
	MinIdleConns int
	MaxConnAge   time.Duration
	PoolTimeout  time.Duration
	IdleTimeout  time.Duration
}

type Redis

type Redis interface {
	Close() error

	Ping() (int64, error)
	Exists(keys ...string) (int64, error)
	Append(key, value string) (int64, error)
	Set(key string, value interface{}, expiration time.Duration) (string, error)
	SetEX(key string, value interface{}, expiration time.Duration) (string, error)
	SetNX(key string, value interface{}, expiration time.Duration) (bool, error)

	Get(key string) (string, error)
	Del(key string) (int64, error)
	TTL(key string) (time.Duration, error)
	Expire(key string, expiration time.Duration) (bool, error)
	Incr(key string) (int64, error)
	Decr(key string) (int64, error)

	LRange(key string, start, stop int64) ([]string, error)
	Publish(channel string, message interface{}) (int64, error)
	PSubscribe(channel string, message interface{}) (*redis.PubSub, error)

	NewLocker() *RedisLock //分布式锁
}

type RedisLock

type RedisLock interface {
	LinearBackoff(backoff time.Duration)
	LimitRetry(backoff time.Duration, max int)
	ExponentialBackoff(min, max time.Duration)

	Key() string
	Token() string
	Metadata() string
	TTL(ctx context.Context) (time.Duration, error)
	Refresh(ctx context.Context, ttl time.Duration)
	Release(ctx context.Context) error
}

RedisLock 分布式锁

Jump to

Keyboard shortcuts

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