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