mredis

package
v0.0.0-...-da45c02 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2017 License: Apache-2.0 Imports: 4 Imported by: 0

README

redis

redis常用命令操作封装。根据具体情况而定

快速开始


dialFunc := func(config PoolConfig) (c redis.Conn, err error) {
c, err = redis.Dial(config.Network, config.Address)
if err != nil {
    return nil, err
}

if config.password != "" {
    if _, err := c.Do("AUTH", config.Password); err != nil {
        c.Close()
        return nil, err
    }
}

_, selecterr := c.Do("SELECT", config.DbNum)
if selecterr != nil {
    c.Close()
    return nil, selecterr
}
return
 }

 config=PoolConfig{
        Network  :"tcp",
      Address  :   "127.0.0.1:6379",
      MaxIdle  :10,
      Password :"123456",
      DbNum    :0,
      Df       :dialFunc,
 }

redis, _ := NewRedisPool(config)

API

  • 字符串类型相关命令操作

GET

 redis.Get("cacheKey") 

SET

redis.Set("cacheKey","value") 
  • 哈希(Hash)类型相关命令操作

Redis hash 是一个string类型的field和value的映射表,hash特别适合用于存储对象。 Redis 中每个 hash 可以存储 232 - 1 键值对(40多亿)。

HGET

redis.HGet("cacheKey","field")

依赖

  • go get github.com/garyburd/redigo/redis

redis

Documentation

Overview

redis 封装包 主要引用 github.com/garyburd/redigo/redis

一、自定义Pool
     不用github.com/garyburd/redigo/redis包中原有的pool功能。而已重新实现pool功能。
     新的pool主要参考github.com/fzzy/radix中的pool的功能,通过golang的channel来实现,
     非常漂亮;同时,pool的Put看起来非常清晰;总体实现比较优雅。

例子:

  dialFunc := func(config PoolConfig) (c redis.Conn, err error) {
	c, err = redis.Dial(config.Network, config.Address)
	if err != nil {
		return nil, err
	}

	if config.password != "" {
		if _, err := c.Do("AUTH", config.Password); err != nil {
			c.Close()
			return nil, err
		}
	}

	_, selecterr := c.Do("SELECT", config.DbNum)
	if selecterr != nil {
		c.Close()
		return nil, selecterr
	}
	return
    }

    config=PoolConfig{
           Network  :"tcp",
	      Address  :   "127.0.0.1:6379",
	      MaxIdle  :10,
	      Password :"123456",
	      DbNum    :0,
	      Df       :dialFunc,
    }

	redis, _ := NewRedisPool(config)

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNil     = redis.ErrNil
	ErrPowerOn = errors.New("RedisPool:turn on first")
	ErrNotOK   = errors.New("not ok")
)

Functions

This section is empty.

Types

type Pool

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

池 获取一个可用的redis连接。 如果没有可用的它将新建一个redis连接 主要方法:

pool.Get() 从池中获取连接
pool.Put() 重新放回池中

func NewPool

func NewPool(config PoolConfig) (*Pool, error)

新建池

例子:

dialFunc := func(config PoolConfig) (c redis.Conn, err error) {

c, err = redis.Dial(config.Network, config.Address)

if err != nil {
	return nil, err
}
if config.Password != "" {
	if _, err := c.Do("AUTH", config.Password); err != nil {
		c.Close()
		return nil, err
	}
}

_, selecterr := c.Do("SELECT", config.DbNum)

if selecterr != nil {
	c.Close()
	return nil, selecterr
}
       return
}
config := PoolConfig{
	Network  :"tcp",
	Address  : "127.0.0.1:6379",
	MaxIdle  :10,
	Password :"123456",
	DbNum    :0,
	Df       :dialFunc,
}
p, _ := NewPool(config)

func (*Pool) Empty

func (p *Pool) Empty()

移除并关闭池中所有的redis连接。 假设没有其他的连接等着被放回 这个方法有效地关闭和清理池。

func (*Pool) Get

func (p *Pool) Get() (redis.Conn, error)

获取一个可用的redis连接。如果没有可用的它将新建一个redis连接

func (*Pool) Put

func (p *Pool) Put(conn redis.Conn) error

把一个redis连接放回池中。如果池已经满了,将关闭此redis连接。 如果此redis连接已经关闭(由于连接失败或其他原因),则不应该再放回池中。

type PoolConfig

type PoolConfig struct {
	Network        string
	Address        string
	MaxIdle        int
	DbNum          int
	Password       string
	IdleTimeout    time.Duration
	ReadTimeout    time.Duration
	WriteTimeout   time.Duration
	ConnectTimeout time.Duration
	Df             func(config PoolConfig) (redis.Conn, error)
}

配置信息

type RedisPool

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

内部使用了池功能

func NewRedisPool

func NewRedisPool(config PoolConfig) (*RedisPool, error)

新建

func (*RedisPool) Append

func (rp *RedisPool) Append(key, val string) (int, error)

APPEND key value 如果 key 已经存在并且是一个字符串, APPEND 命令将 value 追加到 key 原来的值的末尾。 否则,新建key/value

func (*RedisPool) BLPop

func (rp *RedisPool) BLPop(key string, timeout int64) (map[string]string, error)

BLPOP key1 timeout(秒) 移出并获取列表的第一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。

func (*RedisPool) BLPopMulti

func (rp *RedisPool) BLPopMulti(timeout int64, keys ...interface{}) (map[string]string, error)

BLPOP key1 [key2 ] timeout(秒) 移出并获取列表的第一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。

func (*RedisPool) BRPop

func (rp *RedisPool) BRPop(key string, timeout int64) (map[string]string, error)

BRPOP key1 timeout 移出并获取列表的最后一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。

func (*RedisPool) BRPopMulti

func (rp *RedisPool) BRPopMulti(timeout int64, keys ...interface{}) (map[string]string, error)

BRPOP key1 [key2 ] timeout 移出并获取列表的最后一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。

func (*RedisPool) Close

func (rp *RedisPool) Close() error

关闭 清空内部连接池

func (*RedisPool) Decr

func (rp *RedisPool) Decr(key string) (int64, error)

DECR key 将 key 中储存的数字值减一。 @return int64 减一之后的数字值

func (*RedisPool) Del

func (rp *RedisPool) Del(key string) (int, error)

删除指定 key 的值 @return 返回删除个数。

func (*RedisPool) Do

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

从池中获取空闲(或者新建)连接处理redis命令操作 调用有效连接redis.Conn的Do方法 此方法处理连接池连接的取出和放回等额外的相关工作

func (*RedisPool) Expire

func (rp *RedisPool) Expire(key string, expired int64) (int, error)

EXPIRE key seconds 为给定 key 设置过期时间。(以秒为单位)。

func (*RedisPool) Get

func (rp *RedisPool) Get(key string) (string, error)

获取指定 key 的值。(string(字符串)) 如果key不存在或者有异常则返回空字符串

func (*RedisPool) GetConn

func (rp *RedisPool) GetConn() (redis.Conn, error)

调用内部pool.Get

func (*RedisPool) GetJson

func (rp *RedisPool) GetJson(key string, reply interface{}) (err error)

json @see Get()

func (*RedisPool) HDel

func (rp *RedisPool) HDel(key string, field ...interface{}) (int, error)

HDEL key field2 [field2] 删除一个或多个哈希表字段

func (*RedisPool) HGet

func (rp *RedisPool) HGet(key, field string) (string, error)

HGET key field 获取存储在哈希表中指定字段的值 如果key或field不存在或者有异常则返回空字符串

func (*RedisPool) HGetAll

func (rp *RedisPool) HGetAll(key string) (map[string]string, error)

HGETALL key 获取在哈希表中指定 key 的所有字段和值

func (*RedisPool) HLen

func (rp *RedisPool) HLen(key string) (int, error)

HLEN key 获取哈希表中字段的数量

func (*RedisPool) HMSet

func (rp *RedisPool) HMSet(key string, field_value ...interface{}) error

HMSET key field1 value1 [field2 value2 ] 同时将多个 field-value (域-值)对设置到哈希表 key 中。

func (*RedisPool) HSet

func (rp *RedisPool) HSet(key, field, value string) (int, error)

HSET key field value 将哈希表 key 中的字段 field 的值设为 value

func (*RedisPool) Incr

func (rp *RedisPool) Incr(key string) (int64, error)

INCR key 将 key 中储存的数字值增一。 @return int64 增一之后的数字值

func (*RedisPool) LLen

func (rp *RedisPool) LLen(key string) (int64, error)

LLEN key 获取列表长度

func (*RedisPool) LPop

func (rp *RedisPool) LPop(key string) (string, error)

LPOP key 移出并获取列表的第一个元素

func (*RedisPool) LPush

func (rp *RedisPool) LPush(key string, values ...interface{}) (int64, error)

LPUSH key value1 [value2] 将一个或多个值插入到列表头部

func (*RedisPool) LPushX

func (rp *RedisPool) LPushX(key string, value interface{}) (int64, error)

LPUSHX key value1 [value2] 将一个或多个值插入到已存在的列表头部

func (*RedisPool) LRange

func (rp *RedisPool) LRange(key string, start, stop int64) ([]string, error)

LRANGE key start stop 获取列表指定范围内的元素

func (*RedisPool) LRem

func (rp *RedisPool) LRem(key string, count int64, value string) (int64, error)

LREM key count value 移除列表元素 Redis Lrem 根据参数 COUNT 的值,移除列表中与参数 VALUE 相等的元素。

COUNT 的值可以是以下几种:
  count > 0 : 从表头开始向表尾搜索,移除与 VALUE 相等的元素,数量为 COUNT 。
  count < 0 : 从表尾开始向表头搜索,移除与 VALUE 相等的元素,数量为 COUNT 的绝对值。
  count = 0 : 移除表中所有与 VALUE 相等的值。

func (*RedisPool) LSet

func (rp *RedisPool) LSet(key string, index int64, value string) error

LSET key index value 通过索引设置列表元素的值

func (*RedisPool) LTrim

func (rp *RedisPool) LTrim(key string, start, stop int64) error

LTRIM key start stop Redis Ltrim 对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。 下标 0 表示列表的第一个元素,以 1 表示列表的第二个元素,以此类推。 你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推。

func (*RedisPool) PExpire

func (rp *RedisPool) PExpire(key string, expired int64) (int, error)

PEXPIRE key milliseconds 设置 key 的过期时间亿以毫秒计。

func (*RedisPool) PutConn

func (rp *RedisPool) PutConn(conn redis.Conn) error

调用内部pool.Put

func (*RedisPool) RPop

func (rp *RedisPool) RPop(key string) (string, error)

RPOP key 移除并获取列表最后一个元素

func (*RedisPool) RPush

func (rp *RedisPool) RPush(key string, values ...interface{}) (int64, error)

RPUSH key value1 [value2] 在列表尾部中添加一个或多个值

func (*RedisPool) RPushX

func (rp *RedisPool) RPushX(key string, value interface{}) (int64, error)

RPUSHX key value1 [value2] 为已存在的列表尾部添加值

func (*RedisPool) Set

func (rp *RedisPool) Set(key, val string) error

设置指定 key 的值 注意:一个键最大能存储512MB。

func (*RedisPool) SetEx

func (rp *RedisPool) SetEx(key, val string, expired int64) error

SETEX key seconds value 将值 value 关联到 key , 并将 key 的过期时间设为 seconds (以秒为单位)。 @expired 有效时长 (以秒为单位)

func (*RedisPool) SetExJson

func (rp *RedisPool) SetExJson(key string, val interface{}, expired int64) (err error)

json @see SetEx()

func (*RedisPool) SetJson

func (rp *RedisPool) SetJson(key string, val interface{}) (err error)

json @see Set()

func (*RedisPool) SetPowerOn

func (rp *RedisPool) SetPowerOn(powerOn bool) *RedisPool

Jump to

Keyboard shortcuts

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