redis

package
v1.1.18 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2020 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Expiry 默认2秒的超时时间,当到达超时时强行释放锁。
	Expiry = 3 * time.Second

	// Tries 如果获取锁失败,可重试的次数
	Tries = 4

	// Delay 重新获得锁的间隔(毫秒
	Delay = 700
)

Variables

View Source
var (
	// ErrRedisMtxAcquire 申请分布式锁失败
	ErrRedisMtxAcquire = errors.New("failed to acquire lock")
	// ErrRedisMtxInvaildToken 错误的传入token
	ErrRedisMtxInvaildToken = errors.New("acquire token is empty")
)
View Source
var (

	// ErrConfigConvert 配置转换失败
	ErrConfigConvert = errors.New("Convert linker config")
)
View Source
var GetDelScript = redis.NewScript(1, `
if redis.call("get", KEYS[1]) == ARGV[1] then
	return redis.call("del", KEYS[1])
else
	return 0
end`)

GetDelScript 将get 和del整合成原子操作

View Source
var IncrExpireScript = redis.NewScript(1, `
local current
current = redis.call("incr", KEYS[1])
if tonumber(current) == 1 then
    redis.call("expire", KEYS[1], 1)
end`)

IncrExpireScript 将incr和expire指令整合成原子操作

Functions

func ConnGet

func ConnGet(conn redis.Conn, key string) (string, error)

ConnGet 返回 key 所关联的字符串值。

func ConnHDel

func ConnHDel(conn redis.Conn, args ...interface{}) (int64, error)

ConnHDel HDel

func ConnHGet

func ConnHGet(conn redis.Conn, hashID string, field string) (string, error)

ConnHGet hget

func ConnSetEx

func ConnSetEx(conn redis.Conn, key string, val string, timeOutSeconds int64) error

ConnSetEx 将值 value 关联到 key ,并将 key 的生存时间设为 seconds (以秒为单位)。

func ConnZRangeByScorelimit

func ConnZRangeByScorelimit(conn redis.Conn, field string, min int64, max int64, limit int) ([]string, error)

ConnZRangeByScorelimit 返回有序集

Types

type Client

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

Client redis client

func Get added in v1.1.10

func Get() *Client

Get get redis client

func New added in v1.1.10

func New() *Client

New 构建 Redis client

func (*Client) ActiveConnCount added in v1.1.10

func (rc *Client) ActiveConnCount() int

ActiveConnCount 获取 redis 当前连接数量

func (*Client) Close added in v1.1.17

func (rc *Client) Close()

Close 释放redis

func (*Client) Conn added in v1.1.10

func (rc *Client) Conn() redis.Conn

Conn 获取一个redis连接,使用这个接口的时候一定要记得手动`回收`连接

func (*Client) DBSize

func (rc *Client) DBSize() (int64, error)

DBSize O(1) 返回当前数据库的 key 的数量

func (*Client) Del

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

Del 删除给定的一个或多个 key

func (*Client) Expire

func (rc *Client) Expire(key string, timeOutSeconds int64) (int64, error)

Expire 设置指定key的过期时间

func (*Client) Get

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

Get 返回 key 所关联的字符串值。

func (*Client) HDel

func (rc *Client) HDel(args ...interface{}) (int64, error)

HDel 设置指定hashset的内容, 如果field不存在, 该操作无效, 返回0

func (*Client) HExist

func (rc *Client) HExist(hashID string, field string) (int, error)

HExist 返回hash里面field是否存在

func (*Client) HGet

func (rc *Client) HGet(hashID string, field string) (string, error)

HGet 返回哈希表 key 中给定域 field 的值

func (*Client) HGetAll

func (rc *Client) HGetAll(hashID string) (map[string]string, error)

HGetAll 获取指定hash的所有内容

func (*Client) HIncrBy

func (rc *Client) HIncrBy(hashID string, field string, increment int) (int, error)

HIncrBy 为哈希表 key 中的域 field 的值加上增量 increment

func (*Client) HKeys

func (rc *Client) HKeys(hashID string) ([]string, error)

HKeys 返回哈希表 key 中的所有域

func (*Client) HLen

func (rc *Client) HLen(field string) (int, error)

HLen 返回哈希表 key 中域的数量。

func (*Client) HSet

func (rc *Client) HSet(hashID string, field string, val string) error

HSet 将哈希表 key 中的域 field 的值设为 value

func (*Client) Init added in v1.1.17

func (rc *Client) Init(cfg interface{}) error

Init 创建redis连接池基于传入配置

func (*Client) Keys

func (rc *Client) Keys(pattern string) ([]string, error)

Keys O(N) 查找所有符合给定模式 pattern 的 key 。注:生产环境注意使用,如果有大量的key会阻塞redis操作。

func (*Client) LLen

func (rc *Client) LLen(key string) (int, error)

LLen 返回列表 key 的长度。

func (*Client) LPush

func (rc *Client) LPush(key string, value string) (int, error)

LPush 将一个或多个值 value 插入到列表 key 的表头

func (*Client) LPushX

func (rc *Client) LPushX(key string, value string) (int, error)

LPushX 将值 value 插入到列表 key 的表头,当且仅当 key 存在并且是一个列表。

func (*Client) LRange

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

LRange 返回列表 key 中指定区间内的元素,区间以偏移量 start 和 stop 指定。

func (*Client) LRem

func (rc *Client) LRem(key string, count int, value string) (int, error)

LRem 根据参数 count 的值,移除列表中与参数 value 相等的元素。

func (*Client) Ping

func (rc *Client) Ping() (string, error)

Ping 测试一个连接是否可用

func (*Client) RPop

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

RPop 移除并返回列表 key 的尾元素。

func (*Client) RPush

func (rc *Client) RPush(key string, value string) (int, error)

RPush 将一个或多个值 value 插入到列表 key 的表尾(最右边)。

func (*Client) Run added in v1.1.17

func (rc *Client) Run()

Run nil

func (*Client) SAdd

func (rc *Client) SAdd(key string, member ...interface{}) (int, error)

SAdd 将一个或多个 member 元素加入到集合 key 当中,已经存在于集合的 member 元素将被忽略。

func (*Client) SCard

func (rc *Client) SCard(key string) (int, error)

SCard returns cardinality of the set(count of elements). returns 0 when set does not exist

func (*Client) SMembers

func (rc *Client) SMembers(key string) ([]string, error)

SMembers 返回集合 key 中的所有成员。

func (*Client) SRandMember

func (rc *Client) SRandMember(key string, count int) ([]string, error)

SRandMember 如果命令执行时,只提供了 key 参数,那么返回集合中的一个随机元素。

func (*Client) SRem

func (rc *Client) SRem(key string, member ...interface{}) (int, error)

SRem 移除集合 key 中的一个或多个 member 元素,不存在的 member 元素会被忽略。

func (*Client) Set

func (rc *Client) Set(key string, val string) error

Set 将字符串值 value 关联到 key 。

func (*Client) SetEx

func (rc *Client) SetEx(key string, timeOutSeconds int64, val string) error

SetEx 将值 value 关联到 key ,并将 key 的生存时间设为 seconds (以秒为单位)。

func (*Client) SetWithExpire

func (rc *Client) SetWithExpire(key string, val interface{}, timeOutSeconds int64) (interface{}, error)

SetWithExpire Set带ex

func (*Client) SiSMembers

func (rc *Client) SiSMembers(key string, member string) (int, error)

SiSMembers 判断 member 元素是否集合 key 的成员。

func (*Client) ZAdd

func (rc *Client) ZAdd(field string, score int64, member string) (int64, error)

ZAdd 添加到集合

func (*Client) ZCount

func (rc *Client) ZCount(field string, min int64, max int64) (int64, error)

ZCount 返回有序集 key 中, score 值在 min 和 max 之间(默认包括 score 值等于 min 或 max )的成员的数量。

func (*Client) ZIncrby

func (rc *Client) ZIncrby(field string, incScore int, member string) (int64, error)

ZIncrby 为有序集 key 的成员 member 的 score 值加上增量 increment 。

func (*Client) ZRange

func (rc *Client) ZRange(field string, start int64, stop int64) ([]string, error)

ZRange 返回有序集 key 中,指定区间内的成员。

func (*Client) ZRangeByScore

func (rc *Client) ZRangeByScore(field string, min int64, max int64) ([]string, error)

ZRangeByScore 返回有序集 key 中,所有 score 值介于 min 和 max 之间(包括等于 min 或 max )的成员。有序集成员按 score 值递增(从小到大)次序排列。

func (*Client) ZRangeByScorelimit

func (rc *Client) ZRangeByScorelimit(field string, min int64, max int64, limit int) ([]string, error)

ZRangeByScorelimit 返回有序集

func (*Client) ZRem

func (rc *Client) ZRem(field string, member string) (int64, error)

ZRem 从集合中删除成员

func (*Client) ZRemRangeByScore

func (rc *Client) ZRemRangeByScore(field string, min int64, max int64) (int64, error)

ZRemRangeByScore 移除有序集 key 中,所有 score 值介于 min 和 max 之间(包括等于 min 或 max )的成员

func (*Client) ZRevRange

func (rc *Client) ZRevRange(field string, start int64, stop int64) ([]string, error)

ZRevRange 返回有序集 key 中,指定区间内的成员。

func (*Client) ZRevRangeByScoreLimitWithscores

func (rc *Client) ZRevRangeByScoreLimitWithscores(field string, max int64, min int64, offset int, count int) ([]string, error)

ZRevRangeByScoreLimitWithscores 返回有序集 key 中, score 值介于 max 和 min 之间(默认包括等于 max 或 min )的所有的成员

func (*Client) ZRevRangeByScorelimit

func (rc *Client) ZRevRangeByScorelimit(field string, max int64, min int64, offset int, count int) ([]string, error)

ZRevRangeByScorelimit 返回有序集 key 中, score 值介于 max 和 min 之间(默认包括等于 max 或 min )的所有的成员

func (*Client) ZRevRangeWithscores

func (rc *Client) ZRevRangeWithscores(field string, start int64, stop int64) ([]string, error)

ZRevRangeWithscores 返回有序集 key 中,指定区间内的成员。

func (*Client) ZRevRank

func (rc *Client) ZRevRank(field string, member string) (int64, error)

ZRevRank 返回有序集 key 中成员 member 的排名。其中有序集成员按 score 值递减(从大到小)排序。

func (*Client) ZScore

func (rc *Client) ZScore(field string, member string) (int64, error)

ZScore 返回有序集 key 中,成员 member 的 score 值。

type Config added in v1.1.10

type Config struct {
	Address string //connection string, like "redis:// :password@10.0.1.11:6379/0"

	ReadTimeOut    time.Duration // 连接的读取超时时间
	WriteTimeOut   time.Duration // 连接的写入超时时间
	ConnectTimeOut time.Duration // 连接超时时间
	MaxIdle        int           // 最大空闲连接数
	MaxActive      int           // 最大连接数,当为0时没有连接数限制
	IdleTimeout    time.Duration // 闲置连接的超时时间, 设置小于服务器的超时时间 redis.conf : timeout
}

Config 配置项

type Mutex

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

Mutex 分布式token锁

func (*Mutex) Lock

func (m *Mutex) Lock(from string) error

Lock 分布式锁

func (*Mutex) Unlock

func (m *Mutex) Unlock() bool

Unlock 释放锁

Jump to

Keyboard shortcuts

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