redis

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2022 License: MIT Imports: 4 Imported by: 0

README

redis 客户端

redigo基础上封装redis客户端。


安装

go get -u github.com/zhufuyi/pkg/redis


使用示例

    err := redis.NewRedisPool("192.168.101.88:6379", "123456")

    // 数据
    var cmds = []struct {
        Cmd    string
        Args   []interface{}
        Result []string
    }{
        // string
        {"SET", []interface{}{"testKey", "hello redis"}, []string{"OK"}},
        {"GET", []interface{}{"testKey"}, []string{"hello redis"}},

        // 集合
        {"SADD", []interface{}{"testSet", "zhangsan", "lisi", 100}, []string{"0"}},
        {"SMEMBERS", []interface{}{"testSet"}, []string{"100", "zhangsan", "lisi"}},

        // 有序集合
        {"ZADD", []interface{}{"testZset", 28, "zhangsan", 24, "lisi", 26, "wangwu"}, []string{"0"}},
        {"ZRANGE", []interface{}{"testZset", 0, -1, "withscores"}, []string{"lisi", "24", "wangwu", "26", "zhangsan", "28"}},
        {"ZREVRANGEBYSCORE", []interface{}{"testZset", "+inf", "-inf", "withscores", "limit", 0, 100}, []string{"zhangsan", "28", "wangwu", "26", "lisi", "24"}},

        // 哈希
        {"HMSET", []interface{}{"testHSet", "name", "lisi", "age", 11}, []string{"OK"}},
        {"HGETALL", []interface{}{"testHSet"}, []string{"name", "lisi", "age", "11"}},
    }


	rconn, err := redis.GetConn()
	if err != nil {
		return
	}
	defer rconn.Close()

	for _, v := range cmds {
	    result, err := rconn.Do(v.Cmd, v.Args...)
		// result, err := rconn.WithLog().Do(v.Cmd, v.Args...) // 打印执行语句
		if err != nil {
			continue
		}
        fmt.Println(result)
	}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNil = redis.ErrNil

ErrNil redis空值

Functions

func Bool

func Bool(reply interface{}, err error) (bool, error)

Bool 布尔类型

func ByteSlices

func ByteSlices(reply interface{}, err error) ([][]byte, error)

ByteSlices 字节流slice类型

func Bytes

func Bytes(reply interface{}, err error) ([]byte, error)

Bytes 字节流类型

func Float64

func Float64(reply interface{}, err error) (float64, error)

Float64 64位浮点数

func Int

func Int(reply interface{}, err error) (int, error)

Int 整型

func Int64

func Int64(reply interface{}, err error) (int64, error)

Int64 64位整型

func Int64Map

func Int64Map(result interface{}, err error) (map[string]int64, error)

Int64Map key字符串,val为int64的map类型

func IntMap

func IntMap(result interface{}, err error) (map[string]int, error)

IntMap key字符串,val为int的map类型

func Ints

func Ints(reply interface{}, err error) ([]int, error)

Ints 整型slice类型

func NewRedisPool

func NewRedisPool(server, password string) error

NewRedisPool connect redis,if test ping failed,return error

func NewRedisPoolWithNoAuth

func NewRedisPoolWithNoAuth(server string) error

NewRedisPoolWithNoAuth connect redis with no auth, if test ping failed,return error

func String

func String(reply interface{}, err error) (string, error)

String 字符串类型

func StringMap

func StringMap(result interface{}, err error) (map[string]string, error)

StringMap kv都是字符串的map类型

func Strings

func Strings(reply interface{}, err error) ([]string, error)

Strings 字符串slice类型

func Uint64

func Uint64(reply interface{}, err error) (uint64, error)

Uint64 64位非负数整型

func Values

func Values(reply interface{}, err error) ([]interface{}, error)

Values 任意类型

Types

type DefaultRedisConn

type DefaultRedisConn struct {
	redis.Conn
	// contains filtered or unexported fields
}

DefaultRedisConn 默认redis连接信息

func (*DefaultRedisConn) Do

func (d *DefaultRedisConn) Do(commandName string, args ...interface{}) (reply interface{}, err error)

Do The Do method combines the functionality of the Send, Flush and Receive methods.

func (*DefaultRedisConn) Flush

func (d *DefaultRedisConn) Flush() error

Flush flushes the output buffer to the Redis server.

func (*DefaultRedisConn) Receive

func (d *DefaultRedisConn) Receive() (reply interface{}, err error)

Receive receives a single reply from the Redis server

func (*DefaultRedisConn) Send

func (d *DefaultRedisConn) Send(commandName string, args ...interface{}) error

Send writes the command to the client's output buffer.

func (*DefaultRedisConn) WithLog

func (d *DefaultRedisConn) WithLog() RedisConn

WithLog 设置打印日志

type RedisConn

type RedisConn interface {
	redis.Conn
	WithLog() RedisConn
}

RedisConn redis接口

func GetConn

func GetConn() (RedisConn, error)

GetConn 对redis操作前,先要调用此函数从redis连接池获取一个连接,要对结果做错误判断,防止获取nil的连接,然后对空指针操作造成panic

type RedisPool

type RedisPool struct {
	redis.Pool
}

RedisPool redis连接池

func (*RedisPool) Get

func (r *RedisPool) Get() RedisConn

Get 从连接池获取redis连接

Jump to

Keyboard shortcuts

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