mirror of https://github.com/go-redis/redis.git
Review API.
This commit is contained in:
parent
df1b8a3f5c
commit
11e1783560
77
README.md
77
README.md
|
@ -20,21 +20,6 @@ Install:
|
|||
|
||||
go get github.com/vmihailenco/redis
|
||||
|
||||
Contributing
|
||||
------------
|
||||
|
||||
Configure Redis to allow maximum 10 clients:
|
||||
|
||||
maxclients 10
|
||||
|
||||
Run tests:
|
||||
|
||||
go test -gocheck.v
|
||||
|
||||
Run benchmarks:
|
||||
|
||||
go test -gocheck.b
|
||||
|
||||
Getting Client instance
|
||||
-----------------------
|
||||
|
||||
|
@ -64,7 +49,7 @@ Example 2:
|
|||
}
|
||||
|
||||
initConn := func(client *redis.Client) error {
|
||||
auth := client.Auth("foo")
|
||||
auth := client.Auth("key")
|
||||
if auth.Err() != nil {
|
||||
return auth.Err()
|
||||
}
|
||||
|
@ -84,13 +69,13 @@ Both `closeConn` and `initConn` functions can be `nil`.
|
|||
Running commands
|
||||
----------------
|
||||
|
||||
set := redisClient.Set("foo", "bar")
|
||||
set := redisClient.Set("key", "hello")
|
||||
if set.Err() != nil {
|
||||
panic(set.Err())
|
||||
}
|
||||
ok := set.Val()
|
||||
|
||||
get := redisClient.Get("foo")
|
||||
get := redisClient.Get("key")
|
||||
if get.Err() != nil && get.Err() != redis.Nil {
|
||||
panic(get.Err())
|
||||
}
|
||||
|
@ -101,14 +86,27 @@ Pipelining
|
|||
|
||||
Client has ability to run commands in batches:
|
||||
|
||||
reqs, err := redisClient.Pipelined(func(c *redis.PipelineClient) {
|
||||
c.Set("key1", "hello1") // queue command SET
|
||||
c.Set("key2", "hello2") // queue command SET
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
for _, req := range reqs {
|
||||
// ...
|
||||
}
|
||||
|
||||
Or:
|
||||
|
||||
pipeline, err := redisClient.PipelineClient()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer pipeline.Close()
|
||||
|
||||
setReq := pipeline.Set("foo1", "bar1") // queue command SET
|
||||
getReq := pipeline.Get("foo2") // queue command GET
|
||||
setReq := pipeline.Set("key1", "hello1") // queue command SET
|
||||
getReq := pipeline.Get("key2") // queue command GET
|
||||
|
||||
reqs, err := pipeline.RunQueued() // run queued commands
|
||||
if err != nil {
|
||||
|
@ -132,13 +130,13 @@ Multi/Exec
|
|||
Example:
|
||||
|
||||
func transaction(multi *redis.MultiClient) ([]redis.Req, error) {
|
||||
get := multiClient.Get("foo")
|
||||
get := multiClient.Get("key")
|
||||
if get.Err() != nil {
|
||||
panic(get.Err())
|
||||
}
|
||||
|
||||
reqs, err = multiClient.Exec(func() {
|
||||
multi.Set("foo", get.Val() + "1")
|
||||
multi.Set("key", get.Val() + "1")
|
||||
})
|
||||
if err == redis.Nil {
|
||||
return transaction()
|
||||
|
@ -153,7 +151,7 @@ Example:
|
|||
}
|
||||
defer multiClient.Close()
|
||||
|
||||
watch := multiClient.Watch("foo")
|
||||
watch := multiClient.Watch("key")
|
||||
if watch.Err() != nil {
|
||||
panic(watch.Err())
|
||||
}
|
||||
|
@ -205,7 +203,7 @@ Commands are thread safe. Following code is correct:
|
|||
|
||||
for i := 0; i < 1000; i++ {
|
||||
go func() {
|
||||
redisClient.Incr("foo")
|
||||
redisClient.Incr("key")
|
||||
}()
|
||||
}
|
||||
|
||||
|
@ -220,7 +218,7 @@ Example:
|
|||
return req
|
||||
}
|
||||
|
||||
get := Get(redisClient, "foo")
|
||||
get := Get(redisClient, "key")
|
||||
if get.Err() != nil && get.Err() != redis.Nil {
|
||||
panic(get.Err())
|
||||
}
|
||||
|
@ -231,3 +229,32 @@ Connection pool
|
|||
Client uses connection pool with default capacity of 10 connections. To change pool capacity:
|
||||
|
||||
redisClient.ConnPool.(*redis.MultiConnPool).MaxCap = 1
|
||||
|
||||
Look and feel
|
||||
-------------
|
||||
|
||||
Some corner cases:
|
||||
|
||||
SORT list LIMIT 0 2 ASC
|
||||
client.Sort("list", redis.Sort{Offset: 0, Count: 2, Order: "ASC"})
|
||||
|
||||
ZRANGEBYSCORE zset -inf +inf WITHSCORES LIMIT 0 2
|
||||
client.ZRangeByScoreWithScores("zset", "-inf", "+inf", 0, 2)
|
||||
|
||||
ZINTERSTORE out 2 zset1 zset2 WEIGHTS 2 3 AGGREGATE SUM
|
||||
client.ZInterStore("out", 2, redis.ZStore{Weights: []int64{2, 3}}, "zset1", "zset2")
|
||||
|
||||
Contributing
|
||||
------------
|
||||
|
||||
Configure Redis to allow maximum 10 clients:
|
||||
|
||||
maxclients 10
|
||||
|
||||
Run tests:
|
||||
|
||||
go test -gocheck.v
|
||||
|
||||
Run benchmarks:
|
||||
|
||||
go test -gocheck.b
|
||||
|
|
301
commands.go
301
commands.go
|
@ -4,14 +4,8 @@ import (
|
|||
"strconv"
|
||||
)
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
type Limit struct {
|
||||
Offset, Count int64
|
||||
}
|
||||
|
||||
func NewLimit(offset, count int64) *Limit {
|
||||
return &Limit{offset, count}
|
||||
func formatFloat(f float64) string {
|
||||
return strconv.FormatFloat(f, 'f', -1, 32)
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
@ -22,8 +16,8 @@ func (c *Client) Auth(password string) *StatusReq {
|
|||
return req
|
||||
}
|
||||
|
||||
func (c *Client) Echo(message string) *BulkReq {
|
||||
req := NewBulkReq("ECHO", message)
|
||||
func (c *Client) Echo(message string) *StringReq {
|
||||
req := NewStringReq("ECHO", message)
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
@ -49,8 +43,8 @@ func (c *Client) Del(keys ...string) *IntReq {
|
|||
return req
|
||||
}
|
||||
|
||||
func (c *Client) Dump(key string) *BulkReq {
|
||||
req := NewBulkReq("DUMP", key)
|
||||
func (c *Client) Dump(key string) *StringReq {
|
||||
req := NewStringReq("DUMP", key)
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
@ -73,19 +67,19 @@ func (c *Client) ExpireAt(key string, timestamp int64) *BoolReq {
|
|||
return req
|
||||
}
|
||||
|
||||
func (c *Client) Keys(pattern string) *MultiBulkReq {
|
||||
req := NewMultiBulkReq("KEYS", pattern)
|
||||
func (c *Client) Keys(pattern string) *StringSliceReq {
|
||||
req := NewStringSliceReq("KEYS", pattern)
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
||||
func (c *Client) Migrate(host string, port int32, key, db string, timeout int64) *StatusReq {
|
||||
func (c *Client) Migrate(host, port, key string, db, timeout int64) *StatusReq {
|
||||
req := NewStatusReq(
|
||||
"MIGRATE",
|
||||
host,
|
||||
strconv.FormatInt(int64(port), 10),
|
||||
port,
|
||||
key,
|
||||
db,
|
||||
strconv.FormatInt(db, 10),
|
||||
strconv.FormatInt(timeout, 10),
|
||||
)
|
||||
c.Process(req)
|
||||
|
@ -105,9 +99,9 @@ func (c *Client) ObjectRefCount(keys ...string) *IntReq {
|
|||
return req
|
||||
}
|
||||
|
||||
func (c *Client) ObjectEncoding(keys ...string) *BulkReq {
|
||||
func (c *Client) ObjectEncoding(keys ...string) *StringReq {
|
||||
args := append([]string{"OBJECT", "ENCODING"}, keys...)
|
||||
req := NewBulkReq(args...)
|
||||
req := NewStringReq(args...)
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
@ -143,8 +137,8 @@ func (c *Client) PTTL(key string) *IntReq {
|
|||
return req
|
||||
}
|
||||
|
||||
func (c *Client) RandomKey() *BulkReq {
|
||||
req := NewBulkReq("RANDOMKEY")
|
||||
func (c *Client) RandomKey() *StringReq {
|
||||
req := NewStringReq("RANDOMKEY")
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
@ -161,9 +155,10 @@ func (c *Client) RenameNX(key, newkey string) *BoolReq {
|
|||
return req
|
||||
}
|
||||
|
||||
func (c *Client) Restore(key, ttl int64, value string) *StatusReq {
|
||||
func (c *Client) Restore(key string, ttl int64, value string) *StatusReq {
|
||||
req := NewStatusReq(
|
||||
"RESTORE",
|
||||
key,
|
||||
strconv.FormatInt(ttl, 10),
|
||||
value,
|
||||
)
|
||||
|
@ -171,9 +166,36 @@ func (c *Client) Restore(key, ttl int64, value string) *StatusReq {
|
|||
return req
|
||||
}
|
||||
|
||||
func (c *Client) Sort(key string, params ...string) *MultiBulkReq {
|
||||
args := append([]string{"SORT", key}, params...)
|
||||
req := NewMultiBulkReq(args...)
|
||||
type Sort struct {
|
||||
By string
|
||||
Offset, Count float64
|
||||
Get []string
|
||||
Order string
|
||||
IsAlpha bool
|
||||
Store string
|
||||
}
|
||||
|
||||
func (c *Client) Sort(key string, sort Sort) *StringSliceReq {
|
||||
args := []string{"SORT", key}
|
||||
if sort.By != "" {
|
||||
args = append(args, sort.By)
|
||||
}
|
||||
if sort.Offset != 0 || sort.Count != 0 {
|
||||
args = append(args, "LIMIT", formatFloat(sort.Offset), formatFloat(sort.Count))
|
||||
}
|
||||
for _, get := range sort.Get {
|
||||
args = append(args, "GET", get)
|
||||
}
|
||||
if sort.Order != "" {
|
||||
args = append(args, sort.Order)
|
||||
}
|
||||
if sort.IsAlpha {
|
||||
args = append(args, "ALPHA")
|
||||
}
|
||||
if sort.Store != "" {
|
||||
args = append(args, "STORE", sort.Store)
|
||||
}
|
||||
req := NewStringSliceReq(args...)
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
@ -214,8 +236,8 @@ func (c *Client) DecrBy(key string, decrement int64) *IntReq {
|
|||
return req
|
||||
}
|
||||
|
||||
func (c *Client) Get(key string) *BulkReq {
|
||||
req := NewBulkReq("GET", key)
|
||||
func (c *Client) Get(key string) *StringReq {
|
||||
req := NewStringReq("GET", key)
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
@ -226,8 +248,8 @@ func (c *Client) GetBit(key string, offset int64) *IntReq {
|
|||
return req
|
||||
}
|
||||
|
||||
func (c *Client) GetRange(key string, start, end int64) *BulkReq {
|
||||
req := NewBulkReq(
|
||||
func (c *Client) GetRange(key string, start, end int64) *StringReq {
|
||||
req := NewStringReq(
|
||||
"GETRANGE",
|
||||
key,
|
||||
strconv.FormatInt(start, 10),
|
||||
|
@ -237,8 +259,8 @@ func (c *Client) GetRange(key string, start, end int64) *BulkReq {
|
|||
return req
|
||||
}
|
||||
|
||||
func (c *Client) GetSet(key, value string) *BulkReq {
|
||||
req := NewBulkReq("GETSET", key, value)
|
||||
func (c *Client) GetSet(key, value string) *StringReq {
|
||||
req := NewStringReq("GETSET", key, value)
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
@ -257,9 +279,9 @@ func (c *Client) IncrBy(key string, value int64) *IntReq {
|
|||
|
||||
// incrbyfloat
|
||||
|
||||
func (c *Client) MGet(keys ...string) *MultiBulkReq {
|
||||
func (c *Client) MGet(keys ...string) *IfaceSliceReq {
|
||||
args := append([]string{"MGET"}, keys...)
|
||||
req := NewMultiBulkReq(args...)
|
||||
req := NewIfaceSliceReq(args...)
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
@ -345,14 +367,14 @@ func (c *Client) HExists(key, field string) *BoolReq {
|
|||
return req
|
||||
}
|
||||
|
||||
func (c *Client) HGet(key, field string) *BulkReq {
|
||||
req := NewBulkReq("HGET", key, field)
|
||||
func (c *Client) HGet(key, field string) *StringReq {
|
||||
req := NewStringReq("HGET", key, field)
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
||||
func (c *Client) HGetAll(key string) *MultiBulkReq {
|
||||
req := NewMultiBulkReq("HGETALL", key)
|
||||
func (c *Client) HGetAll(key string) *StringSliceReq {
|
||||
req := NewStringSliceReq("HGETALL", key)
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
@ -363,10 +385,14 @@ func (c *Client) HIncrBy(key, field string, incr int64) *IntReq {
|
|||
return req
|
||||
}
|
||||
|
||||
// hincrbyfloat
|
||||
func (c *Client) HIncrByFloat(key, field string, incr float64) *FloatReq {
|
||||
req := NewFloatReq("HINCRBYFLOAT", key, field, formatFloat(incr))
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
||||
func (c *Client) HKeys(key string) *MultiBulkReq {
|
||||
req := NewMultiBulkReq("HKEYS", key)
|
||||
func (c *Client) HKeys(key string) *StringSliceReq {
|
||||
req := NewStringSliceReq("HKEYS", key)
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
@ -377,9 +403,9 @@ func (c *Client) HLen(key string) *IntReq {
|
|||
return req
|
||||
}
|
||||
|
||||
func (c *Client) HMGet(key string, fields ...string) *MultiBulkReq {
|
||||
func (c *Client) HMGet(key string, fields ...string) *IfaceSliceReq {
|
||||
args := append([]string{"HMGET", key}, fields...)
|
||||
req := NewMultiBulkReq(args...)
|
||||
req := NewIfaceSliceReq(args...)
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
@ -403,32 +429,32 @@ func (c *Client) HSetNX(key, field, value string) *BoolReq {
|
|||
return req
|
||||
}
|
||||
|
||||
func (c *Client) HVals(key string) *MultiBulkReq {
|
||||
req := NewMultiBulkReq("HVALS", key)
|
||||
func (c *Client) HVals(key string) *StringSliceReq {
|
||||
req := NewStringSliceReq("HVALS", key)
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
func (c *Client) BLPop(timeout int64, keys ...string) *MultiBulkReq {
|
||||
func (c *Client) BLPop(timeout int64, keys ...string) *StringSliceReq {
|
||||
args := append([]string{"BLPOP"}, keys...)
|
||||
args = append(args, strconv.FormatInt(timeout, 10))
|
||||
req := NewMultiBulkReq(args...)
|
||||
req := NewStringSliceReq(args...)
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
||||
func (c *Client) BRPop(timeout int64, keys ...string) *MultiBulkReq {
|
||||
func (c *Client) BRPop(timeout int64, keys ...string) *StringSliceReq {
|
||||
args := append([]string{"BRPOP"}, keys...)
|
||||
args = append(args, strconv.FormatInt(timeout, 10))
|
||||
req := NewMultiBulkReq(args...)
|
||||
req := NewStringSliceReq(args...)
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
||||
func (c *Client) BRPopLPush(source, destination string, timeout int64) *BulkReq {
|
||||
req := NewBulkReq(
|
||||
func (c *Client) BRPopLPush(source, destination string, timeout int64) *StringReq {
|
||||
req := NewStringReq(
|
||||
"BRPOPLPUSH",
|
||||
source,
|
||||
destination,
|
||||
|
@ -438,8 +464,8 @@ func (c *Client) BRPopLPush(source, destination string, timeout int64) *BulkReq
|
|||
return req
|
||||
}
|
||||
|
||||
func (c *Client) LIndex(key string, index int64) *BulkReq {
|
||||
req := NewBulkReq("LINDEX", key, strconv.FormatInt(index, 10))
|
||||
func (c *Client) LIndex(key string, index int64) *StringReq {
|
||||
req := NewStringReq("LINDEX", key, strconv.FormatInt(index, 10))
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
@ -456,8 +482,8 @@ func (c *Client) LLen(key string) *IntReq {
|
|||
return req
|
||||
}
|
||||
|
||||
func (c *Client) LPop(key string) *BulkReq {
|
||||
req := NewBulkReq("LPOP", key)
|
||||
func (c *Client) LPop(key string) *StringReq {
|
||||
req := NewStringReq("LPOP", key)
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
@ -475,8 +501,8 @@ func (c *Client) LPushX(key, value string) *IntReq {
|
|||
return req
|
||||
}
|
||||
|
||||
func (c *Client) LRange(key string, start, stop int64) *MultiBulkReq {
|
||||
req := NewMultiBulkReq(
|
||||
func (c *Client) LRange(key string, start, stop int64) *StringSliceReq {
|
||||
req := NewStringSliceReq(
|
||||
"LRANGE",
|
||||
key,
|
||||
strconv.FormatInt(start, 10),
|
||||
|
@ -509,14 +535,14 @@ func (c *Client) LTrim(key string, start, stop int64) *StatusReq {
|
|||
return req
|
||||
}
|
||||
|
||||
func (c *Client) RPop(key string) *BulkReq {
|
||||
req := NewBulkReq("RPOP", key)
|
||||
func (c *Client) RPop(key string) *StringReq {
|
||||
req := NewStringReq("RPOP", key)
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
||||
func (c *Client) RPopLPush(source, destination string) *BulkReq {
|
||||
req := NewBulkReq("RPOPLPUSH", source, destination)
|
||||
func (c *Client) RPopLPush(source, destination string) *StringReq {
|
||||
req := NewStringReq("RPOPLPUSH", source, destination)
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
@ -549,9 +575,9 @@ func (c *Client) SCard(key string) *IntReq {
|
|||
return req
|
||||
}
|
||||
|
||||
func (c *Client) SDiff(keys ...string) *MultiBulkReq {
|
||||
func (c *Client) SDiff(keys ...string) *StringSliceReq {
|
||||
args := append([]string{"SDIFF"}, keys...)
|
||||
req := NewMultiBulkReq(args...)
|
||||
req := NewStringSliceReq(args...)
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
@ -563,9 +589,9 @@ func (c *Client) SDiffStore(destination string, keys ...string) *IntReq {
|
|||
return req
|
||||
}
|
||||
|
||||
func (c *Client) SInter(keys ...string) *MultiBulkReq {
|
||||
func (c *Client) SInter(keys ...string) *StringSliceReq {
|
||||
args := append([]string{"SINTER"}, keys...)
|
||||
req := NewMultiBulkReq(args...)
|
||||
req := NewStringSliceReq(args...)
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
@ -583,8 +609,8 @@ func (c *Client) SIsMember(key, member string) *BoolReq {
|
|||
return req
|
||||
}
|
||||
|
||||
func (c *Client) SMembers(key string) *MultiBulkReq {
|
||||
req := NewMultiBulkReq("SMEMBERS", key)
|
||||
func (c *Client) SMembers(key string) *StringSliceReq {
|
||||
req := NewStringSliceReq("SMEMBERS", key)
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
@ -595,14 +621,14 @@ func (c *Client) SMove(source, destination, member string) *BoolReq {
|
|||
return req
|
||||
}
|
||||
|
||||
func (c *Client) SPop(key string) *BulkReq {
|
||||
req := NewBulkReq("SPOP", key)
|
||||
func (c *Client) SPop(key string) *StringReq {
|
||||
req := NewStringReq("SPOP", key)
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
||||
func (c *Client) SRandMember(key string) *BulkReq {
|
||||
req := NewBulkReq("SRANDMEMBER", key)
|
||||
func (c *Client) SRandMember(key string) *StringReq {
|
||||
req := NewStringReq("SRANDMEMBER", key)
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
@ -614,9 +640,9 @@ func (c *Client) SRem(key string, members ...string) *IntReq {
|
|||
return req
|
||||
}
|
||||
|
||||
func (c *Client) SUnion(keys ...string) *MultiBulkReq {
|
||||
func (c *Client) SUnion(keys ...string) *StringSliceReq {
|
||||
args := append([]string{"SUNION"}, keys...)
|
||||
req := NewMultiBulkReq(args...)
|
||||
req := NewStringSliceReq(args...)
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
@ -630,20 +656,21 @@ func (c *Client) SUnionStore(destination string, keys ...string) *IntReq {
|
|||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
type ZMember struct {
|
||||
type Z struct {
|
||||
Score float64
|
||||
Member string
|
||||
}
|
||||
|
||||
func NewZMember(score float64, member string) *ZMember {
|
||||
return &ZMember{score, member}
|
||||
type ZStore struct {
|
||||
Weights []int64
|
||||
Aggregate string
|
||||
}
|
||||
|
||||
func (m *ZMember) ScoreString() string {
|
||||
return strconv.FormatFloat(m.Score, 'f', -1, 32)
|
||||
func (m *Z) ScoreString() string {
|
||||
return formatFloat(m.Score)
|
||||
}
|
||||
|
||||
func (c *Client) ZAdd(key string, members ...*ZMember) *IntReq {
|
||||
func (c *Client) ZAdd(key string, members ...Z) *IntReq {
|
||||
args := []string{"ZADD", key}
|
||||
for _, m := range members {
|
||||
args = append(args, m.ScoreString(), m.Member)
|
||||
|
@ -665,8 +692,8 @@ func (c *Client) ZCount(key, min, max string) *IntReq {
|
|||
return req
|
||||
}
|
||||
|
||||
func (c *Client) ZIncrBy(key string, increment int64, member string) *FloatReq {
|
||||
req := NewFloatReq("ZINCRBY", key, strconv.FormatInt(increment, 10), member)
|
||||
func (c *Client) ZIncrBy(key string, increment float64, member string) *FloatReq {
|
||||
req := NewFloatReq("ZINCRBY", key, formatFloat(increment), member)
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
@ -674,27 +701,26 @@ func (c *Client) ZIncrBy(key string, increment int64, member string) *FloatReq {
|
|||
func (c *Client) ZInterStore(
|
||||
destination string,
|
||||
numkeys int64,
|
||||
keys []string,
|
||||
weights []int64,
|
||||
aggregate string,
|
||||
store ZStore,
|
||||
keys ...string,
|
||||
) *IntReq {
|
||||
args := []string{"ZINTERSTORE", destination, strconv.FormatInt(numkeys, 10)}
|
||||
args = append(args, keys...)
|
||||
if weights != nil {
|
||||
if len(store.Weights) > 0 {
|
||||
args = append(args, "WEIGHTS")
|
||||
for _, w := range weights {
|
||||
args = append(args, strconv.FormatInt(w, 10))
|
||||
for _, weight := range store.Weights {
|
||||
args = append(args, strconv.FormatInt(weight, 10))
|
||||
}
|
||||
}
|
||||
if aggregate != "" {
|
||||
args = append(args, "AGGREGATE", aggregate)
|
||||
if store.Aggregate != "" {
|
||||
args = append(args, "AGGREGATE", store.Aggregate)
|
||||
}
|
||||
req := NewIntReq(args...)
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
||||
func (c *Client) ZRange(key string, start, stop int64, withScores bool) *MultiBulkReq {
|
||||
func (c *Client) zRange(key string, start, stop int64, withScores bool) *StringSliceReq {
|
||||
args := []string{
|
||||
"ZRANGE",
|
||||
key,
|
||||
|
@ -704,34 +730,50 @@ func (c *Client) ZRange(key string, start, stop int64, withScores bool) *MultiBu
|
|||
if withScores {
|
||||
args = append(args, "WITHSCORES")
|
||||
}
|
||||
req := NewMultiBulkReq(args...)
|
||||
req := NewStringSliceReq(args...)
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
||||
func (c *Client) ZRangeByScore(
|
||||
func (c *Client) ZRange(key string, start, stop int64) *StringSliceReq {
|
||||
return c.zRange(key, start, stop, false)
|
||||
}
|
||||
|
||||
func (c *Client) ZRangeWithScores(key string, start, stop int64) *StringSliceReq {
|
||||
return c.zRange(key, start, stop, true)
|
||||
}
|
||||
|
||||
func (c *Client) zRangeByScore(
|
||||
key string,
|
||||
min, max string,
|
||||
withScores bool,
|
||||
limit *Limit,
|
||||
) *MultiBulkReq {
|
||||
offset, count int64,
|
||||
) *StringSliceReq {
|
||||
args := []string{"ZRANGEBYSCORE", key, min, max}
|
||||
if withScores {
|
||||
args = append(args, "WITHSCORES")
|
||||
}
|
||||
if limit != nil {
|
||||
if offset != 0 || count != 0 {
|
||||
args = append(
|
||||
args,
|
||||
"LIMIT",
|
||||
strconv.FormatInt(limit.Offset, 10),
|
||||
strconv.FormatInt(limit.Count, 10),
|
||||
strconv.FormatInt(offset, 10),
|
||||
strconv.FormatInt(count, 10),
|
||||
)
|
||||
}
|
||||
req := NewMultiBulkReq(args...)
|
||||
req := NewStringSliceReq(args...)
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
||||
func (c *Client) ZRangeByScore(key string, min, max string, offset, count int64) *StringSliceReq {
|
||||
return c.zRangeByScore(key, min, max, false, offset, count)
|
||||
}
|
||||
|
||||
func (c *Client) ZRangeByScoreWithScores(key string, min, max string, offset, count int64) *StringSliceReq {
|
||||
return c.zRangeByScore(key, min, max, true, offset, count)
|
||||
}
|
||||
|
||||
func (c *Client) ZRank(key, member string) *IntReq {
|
||||
req := NewIntReq("ZRANK", key, member)
|
||||
c.Process(req)
|
||||
|
@ -762,38 +804,50 @@ func (c *Client) ZRemRangeByScore(key, min, max string) *IntReq {
|
|||
return req
|
||||
}
|
||||
|
||||
func (c *Client) ZRevRange(key, start, stop string, withScores bool) *MultiBulkReq {
|
||||
func (c *Client) zRevRange(key, start, stop string, withScores bool) *StringSliceReq {
|
||||
args := []string{"ZREVRANGE", key, start, stop}
|
||||
if withScores {
|
||||
args = append(args, "WITHSCORES")
|
||||
}
|
||||
req := NewMultiBulkReq(args...)
|
||||
req := NewStringSliceReq(args...)
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
||||
func (c *Client) ZRevRangeByScore(
|
||||
key, start, stop string,
|
||||
withScores bool,
|
||||
limit *Limit,
|
||||
) *MultiBulkReq {
|
||||
func (c *Client) ZRevRange(key, start, stop string) *StringSliceReq {
|
||||
return c.zRevRange(key, start, stop, false)
|
||||
}
|
||||
|
||||
func (c *Client) ZRevRangeWithScores(key, start, stop string) *StringSliceReq {
|
||||
return c.zRevRange(key, start, stop, true)
|
||||
}
|
||||
|
||||
func (c *Client) zRevRangeByScore(key, start, stop string, withScores bool, offset, count int64) *StringSliceReq {
|
||||
args := []string{"ZREVRANGEBYSCORE", key, start, stop}
|
||||
if withScores {
|
||||
args = append(args, "WITHSCORES")
|
||||
}
|
||||
if limit != nil {
|
||||
if offset != 0 || count != 0 {
|
||||
args = append(
|
||||
args,
|
||||
"LIMIT",
|
||||
strconv.FormatInt(limit.Offset, 10),
|
||||
strconv.FormatInt(limit.Count, 10),
|
||||
strconv.FormatInt(offset, 10),
|
||||
strconv.FormatInt(count, 10),
|
||||
)
|
||||
}
|
||||
req := NewMultiBulkReq(args...)
|
||||
req := NewStringSliceReq(args...)
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
||||
func (c *Client) ZRevRangeByScore(key, start, stop string, offset, count int64) *StringSliceReq {
|
||||
return c.zRevRangeByScore(key, start, stop, false, offset, count)
|
||||
}
|
||||
|
||||
func (c *Client) ZRevRangeByScoreWithScores(key, start, stop string, offset, count int64) *StringSliceReq {
|
||||
return c.zRevRangeByScore(key, start, stop, false, offset, count)
|
||||
}
|
||||
|
||||
func (c *Client) ZRevRank(key, member string) *IntReq {
|
||||
req := NewIntReq("ZREVRANK", key, member)
|
||||
c.Process(req)
|
||||
|
@ -809,20 +863,19 @@ func (c *Client) ZScore(key, member string) *FloatReq {
|
|||
func (c *Client) ZUnionStore(
|
||||
destination string,
|
||||
numkeys int64,
|
||||
keys []string,
|
||||
weights []int64,
|
||||
aggregate string,
|
||||
store ZStore,
|
||||
keys ...string,
|
||||
) *IntReq {
|
||||
args := []string{"ZUNIONSTORE", destination, strconv.FormatInt(numkeys, 10)}
|
||||
args = append(args, keys...)
|
||||
if weights != nil {
|
||||
if len(store.Weights) > 0 {
|
||||
args = append(args, "WEIGHTS")
|
||||
for _, w := range weights {
|
||||
args = append(args, strconv.FormatInt(w, 10))
|
||||
for _, weight := range store.Weights {
|
||||
args = append(args, strconv.FormatInt(weight, 10))
|
||||
}
|
||||
}
|
||||
if aggregate != "" {
|
||||
args = append(args, "AGGREGATE", aggregate)
|
||||
if store.Aggregate != "" {
|
||||
args = append(args, "AGGREGATE", store.Aggregate)
|
||||
}
|
||||
req := NewIntReq(args...)
|
||||
c.Process(req)
|
||||
|
@ -849,14 +902,14 @@ func (c *Client) ClientKill(ipPort string) *StatusReq {
|
|||
return req
|
||||
}
|
||||
|
||||
func (c *Client) ClientList() *BulkReq {
|
||||
req := NewBulkReq("CLIENT", "LIST")
|
||||
func (c *Client) ClientList() *StringReq {
|
||||
req := NewStringReq("CLIENT", "LIST")
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
||||
func (c *Client) ConfigGet(parameter string) *MultiBulkReq {
|
||||
req := NewMultiBulkReq("CONFIG", "GET", parameter)
|
||||
func (c *Client) ConfigGet(parameter string) *StringSliceReq {
|
||||
req := NewStringSliceReq("CONFIG", "GET", parameter)
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
@ -891,8 +944,8 @@ func (c *Client) FlushDb() *StatusReq {
|
|||
return req
|
||||
}
|
||||
|
||||
func (c *Client) Info() *BulkReq {
|
||||
req := NewBulkReq("INFO")
|
||||
func (c *Client) Info() *StringReq {
|
||||
req := NewStringReq("INFO")
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
@ -931,8 +984,8 @@ func (c *Client) Sync() {
|
|||
panic("not implemented")
|
||||
}
|
||||
|
||||
func (c *Client) Time() *MultiBulkReq {
|
||||
req := NewMultiBulkReq("TIME")
|
||||
func (c *Client) Time() *StringSliceReq {
|
||||
req := NewStringSliceReq("TIME")
|
||||
c.Process(req)
|
||||
return req
|
||||
}
|
||||
|
|
|
@ -11,15 +11,13 @@ import (
|
|||
|
||||
type Conn struct {
|
||||
RW io.ReadWriteCloser
|
||||
Rd *bufio.Reader
|
||||
ReqBuf []byte
|
||||
Rd reader
|
||||
}
|
||||
|
||||
func NewConn(rw io.ReadWriteCloser) *Conn {
|
||||
return &Conn{
|
||||
RW: rw,
|
||||
Rd: bufio.NewReaderSize(rw, 1024),
|
||||
ReqBuf: make([]byte, 0, 1024),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
2
multi.go
2
multi.go
|
@ -45,7 +45,7 @@ func (c *MultiClient) Exec(do func()) ([]Req, error) {
|
|||
do()
|
||||
|
||||
c.mtx.Lock()
|
||||
c.reqs = append(c.reqs, NewMultiBulkReq("EXEC"))
|
||||
c.reqs = append(c.reqs, NewIfaceSliceReq("EXEC"))
|
||||
if len(c.reqs) == 2 {
|
||||
c.mtx.Unlock()
|
||||
return []Req{}, nil
|
||||
|
|
81
parser.go
81
parser.go
|
@ -21,7 +21,7 @@ var (
|
|||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
func AppendReq(buf []byte, args []string) []byte {
|
||||
func appendReq(buf []byte, args []string) []byte {
|
||||
buf = append(buf, '*')
|
||||
buf = strconv.AppendUint(buf, uint64(len(args)), 10)
|
||||
buf = append(buf, '\r', '\n')
|
||||
|
@ -37,13 +37,14 @@ func AppendReq(buf []byte, args []string) []byte {
|
|||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
type ReadLiner interface {
|
||||
type reader interface {
|
||||
ReadLine() ([]byte, bool, error)
|
||||
Read([]byte) (int, error)
|
||||
ReadN(n int) ([]byte, error)
|
||||
Buffered() int
|
||||
}
|
||||
|
||||
func readLine(rd ReadLiner) ([]byte, error) {
|
||||
func readLine(rd reader) ([]byte, error) {
|
||||
line, isPrefix, err := rd.ReadLine()
|
||||
if err != nil {
|
||||
return line, err
|
||||
|
@ -56,42 +57,15 @@ func readLine(rd ReadLiner) ([]byte, error) {
|
|||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
func ParseReq(rd ReadLiner) ([]string, error) {
|
||||
line, err := readLine(rd)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if line[0] != '*' {
|
||||
return []string{string(line)}, nil
|
||||
}
|
||||
numReplies, err := strconv.ParseInt(string(line[1:]), 10, 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
args := make([]string, 0)
|
||||
for i := int64(0); i < numReplies; i++ {
|
||||
line, err = readLine(rd)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if line[0] != '$' {
|
||||
return nil, fmt.Errorf("Expected '$', but got %q", line)
|
||||
}
|
||||
|
||||
line, err = readLine(rd)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
args = append(args, string(line))
|
||||
}
|
||||
return args, nil
|
||||
func parseReply(rd reader) (interface{}, error) {
|
||||
return _parseReply(rd, false)
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
func parseIfaceSliceReply(rd reader) (interface{}, error) {
|
||||
return _parseReply(rd, true)
|
||||
}
|
||||
|
||||
func ParseReply(rd ReadLiner) (interface{}, error) {
|
||||
func _parseReply(rd reader, useIfaceSlice bool) (interface{}, error) {
|
||||
line, err := readLine(rd)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
|
@ -139,9 +113,10 @@ func ParseReply(rd ReadLiner) (interface{}, error) {
|
|||
return nil, Nil
|
||||
}
|
||||
|
||||
val := make([]interface{}, 0)
|
||||
if useIfaceSlice {
|
||||
vals := make([]interface{}, 0)
|
||||
if len(line) == 2 && line[1] == '0' {
|
||||
return val, nil
|
||||
return vals, nil
|
||||
}
|
||||
|
||||
numReplies, err := strconv.ParseInt(string(line[1:]), 10, 64)
|
||||
|
@ -150,17 +125,39 @@ func ParseReply(rd ReadLiner) (interface{}, error) {
|
|||
}
|
||||
|
||||
for i := int64(0); i < numReplies; i++ {
|
||||
v, err := ParseReply(rd)
|
||||
v, err := parseReply(rd)
|
||||
if err == Nil {
|
||||
val = append(val, nil)
|
||||
vals = append(vals, nil)
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
val = append(val, v)
|
||||
vals = append(vals, v)
|
||||
}
|
||||
}
|
||||
|
||||
return val, nil
|
||||
return vals, nil
|
||||
} else {
|
||||
vals := make([]string, 0)
|
||||
if len(line) == 2 && line[1] == '0' {
|
||||
return vals, nil
|
||||
}
|
||||
|
||||
numReplies, err := strconv.ParseInt(string(line[1:]), 10, 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for i := int64(0); i < numReplies; i++ {
|
||||
v, err := parseReply(rd)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
vals = append(vals, v.(string))
|
||||
}
|
||||
}
|
||||
|
||||
return vals, nil
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf("redis: can't parse %q", line)
|
||||
}
|
||||
|
|
10
pipeline.go
10
pipeline.go
|
@ -16,6 +16,16 @@ func (c *Client) PipelineClient() (*PipelineClient, error) {
|
|||
}, nil
|
||||
}
|
||||
|
||||
func (c *Client) Pipelined(do func(*PipelineClient)) ([]Req, error) {
|
||||
pc, err := c.PipelineClient()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer pc.Close()
|
||||
do(pc)
|
||||
return pc.RunQueued()
|
||||
}
|
||||
|
||||
func (c *PipelineClient) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
|
16
pubsub.go
16
pubsub.go
|
@ -11,20 +11,16 @@ type PubSubClient struct {
|
|||
once sync.Once
|
||||
}
|
||||
|
||||
func newPubSubClient(client *Client) (*PubSubClient, error) {
|
||||
func (c *Client) PubSubClient() (*PubSubClient, error) {
|
||||
return &PubSubClient{
|
||||
BaseClient: &BaseClient{
|
||||
ConnPool: NewSingleConnPool(client.ConnPool, false),
|
||||
InitConn: client.InitConn,
|
||||
ConnPool: NewSingleConnPool(c.ConnPool, false),
|
||||
InitConn: c.InitConn,
|
||||
},
|
||||
ch: make(chan *Message),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *Client) PubSubClient() (*PubSubClient, error) {
|
||||
return newPubSubClient(c)
|
||||
}
|
||||
|
||||
func (c *Client) Publish(channel, message string) *IntReq {
|
||||
req := NewIntReq("PUBLISH", channel, message)
|
||||
c.Process(req)
|
||||
|
@ -39,7 +35,7 @@ type Message struct {
|
|||
}
|
||||
|
||||
func (c *PubSubClient) consumeMessages(conn *Conn) {
|
||||
req := NewMultiBulkReq()
|
||||
req := NewIfaceSliceReq()
|
||||
|
||||
for {
|
||||
for {
|
||||
|
@ -82,7 +78,7 @@ func (c *PubSubClient) consumeMessages(conn *Conn) {
|
|||
|
||||
func (c *PubSubClient) subscribe(cmd string, channels ...string) (chan *Message, error) {
|
||||
args := append([]string{cmd}, channels...)
|
||||
req := NewMultiBulkReq(args...)
|
||||
req := NewIfaceSliceReq(args...)
|
||||
|
||||
conn, err := c.conn()
|
||||
if err != nil {
|
||||
|
@ -110,7 +106,7 @@ func (c *PubSubClient) PSubscribe(patterns ...string) (chan *Message, error) {
|
|||
|
||||
func (c *PubSubClient) unsubscribe(cmd string, channels ...string) error {
|
||||
args := append([]string{cmd}, channels...)
|
||||
req := NewMultiBulkReq(args...)
|
||||
req := NewIfaceSliceReq(args...)
|
||||
|
||||
conn, err := c.conn()
|
||||
if err != nil {
|
||||
|
|
6
redis.go
6
redis.go
|
@ -57,12 +57,12 @@ type BaseClient struct {
|
|||
}
|
||||
|
||||
func (c *BaseClient) WriteReq(conn *Conn, reqs ...Req) error {
|
||||
conn.ReqBuf = conn.ReqBuf[:0]
|
||||
buf := make([]byte, 0, 1000)
|
||||
for _, req := range reqs {
|
||||
conn.ReqBuf = AppendReq(conn.ReqBuf, req.Args())
|
||||
buf = appendReq(buf, req.Args())
|
||||
}
|
||||
|
||||
_, err := conn.RW.Write(conn.ReqBuf)
|
||||
_, err := conn.RW.Write(buf)
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
851
redis_test.go
851
redis_test.go
File diff suppressed because it is too large
Load Diff
57
request.go
57
request.go
|
@ -6,11 +6,11 @@ import (
|
|||
|
||||
type Req interface {
|
||||
Args() []string
|
||||
ParseReply(ReadLiner) (interface{}, error)
|
||||
ParseReply(reader) (interface{}, error)
|
||||
SetErr(error)
|
||||
Err() error
|
||||
SetVal(interface{})
|
||||
InterfaceVal() interface{}
|
||||
IfaceVal() interface{}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
@ -56,12 +56,12 @@ func (r *BaseReq) SetVal(val interface{}) {
|
|||
r.val = val
|
||||
}
|
||||
|
||||
func (r *BaseReq) InterfaceVal() interface{} {
|
||||
func (r *BaseReq) IfaceVal() interface{} {
|
||||
return r.val
|
||||
}
|
||||
|
||||
func (r *BaseReq) ParseReply(rd ReadLiner) (interface{}, error) {
|
||||
return ParseReply(rd)
|
||||
func (r *BaseReq) ParseReply(rd reader) (interface{}, error) {
|
||||
return parseReply(rd)
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
@ -114,8 +114,8 @@ func NewBoolReq(args ...string) *BoolReq {
|
|||
}
|
||||
}
|
||||
|
||||
func (r *BoolReq) ParseReply(rd ReadLiner) (interface{}, error) {
|
||||
v, err := ParseReply(rd)
|
||||
func (r *BoolReq) ParseReply(rd reader) (interface{}, error) {
|
||||
v, err := parseReply(rd)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -131,17 +131,17 @@ func (r *BoolReq) Val() bool {
|
|||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
type BulkReq struct {
|
||||
type StringReq struct {
|
||||
*BaseReq
|
||||
}
|
||||
|
||||
func NewBulkReq(args ...string) *BulkReq {
|
||||
return &BulkReq{
|
||||
func NewStringReq(args ...string) *StringReq {
|
||||
return &StringReq{
|
||||
BaseReq: NewBaseReq(args...),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *BulkReq) Val() string {
|
||||
func (r *StringReq) Val() string {
|
||||
if r.val == nil {
|
||||
return ""
|
||||
}
|
||||
|
@ -160,8 +160,8 @@ func NewFloatReq(args ...string) *FloatReq {
|
|||
}
|
||||
}
|
||||
|
||||
func (r *FloatReq) ParseReply(rd ReadLiner) (interface{}, error) {
|
||||
v, err := ParseReply(rd)
|
||||
func (r *FloatReq) ParseReply(rd reader) (interface{}, error) {
|
||||
v, err := parseReply(rd)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -177,19 +177,42 @@ func (r *FloatReq) Val() float64 {
|
|||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
type MultiBulkReq struct {
|
||||
type IfaceSliceReq struct {
|
||||
*BaseReq
|
||||
}
|
||||
|
||||
func NewMultiBulkReq(args ...string) *MultiBulkReq {
|
||||
return &MultiBulkReq{
|
||||
func NewIfaceSliceReq(args ...string) *IfaceSliceReq {
|
||||
return &IfaceSliceReq{
|
||||
BaseReq: NewBaseReq(args...),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *MultiBulkReq) Val() []interface{} {
|
||||
func (r *IfaceSliceReq) ParseReply(rd reader) (interface{}, error) {
|
||||
return parseIfaceSliceReply(rd)
|
||||
}
|
||||
|
||||
func (r *IfaceSliceReq) Val() []interface{} {
|
||||
if r.val == nil {
|
||||
return nil
|
||||
}
|
||||
return r.val.([]interface{})
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
type StringSliceReq struct {
|
||||
*BaseReq
|
||||
}
|
||||
|
||||
func NewStringSliceReq(args ...string) *StringSliceReq {
|
||||
return &StringSliceReq{
|
||||
BaseReq: NewBaseReq(args...),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *StringSliceReq) Val() []string {
|
||||
if r.val == nil {
|
||||
return nil
|
||||
}
|
||||
return r.val.([]string)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue