redis

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2019 License: Apache-2.0 Imports: 8 Imported by: 0

README

使用说明:

config.ini 配置

[redis]
; 地址(配置多个不同地址即为哨兵模式)
; 例如: 192.168.0.10:6379|192.168.0.11:6379|192.168.0.12:6379
host                = 192.168.0.213:16379
; 密码
pwd                 = 123456
; 数据库序号
db_index            = 0
; 最大空闲连接数
max_idle            = 1
; 最大连接数
max_active          = 100
; 空闲连接超时(秒)
idle_timeout        = 10
; 连接、读写超时(秒)
conn_timeout        = 30

API使用

redisPool := InitRedisPool()

//String
_, err := redisPool.Set("key", "123")
if err != nil {
	log.Fatalln(err)
}
val, err := redisPool.Get("key")
if err != nil {
	log.Fatalln(err)
}
fmt.Println("key:", val)

//Hash
hash := map[string]string{
	"field1": "value1",
	"field2": "value2",
	"field3": "value3",
}
_, err = redisPool.Hmset("Hkey", hash)
if err != nil {
	log.Fatalln(err)
}
hVal, err := redisPool.Hmget("Hkey", []string{"field1", "field2"})
if err != nil {
	log.Fatalln(err)
}
for _, item := range hVal {
	fmt.Println(item)
}

命令参考: redisfans

Documentation

Overview

Package redis (go-sentinel): https://github.com/FZambia/sentinel

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TestRole added in v1.3.0

func TestRole(c redis.Conn, expectedRole string) bool

TestRole wraps GetRole in a test to verify if the role matches an expected role string. If there was any error in querying the supplied connection, the function returns false. Works with Redis >= 2.8.12. It's not goroutine safe, but if you call this method on pooled connections then you are OK.

Types

type ConnPool

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

ConnPool redis连接池

func InitRedisPool

func InitRedisPool() *ConnPool

InitRedisPool 初始化

func (*ConnPool) Brpop

func (rc *ConnPool) Brpop(key string, timeoutSeconds int) ([]string, error)

Brpop for List

func (*ConnPool) Decr

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

Decr for string

func (*ConnPool) DelKey

func (rc *ConnPool) DelKey(key string) (interface{}, error)

DelKey for key

func (*ConnPool) ExistsKey

func (rc *ConnPool) ExistsKey(key string) (bool, error)

ExistsKey for key

func (*ConnPool) ExpireKey

func (rc *ConnPool) ExpireKey(key string, seconds int) (interface{}, error)

ExpireKey for key

func (*ConnPool) Get

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

Get for string

func (*ConnPool) GetErrNil added in v1.1.1

func (rc *ConnPool) GetErrNil(key string) (string, error)

GetErrNil for string

func (*ConnPool) Hdel

func (rc *ConnPool) Hdel(key string, field []string) (interface{}, error)

Hdel for Hash

func (*ConnPool) Hexists

func (rc *ConnPool) Hexists(key string, field string) (bool, error)

Hexists for Hash

func (*ConnPool) Hget

func (rc *ConnPool) Hget(key string, field string) (string, error)

Hget for Hash

func (*ConnPool) Hgetall

func (rc *ConnPool) Hgetall(key string) (map[string]string, error)

Hgetall for Hash

func (*ConnPool) Hlen

func (rc *ConnPool) Hlen(key string) (int, error)

Hlen for Hash

func (*ConnPool) Hmget

func (rc *ConnPool) Hmget(key string, field []string) ([]string, error)

Hmget for Hash

func (*ConnPool) Hmset

func (rc *ConnPool) Hmset(key string, fieldValue map[string]string) (interface{}, error)

Hmset for Hash

func (*ConnPool) Hset

func (rc *ConnPool) Hset(key string, field string, value string) (interface{}, error)

Hset for Hash

func (*ConnPool) Incr

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

Incr for string

func (*ConnPool) Llen

func (rc *ConnPool) Llen(key string) (int, error)

Llen for List

func (*ConnPool) Lpush

func (rc *ConnPool) Lpush(key string, value []string) (interface{}, error)

Lpush for List

func (*ConnPool) LuaScriptZset added in v1.2.0

func (rc *ConnPool) LuaScriptZset(script, key string, score int64, member string) (interface{}, error)

LuaScriptZset for SortedSet

func (*ConnPool) Rpop

func (rc *ConnPool) Rpop(key string) (string, error)

Rpop for List

func (*ConnPool) Sadd

func (rc *ConnPool) Sadd(key string, member []string) (interface{}, error)

Sadd for Set

func (*ConnPool) Scard

func (rc *ConnPool) Scard(key string) (int, error)

Scard for Set

func (*ConnPool) Set

func (rc *ConnPool) Set(key string, value string) (interface{}, error)

Set for string

func (*ConnPool) SetExpire

func (rc *ConnPool) SetExpire(key string, value string, seconds int) (interface{}, error)

SetExpire for string

func (*ConnPool) Smembers

func (rc *ConnPool) Smembers(key string) ([]string, error)

Smembers for Set

func (*ConnPool) Srem

func (rc *ConnPool) Srem(key string, member []string) (interface{}, error)

Srem for Set

func (*ConnPool) Zadd

func (rc *ConnPool) Zadd(key string, score int64, member string) (interface{}, error)

Zadd for SortedSet

func (*ConnPool) Zcard

func (rc *ConnPool) Zcard(key string) (int, error)

Zcard for SortedSet

func (*ConnPool) Zrange

func (rc *ConnPool) Zrange(key string, start, stop int) ([]string, error)

Zrange for SortedSet

func (*ConnPool) Zrangebyscore added in v1.2.4

func (rc *ConnPool) Zrangebyscore(key string, min, max string, offset, limit int) ([]string, error)

Zrangebyscore for SortedSet all: -inf ~ +inf min ~ max min-max: (min ~ (max

func (*ConnPool) Zrem

func (rc *ConnPool) Zrem(key string, member string) (interface{}, error)

Zrem for SortedSet

func (*ConnPool) Zremrangebyrank

func (rc *ConnPool) Zremrangebyrank(key string, start, stop int) (interface{}, error)

Zremrangebyrank for SortedSet

func (*ConnPool) Zremrangebyscore

func (rc *ConnPool) Zremrangebyscore(key string, min, max int64) (interface{}, error)

Zremrangebyscore for SortedSet

func (*ConnPool) Zrevrange

func (rc *ConnPool) Zrevrange(key string, start, stop int) ([]string, error)

Zrevrange for SortedSet

func (*ConnPool) Zrevrangebyscore added in v1.2.4

func (rc *ConnPool) Zrevrangebyscore(key string, max, min string, offset, limit int) ([]string, error)

Zrevrangebyscore for SortedSet

type NoSentinelsAvailable added in v1.3.0

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

NoSentinelsAvailable is returned when all sentinels in the list are exhausted (or none configured), and contains the last error returned by Dial (which may be nil)

func (NoSentinelsAvailable) Error added in v1.3.0

func (ns NoSentinelsAvailable) Error() string

type Sentinel added in v1.3.0

type Sentinel struct {
	// Addrs is a slice with known Sentinel addresses.
	Addrs []string

	// MasterName is a name of Redis master Sentinel servers monitor.
	MasterName string

	// Dial is a user supplied function to connect to Sentinel on given address. This
	// address will be chosen from Addrs slice.
	// Note that as per the redis-sentinel client guidelines, a timeout is mandatory
	// while connecting to Sentinels, and should not be set to 0.
	Dial func(addr string) (redis.Conn, error)

	// Pool is a user supplied function returning custom connection pool to Sentinel.
	// This can be useful to tune options if you are not satisfied with what default
	// Sentinel pool offers. See defaultPool() method for default pool implementation.
	// In most cases you only need to provide Dial function and let this be nil.
	Pool func(addr string) *redis.Pool
	// contains filtered or unexported fields
}

Sentinel provides a way to add high availability (HA) to Redis Pool using preconfigured addresses of Sentinel servers and name of master which Sentinels monitor. It works with Redis >= 2.8.12 (mostly because of ROLE command that was introduced in that version, it's possible though to support old versions using INFO command).

func (*Sentinel) Close added in v1.3.0

func (s *Sentinel) Close() error

Close closes current connection to Sentinel.

func (*Sentinel) Discover added in v1.3.0

func (s *Sentinel) Discover() error

Discover allows to update list of known Sentinel addresses. From docs:

A client may update its internal list of Sentinel nodes following this procedure: 1) Obtain a list of other Sentinels for this master using the command SENTINEL sentinels <master-name>. 2) Add every ip:port pair not already existing in our list at the end of the list.

func (*Sentinel) MasterAddr added in v1.3.0

func (s *Sentinel) MasterAddr() (string, error)

MasterAddr returns an address of current Redis master instance.

func (*Sentinel) SentinelAddrs added in v1.3.0

func (s *Sentinel) SentinelAddrs() ([]string, error)

SentinelAddrs returns a slice of known Sentinel addresses Sentinel server aware of.

func (*Sentinel) SlaveAddrs added in v1.3.0

func (s *Sentinel) SlaveAddrs() ([]string, error)

SlaveAddrs returns a slice with known slave addresses of current master instance.

func (*Sentinel) Slaves added in v1.3.0

func (s *Sentinel) Slaves() ([]*Slave, error)

Slaves returns a slice with known slaves of master instance.

type Slave added in v1.3.0

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

Slave represents a Redis slave instance which is known by Sentinel.

func (*Slave) Addr added in v1.3.0

func (s *Slave) Addr() string

Addr returns an address of slave.

func (*Slave) Available added in v1.3.0

func (s *Slave) Available() bool

Available returns if slave is in working state at moment based on information in slave flags.

Jump to

Keyboard shortcuts

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