redispool

package
v0.0.0-...-037b315 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// RoutePolicyRoundRobin 轮转
	RoutePolicyRoundRobin = iota
	// RoutePolicyRandom 随机
	RoutePolicyRandom
)

Variables

This section is empty.

Functions

func PutClient

func PutClient(cli *Client)

PutRedisClient puts a redis client into pool

func RegisterParamBuilder

func RegisterParamBuilder(builder ParamBuilder)

Types

type Client

type Client struct {
	redis.Conn
}

Client Wrapper the Redis conn

func GetClient

func GetClient(name string) (cli *Client, err error)

GetClient gets a redis client by business key

func (*Client) Del

func (c *Client) Del(keys ...string) (err error)

Del Del multiple keys in Redis

func (*Client) Exists

func (c *Client) Exists(key string) (ok bool, err error)

Exists Redis check whether key exists

func (*Client) Expire

func (c *Client) Expire(key string, ttl int) (err error)

Expire set expire of a key(in seconds)

func (*Client) ExpireAt

func (c *Client) ExpireAt(key string, expire int) (err error)

ExpireAt a key at a certain timestamp

func (*Client) Get

func (c *Client) Get(key string) (val []byte, ok bool, err error)

Get from Redis

func (*Client) GetInt64

func (c *Client) GetInt64(key string) (val int64, ok bool, err error)

Get from Redis

func (*Client) GetMD5

func (c *Client) GetMD5(key string) (val []byte, md string, ok bool, err error)

GetMD5 get value from redis by key and return the md5sum of the value

func (*Client) HDel

func (c *Client) HDel(key string, fields ...any) error

HDel hash delete

func (*Client) HExists

func (c *Client) HExists(key, field string) (ok bool, err error)

HExists Redis check whether hash field exists

func (*Client) HGet

func (c *Client) HGet(key, field string) (val []byte, err error)

HGet do a HGET cmd in Redis

func (*Client) HGetAll

func (c *Client) HGetAll(key string) (val map[string]string, err error)

HGetAll Do a Redis HGETALL command

func (*Client) HGetInt64

func (c *Client) HGetInt64(key, field string) (val int64, err error)

func (*Client) HIncrBy

func (c *Client) HIncrBy(key, field string, delta int) (val int, err error)

HIncrBy do a HINCRBY cmd in Redis

func (*Client) HKeys

func (c *Client) HKeys(key string) (keys []string, err error)

HKeys 获取hash所有fields

func (*Client) HLen

func (c *Client) HLen(key string) (nLen int, err error)

HLen do a HLen cmd in Redis

func (*Client) HMGet

func (c *Client) HMGet(key string, fields ...any) (ret [][]byte, err error)

HMGet do a HMGET

func (*Client) HMGetInt64

func (c *Client) HMGetInt64(key string, fields ...any) (ret []int64, err error)

func (*Client) HMSet

func (c *Client) HMSet(key string, val ...any) (err error)

HMSet do a HMSET cmd in Redis

func (*Client) HMSetMap

func (c *Client) HMSetMap(key string, val map[string]any) (err error)

HMSetMap do a HMSET in Redis, the key-value pairs are stored in map

func (*Client) HMSetMapInt64

func (c *Client) HMSetMapInt64(key string, val map[string]int64) (err error)

func (*Client) HScan

func (c *Client) HScan(key string, cursor, count int, pattern string) (newCursor int, pairs map[string]string, err error)

HScan hash scan

func (*Client) HSet

func (c *Client) HSet(key, field string, val any) (err error)

HSet do a HSET cmd in Redis

func (*Client) HSetNX

func (c *Client) HSetNX(key, field string, val any) (ok bool, err error)

HSetNX Redis HSETNX

func (*Client) IncrBy

func (c *Client) IncrBy(key string, delta int) (ret int, err error)

IncrBy Redis incrby

func (*Client) LLen

func (c *Client) LLen(key string) (n int, err error)

LLen 返回list长度

func (*Client) LPop

func (c *Client) LPop(key string) (val []byte, err error)

LPop 移除并返回list头元素

func (*Client) LPush

func (c *Client) LPush(key string, val ...any) error

LPush 插入到list头

func (*Client) LRange

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

LRange 返回list元素

func (*Client) LTrim

func (c *Client) LTrim(key string, start int, end int) (err error)

LTrim 对一个列表进行修剪, 从0开始的闭区间

func (*Client) MGet

func (c *Client) MGet(keys ...string) (ret [][]byte, err error)

MGet Redis MGet

func (*Client) MSet

func (c *Client) MSet(kvs ...any) (err error)

MSet Redis Mset

func (*Client) MultiHGet

func (c *Client) MultiHGet(keys []string, field string) (vals [][]byte, err error)

Hash Multi HGET

func (*Client) Put

func (c *Client) Put()

Put returns the connection to the redis pool

func (*Client) RPush

func (c *Client) RPush(key string, val ...any) error

RPush 插入到list尾

func (*Client) SAdd

func (c *Client) SAdd(key string, val ...any) error

SAdd 向集合中添加一个或者多个成员

func (*Client) SCard

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

SRandMembers 返回集合中的总数

func (*Client) SIsMember

func (c *Client) SIsMember(key string, member any) (ok bool, err error)

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

func (*Client) SMembers

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

SMembers 返回集合中的所有成员 Deprecated: 不要使用这个函数,对于大的集合,会卡爆

func (*Client) SPop

func (c *Client) SPop(key string) (val string, err error)

SPop 移除并返回集合中的一个随机元素

func (*Client) SRandMembers

func (c *Client) SRandMembers(key string, count ...int) ([]string, error)

SRandMembers 随机返回集合中的成员

func (*Client) SRem

func (c *Client) SRem(key string, val ...any) error

SRem 移除集合中的一个或多个成员

func (*Client) SScan

func (c *Client) SScan(key string, cursor, count int, pattern string) (nextCursor int, vals []string, err error)

func (*Client) Scan

func (c *Client) Scan(cursor, count int, patterns ...string) (newCursor int, keys []string, err error)

Scan scan

func (*Client) Set

func (c *Client) Set(key string, val any) (err error)

Set RedisSet

func (*Client) SetEX

func (c *Client) SetEX(key string, ttl int, val any) (err error)

SetEX RedisSetEX

func (*Client) SetEXNX

func (c *Client) SetEXNX(key string, ttl int, val any) (ok bool, err error)

SetEX SetEXNX 当ok == true新设置一个key, ok == false key已存在, 设置未生效

func (*Client) SetMD5

func (c *Client) SetMD5(key string, val any, md string) (err error)

SetMD5 set the value to the key, success if the md equals to the original md5sum of the value

func (*Client) SetNX

func (c *Client) SetNX(key string, val any) (ok bool, err error)

SetNX RedisSetNX

func (*Client) SetPXNX

func (c *Client) SetPXNX(key string, ttl int, val any) (ok bool, err error)

SetPXNX 当ok == true新设置一个key, ok == false key已存在, 设置未生效

func (*Client) TTL

func (c *Client) TTL(key string) (int, error)

TTL 返回过期时间,-2表示key不存在,-1表示未设置过期时间

func (*Client) Time

func (c *Client) Time() ([]int, error)

Time 返回当前服务器时间,一个包含两个整型的列表: 第一个整数是当前时间(以 UNIX 时间戳格式表示),而第二个整数是当前这一秒钟已经逝去的微秒数。

func (*Client) Type

func (c *Client) Type(key string) (t KeyType, ok bool, err error)

获取指定类型, 返回值err表示redis错误, ok 表示key是否存在, t为具体的类型

func (*Client) ZAdd

func (c *Client) ZAdd(key string, val ...any) error

ZAdd 向有序集合添加一个或多个成员,或者更新已存在成员的分数

func (*Client) ZCard

func (c *Client) ZCard(key string) (int, error)

ZCard 获取有序集合的成员数

func (*Client) ZIncrBy

func (c *Client) ZIncrBy(key, member string, score int) (int, error)

ZScore 返回有序集中,成员的分数值

func (*Client) ZRange

func (c *Client) ZRange(key string, start, end int) ([]string, error)

ZRange 按分数值递增返回有序集合成指定区间[start, end]内的成员(不包括score)

func (*Client) ZRangeByScore

func (c *Client) ZRangeByScore(key string, start, end int) ([]string, error)

func (*Client) ZRangeOfSlices

func (c *Client) ZRangeOfSlices(key string, start, end int) ([][]byte, error)

ZRangeOfSlices 按分数值递增返回有序集合成指定区间[start, end]内的成员(不包括score)

func (*Client) ZRangeWithScores

func (c *Client) ZRangeWithScores(key string, start, end int) ([]string, error)

ZRangeWithScores 按分数值递减返回有序集合成指定区间[start, end]内的成员 返回的[]string中,偶数下标的元素为member,该member对应的score值存储在后一个元素里面

func (*Client) ZRank

func (c *Client) ZRank(key, member string) (int, error)

ZRank 返回有序集合中指定成员的索引

func (*Client) ZRem

func (c *Client) ZRem(key string, members ...any) error

ZRem 移除有序集合中的一个或多个成员

func (*Client) ZRemRangeByRank

func (c *Client) ZRemRangeByRank(key string, start, stop int64) (n int64, err error)

func (*Client) ZRevRange

func (c *Client) ZRevRange(key string, start, end int) ([]string, error)

ZRevRange 通过索引区间返回有序集合成指定区间[start, end]内的成员

func (*Client) ZRevRangeWithScores

func (c *Client) ZRevRangeWithScores(key string, start, end int) ([]string, error)

ZRevRangeWithScores 通过索引区间返回有序集合成指定区间[start, end]内的成员 返回的[]string中,偶数下标的元素为member,该member对应的score值存储在后一个元素里面

func (*Client) ZRevRank

func (c *Client) ZRevRank(key, member string) (int, error)

ZRevRank 返回有序集合中指定成员的排名(从0开始),有序集成员按分数值递减(从大到小)排序

func (*Client) ZScore

func (c *Client) ZScore(key, member string) (int, error)

ZScore 返回有序集中,成员的分数值

type KeyType

type KeyType string

kv start

const (
	KeyTypeNone   KeyType = "none"
	KeyTypeString KeyType = "string"
	KeyTypeList   KeyType = "list"
	KeyTypeSet    KeyType = "set"
	KeyTypeZSet   KeyType = "zset"
	KeyTypeHash   KeyType = "hash"
)

type ParamBuilder

type ParamBuilder func(name string) *PoolParam

根据name 构建 PoolParam

type Pool

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

Pool implements the connection pool of redis

func GetPool

func GetPool(name string) (pool *Pool, err error)

GetPool 根据name,获取该name对应的pool

func NewPool

func NewPool(addr, pass string, maxIdle, maxActive, idleTimeoutSecond int) (pool *Pool, err error)

NewPool 新建Redis连接池,默认网络超时时间为0

func NewPoolWithParam

func NewPoolWithParam(param *PoolParam) (pool *Pool, err error)

NewPoolWithParam 使用param参数创建连接池

func (*Pool) DumpStatus

func (p *Pool) DumpStatus() string

DumpStatus redis连接池对外暴露的状态接口

func (*Pool) GetClient

func (p *Pool) GetClient() (cli *Client, err error)

GetClient alloc a connection from pool

func (*Pool) SetAddrList

func (p *Pool) SetAddrList(addrList string)

SetAddrList 支持运行时动态设置redis服务器地址

type PoolParam

type PoolParam struct {
	Name string //连接池名字
	Addr string //redis地址 ip:port  @deprecated

	Policy                 RoutePolicy //负载均衡策略
	Pass                   string      //密码,没有则为空
	MaxIdle                int         //做多允许多少空闲连接
	MaxActive              int         //最多允许多少并发连接
	IdleTimeoutSecond      int         //单个链接空闲多久被销毁,单位秒
	NetworkTimeoutMsec     int         //网络超时(connect,send,recv),单位毫秒
	HealthCheckIntervalSec int         //定时检查池子里面的节点是是否健康
	// contains filtered or unexported fields
}

PoolParam 连接池参数

type RoutePolicy

type RoutePolicy int

RoutePolicy redis各个地址负载均衡策略

Jump to

Keyboard shortcuts

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