redis/v2/commands.go

1227 lines
28 KiB
Go
Raw Normal View History

2013-07-02 15:17:31 +04:00
package redis
import (
"strconv"
2013-09-11 20:22:10 +04:00
"time"
2013-07-02 15:17:31 +04:00
)
func formatFloat(f float64) string {
return strconv.FormatFloat(f, 'f', -1, 64)
}
2013-09-11 20:22:10 +04:00
func readTimeout(sec int64) time.Duration {
if sec == 0 {
return 0
}
return time.Duration(sec+1) * time.Second
}
2013-07-02 15:17:31 +04:00
//------------------------------------------------------------------------------
2013-09-29 12:06:49 +04:00
func (c *Client) Auth(password string) *StatusCmd {
req := NewStatusCmd("AUTH", password)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) Echo(message string) *StringCmd {
req := NewStringCmd("ECHO", message)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) Ping() *StatusCmd {
req := NewStatusCmd("PING")
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) Quit() *StatusCmd {
2013-07-02 15:17:31 +04:00
panic("not implemented")
}
2013-09-29 12:06:49 +04:00
func (c *Client) Select(index int64) *StatusCmd {
req := NewStatusCmd("SELECT", strconv.FormatInt(index, 10))
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
//------------------------------------------------------------------------------
2013-09-29 12:06:49 +04:00
func (c *Client) Del(keys ...string) *IntCmd {
2013-07-02 15:17:31 +04:00
args := append([]string{"DEL"}, keys...)
2013-09-29 12:06:49 +04:00
req := NewIntCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) Dump(key string) *StringCmd {
req := NewStringCmd("DUMP", key)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) Exists(key string) *BoolCmd {
req := NewBoolCmd("EXISTS", key)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
func (c *Client) Expire(key string, dur time.Duration) *BoolCmd {
req := NewBoolCmd("EXPIRE", key, strconv.FormatInt(int64(dur/time.Second), 10))
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
func (c *Client) ExpireAt(key string, tm time.Time) *BoolCmd {
req := NewBoolCmd("EXPIREAT", key, strconv.FormatInt(tm.Unix(), 10))
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) Keys(pattern string) *StringSliceCmd {
req := NewStringSliceCmd("KEYS", pattern)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) Migrate(host, port, key string, db, timeout int64) *StatusCmd {
req := NewStatusCmd(
2013-07-02 15:17:31 +04:00
"MIGRATE",
host,
port,
key,
strconv.FormatInt(db, 10),
strconv.FormatInt(timeout, 10),
)
2013-09-11 20:22:10 +04:00
req.setReadTimeout(readTimeout(timeout))
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) Move(key string, db int64) *BoolCmd {
req := NewBoolCmd("MOVE", key, strconv.FormatInt(db, 10))
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) ObjectRefCount(keys ...string) *IntCmd {
2013-07-02 15:17:31 +04:00
args := append([]string{"OBJECT", "REFCOUNT"}, keys...)
2013-09-29 12:06:49 +04:00
req := NewIntCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) ObjectEncoding(keys ...string) *StringCmd {
2013-07-02 15:17:31 +04:00
args := append([]string{"OBJECT", "ENCODING"}, keys...)
2013-09-29 12:06:49 +04:00
req := NewStringCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
func (c *Client) ObjectIdleTime(keys ...string) *DurationCmd {
2013-07-02 15:17:31 +04:00
args := append([]string{"OBJECT", "IDLETIME"}, keys...)
req := NewDurationCmd(time.Second, args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) Persist(key string) *BoolCmd {
req := NewBoolCmd("PERSIST", key)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
func (c *Client) PExpire(key string, dur time.Duration) *BoolCmd {
req := NewBoolCmd("PEXPIRE", key, strconv.FormatInt(int64(dur/time.Millisecond), 10))
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
func (c *Client) PExpireAt(key string, tm time.Time) *BoolCmd {
req := NewBoolCmd(
"PEXPIREAT",
key,
strconv.FormatInt(tm.UnixNano()/int64(time.Millisecond), 10),
)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
func (c *Client) PTTL(key string) *DurationCmd {
req := NewDurationCmd(time.Millisecond, "PTTL", key)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) RandomKey() *StringCmd {
req := NewStringCmd("RANDOMKEY")
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) Rename(key, newkey string) *StatusCmd {
req := NewStatusCmd("RENAME", key, newkey)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) RenameNX(key, newkey string) *BoolCmd {
req := NewBoolCmd("RENAMENX", key, newkey)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) Restore(key string, ttl int64, value string) *StatusCmd {
req := NewStatusCmd(
2013-07-02 15:17:31 +04:00
"RESTORE",
key,
strconv.FormatInt(ttl, 10),
value,
)
c.Process(req)
return req
}
type Sort struct {
By string
Offset, Count float64
Get []string
Order string
IsAlpha bool
Store string
}
2013-09-29 12:06:49 +04:00
func (c *Client) Sort(key string, sort Sort) *StringSliceCmd {
2013-07-02 15:17:31 +04:00
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)
}
2013-09-29 12:06:49 +04:00
req := NewStringSliceCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
func (c *Client) TTL(key string) *DurationCmd {
req := NewDurationCmd(time.Second, "TTL", key)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) Type(key string) *StatusCmd {
req := NewStatusCmd("TYPE", key)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2014-01-09 12:17:38 +04:00
func (c *Client) Scan(cursor int64, match string, count int64) *ScanCmd {
args := []string{"SCAN", strconv.FormatInt(cursor, 10)}
if match != "" {
args = append(args, "MATCH", match)
}
if count > 0 {
args = append(args, "COUNT", strconv.FormatInt(count, 10))
}
req := NewScanCmd(args...)
c.Process(req)
return req
}
func (c *Client) SScan(key string, cursor int64, match string, count int64) *ScanCmd {
args := []string{"SSCAN", key, strconv.FormatInt(cursor, 10)}
if match != "" {
args = append(args, "MATCH", match)
}
if count > 0 {
args = append(args, "COUNT", strconv.FormatInt(count, 10))
}
req := NewScanCmd(args...)
c.Process(req)
return req
}
func (c *Client) HScan(key string, cursor int64, match string, count int64) *ScanCmd {
args := []string{"HSCAN", key, strconv.FormatInt(cursor, 10)}
if match != "" {
args = append(args, "MATCH", match)
}
if count > 0 {
args = append(args, "COUNT", strconv.FormatInt(count, 10))
}
req := NewScanCmd(args...)
c.Process(req)
return req
}
func (c *Client) ZScan(key string, cursor int64, match string, count int64) *ScanCmd {
args := []string{"ZSCAN", key, strconv.FormatInt(cursor, 10)}
if match != "" {
args = append(args, "MATCH", match)
}
if count > 0 {
args = append(args, "COUNT", strconv.FormatInt(count, 10))
}
req := NewScanCmd(args...)
c.Process(req)
return req
}
2014-01-06 18:06:30 +04:00
2013-07-02 15:17:31 +04:00
//------------------------------------------------------------------------------
2013-09-29 12:06:49 +04:00
func (c *Client) Append(key, value string) *IntCmd {
req := NewIntCmd("APPEND", key, value)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
type BitCount struct {
Start, End int64
}
2013-09-29 12:06:49 +04:00
func (c *Client) BitCount(key string, bitCount *BitCount) *IntCmd {
2013-07-02 15:17:31 +04:00
args := []string{"BITCOUNT", key}
if bitCount != nil {
args = append(
args,
strconv.FormatInt(bitCount.Start, 10),
strconv.FormatInt(bitCount.End, 10),
)
}
2013-09-29 12:06:49 +04:00
req := NewIntCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) bitOp(op, destKey string, keys ...string) *IntCmd {
2013-07-02 15:17:31 +04:00
args := []string{"BITOP", op, destKey}
args = append(args, keys...)
2013-09-29 12:06:49 +04:00
req := NewIntCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) BitOpAnd(destKey string, keys ...string) *IntCmd {
2013-07-02 15:17:31 +04:00
return c.bitOp("AND", destKey, keys...)
}
2013-09-29 12:06:49 +04:00
func (c *Client) BitOpOr(destKey string, keys ...string) *IntCmd {
2013-07-02 15:17:31 +04:00
return c.bitOp("OR", destKey, keys...)
}
2013-09-29 12:06:49 +04:00
func (c *Client) BitOpXor(destKey string, keys ...string) *IntCmd {
2013-07-02 15:17:31 +04:00
return c.bitOp("XOR", destKey, keys...)
}
2013-09-29 12:06:49 +04:00
func (c *Client) BitOpNot(destKey string, key string) *IntCmd {
2013-07-02 15:17:31 +04:00
return c.bitOp("NOT", destKey, key)
}
2013-09-29 12:06:49 +04:00
func (c *Client) Decr(key string) *IntCmd {
req := NewIntCmd("DECR", key)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) DecrBy(key string, decrement int64) *IntCmd {
req := NewIntCmd("DECRBY", key, strconv.FormatInt(decrement, 10))
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) Get(key string) *StringCmd {
req := NewStringCmd("GET", key)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) GetBit(key string, offset int64) *IntCmd {
req := NewIntCmd("GETBIT", key, strconv.FormatInt(offset, 10))
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) GetRange(key string, start, end int64) *StringCmd {
req := NewStringCmd(
2013-07-02 15:17:31 +04:00
"GETRANGE",
key,
strconv.FormatInt(start, 10),
strconv.FormatInt(end, 10),
)
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) GetSet(key, value string) *StringCmd {
req := NewStringCmd("GETSET", key, value)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) Incr(key string) *IntCmd {
req := NewIntCmd("INCR", key)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) IncrBy(key string, value int64) *IntCmd {
req := NewIntCmd("INCRBY", key, strconv.FormatInt(value, 10))
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) IncrByFloat(key string, value float64) *FloatCmd {
req := NewFloatCmd("INCRBYFLOAT", key, formatFloat(value))
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) MGet(keys ...string) *SliceCmd {
2013-07-02 15:17:31 +04:00
args := append([]string{"MGET"}, keys...)
2013-09-29 12:06:49 +04:00
req := NewSliceCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) MSet(pairs ...string) *StatusCmd {
2013-07-02 15:17:31 +04:00
args := append([]string{"MSET"}, pairs...)
2013-09-29 12:06:49 +04:00
req := NewStatusCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) MSetNX(pairs ...string) *BoolCmd {
2013-07-02 15:17:31 +04:00
args := append([]string{"MSETNX"}, pairs...)
2013-09-29 12:06:49 +04:00
req := NewBoolCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
func (c *Client) PSetEx(key string, dur time.Duration, value string) *StatusCmd {
2013-09-29 12:06:49 +04:00
req := NewStatusCmd(
2013-07-02 15:17:31 +04:00
"PSETEX",
key,
strconv.FormatInt(int64(dur/time.Millisecond), 10),
2013-07-02 15:17:31 +04:00
value,
)
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) Set(key, value string) *StatusCmd {
req := NewStatusCmd("SET", key, value)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) SetBit(key string, offset int64, value int) *IntCmd {
req := NewIntCmd(
2013-07-02 15:17:31 +04:00
"SETBIT",
key,
strconv.FormatInt(offset, 10),
strconv.FormatInt(int64(value), 10),
)
c.Process(req)
return req
}
func (c *Client) SetEx(key string, dur time.Duration, value string) *StatusCmd {
req := NewStatusCmd("SETEX", key, strconv.FormatInt(int64(dur/time.Second), 10), value)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) SetNX(key, value string) *BoolCmd {
req := NewBoolCmd("SETNX", key, value)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) SetRange(key string, offset int64, value string) *IntCmd {
req := NewIntCmd("SETRANGE", key, strconv.FormatInt(offset, 10), value)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) StrLen(key string) *IntCmd {
req := NewIntCmd("STRLEN", key)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
//------------------------------------------------------------------------------
2013-09-29 12:06:49 +04:00
func (c *Client) HDel(key string, fields ...string) *IntCmd {
2013-07-02 15:17:31 +04:00
args := append([]string{"HDEL", key}, fields...)
2013-09-29 12:06:49 +04:00
req := NewIntCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) HExists(key, field string) *BoolCmd {
req := NewBoolCmd("HEXISTS", key, field)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) HGet(key, field string) *StringCmd {
req := NewStringCmd("HGET", key, field)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) HGetAll(key string) *StringSliceCmd {
req := NewStringSliceCmd("HGETALL", key)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) HGetAllMap(key string) *StringStringMapCmd {
req := NewStringStringMapCmd("HGETALL", key)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) HIncrBy(key, field string, incr int64) *IntCmd {
req := NewIntCmd("HINCRBY", key, field, strconv.FormatInt(incr, 10))
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) HIncrByFloat(key, field string, incr float64) *FloatCmd {
req := NewFloatCmd("HINCRBYFLOAT", key, field, formatFloat(incr))
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) HKeys(key string) *StringSliceCmd {
req := NewStringSliceCmd("HKEYS", key)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) HLen(key string) *IntCmd {
req := NewIntCmd("HLEN", key)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) HMGet(key string, fields ...string) *SliceCmd {
2013-07-02 15:17:31 +04:00
args := append([]string{"HMGET", key}, fields...)
2013-09-29 12:06:49 +04:00
req := NewSliceCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) HMSet(key, field, value string, pairs ...string) *StatusCmd {
2013-07-02 15:17:31 +04:00
args := append([]string{"HMSET", key, field, value}, pairs...)
2013-09-29 12:06:49 +04:00
req := NewStatusCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) HSet(key, field, value string) *BoolCmd {
req := NewBoolCmd("HSET", key, field, value)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) HSetNX(key, field, value string) *BoolCmd {
req := NewBoolCmd("HSETNX", key, field, value)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) HVals(key string) *StringSliceCmd {
req := NewStringSliceCmd("HVALS", key)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
//------------------------------------------------------------------------------
2013-09-29 12:06:49 +04:00
func (c *Client) BLPop(timeout int64, keys ...string) *StringSliceCmd {
2013-07-02 15:17:31 +04:00
args := append([]string{"BLPOP"}, keys...)
args = append(args, strconv.FormatInt(timeout, 10))
2013-09-29 12:06:49 +04:00
req := NewStringSliceCmd(args...)
2013-09-11 20:22:10 +04:00
req.setReadTimeout(readTimeout(timeout))
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) BRPop(timeout int64, keys ...string) *StringSliceCmd {
2013-07-02 15:17:31 +04:00
args := append([]string{"BRPOP"}, keys...)
args = append(args, strconv.FormatInt(timeout, 10))
2013-09-29 12:06:49 +04:00
req := NewStringSliceCmd(args...)
2013-09-11 20:22:10 +04:00
req.setReadTimeout(readTimeout(timeout))
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) BRPopLPush(source, destination string, timeout int64) *StringCmd {
req := NewStringCmd(
2013-07-02 15:17:31 +04:00
"BRPOPLPUSH",
source,
destination,
strconv.FormatInt(timeout, 10),
)
2013-09-11 20:22:10 +04:00
req.setReadTimeout(readTimeout(timeout))
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) LIndex(key string, index int64) *StringCmd {
req := NewStringCmd("LINDEX", key, strconv.FormatInt(index, 10))
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) LInsert(key, op, pivot, value string) *IntCmd {
req := NewIntCmd("LINSERT", key, op, pivot, value)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) LLen(key string) *IntCmd {
req := NewIntCmd("LLEN", key)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) LPop(key string) *StringCmd {
req := NewStringCmd("LPOP", key)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) LPush(key string, values ...string) *IntCmd {
2013-07-02 15:17:31 +04:00
args := append([]string{"LPUSH", key}, values...)
2013-09-29 12:06:49 +04:00
req := NewIntCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) LPushX(key, value string) *IntCmd {
req := NewIntCmd("LPUSHX", key, value)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) LRange(key string, start, stop int64) *StringSliceCmd {
req := NewStringSliceCmd(
2013-07-02 15:17:31 +04:00
"LRANGE",
key,
strconv.FormatInt(start, 10),
strconv.FormatInt(stop, 10),
)
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) LRem(key string, count int64, value string) *IntCmd {
req := NewIntCmd("LREM", key, strconv.FormatInt(count, 10), value)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) LSet(key string, index int64, value string) *StatusCmd {
req := NewStatusCmd("LSET", key, strconv.FormatInt(index, 10), value)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) LTrim(key string, start, stop int64) *StatusCmd {
req := NewStatusCmd(
2013-07-02 15:17:31 +04:00
"LTRIM",
key,
strconv.FormatInt(start, 10),
strconv.FormatInt(stop, 10),
)
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) RPop(key string) *StringCmd {
req := NewStringCmd("RPOP", key)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) RPopLPush(source, destination string) *StringCmd {
req := NewStringCmd("RPOPLPUSH", source, destination)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) RPush(key string, values ...string) *IntCmd {
2013-07-02 15:17:31 +04:00
args := append([]string{"RPUSH", key}, values...)
2013-09-29 12:06:49 +04:00
req := NewIntCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) RPushX(key string, value string) *IntCmd {
req := NewIntCmd("RPUSHX", key, value)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
//------------------------------------------------------------------------------
2013-09-29 12:06:49 +04:00
func (c *Client) SAdd(key string, members ...string) *IntCmd {
2013-07-02 15:17:31 +04:00
args := append([]string{"SADD", key}, members...)
2013-09-29 12:06:49 +04:00
req := NewIntCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) SCard(key string) *IntCmd {
req := NewIntCmd("SCARD", key)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) SDiff(keys ...string) *StringSliceCmd {
2013-07-02 15:17:31 +04:00
args := append([]string{"SDIFF"}, keys...)
2013-09-29 12:06:49 +04:00
req := NewStringSliceCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) SDiffStore(destination string, keys ...string) *IntCmd {
2013-07-02 15:17:31 +04:00
args := append([]string{"SDIFFSTORE", destination}, keys...)
2013-09-29 12:06:49 +04:00
req := NewIntCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) SInter(keys ...string) *StringSliceCmd {
2013-07-02 15:17:31 +04:00
args := append([]string{"SINTER"}, keys...)
2013-09-29 12:06:49 +04:00
req := NewStringSliceCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) SInterStore(destination string, keys ...string) *IntCmd {
2013-07-02 15:17:31 +04:00
args := append([]string{"SINTERSTORE", destination}, keys...)
2013-09-29 12:06:49 +04:00
req := NewIntCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) SIsMember(key, member string) *BoolCmd {
req := NewBoolCmd("SISMEMBER", key, member)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) SMembers(key string) *StringSliceCmd {
req := NewStringSliceCmd("SMEMBERS", key)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) SMove(source, destination, member string) *BoolCmd {
req := NewBoolCmd("SMOVE", source, destination, member)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) SPop(key string) *StringCmd {
req := NewStringCmd("SPOP", key)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) SRandMember(key string) *StringCmd {
req := NewStringCmd("SRANDMEMBER", key)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) SRem(key string, members ...string) *IntCmd {
2013-07-02 15:17:31 +04:00
args := append([]string{"SREM", key}, members...)
2013-09-29 12:06:49 +04:00
req := NewIntCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) SUnion(keys ...string) *StringSliceCmd {
2013-07-02 15:17:31 +04:00
args := append([]string{"SUNION"}, keys...)
2013-09-29 12:06:49 +04:00
req := NewStringSliceCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) SUnionStore(destination string, keys ...string) *IntCmd {
2013-07-02 15:17:31 +04:00
args := append([]string{"SUNIONSTORE", destination}, keys...)
2013-09-29 12:06:49 +04:00
req := NewIntCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
//------------------------------------------------------------------------------
type Z struct {
Score float64
Member string
}
type ZStore struct {
Weights []int64
Aggregate string
}
2013-09-29 12:06:49 +04:00
func (c *Client) ZAdd(key string, members ...Z) *IntCmd {
2013-07-02 15:17:31 +04:00
args := []string{"ZADD", key}
for _, m := range members {
args = append(args, formatFloat(m.Score), m.Member)
}
2013-09-29 12:06:49 +04:00
req := NewIntCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) ZCard(key string) *IntCmd {
req := NewIntCmd("ZCARD", key)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) ZCount(key, min, max string) *IntCmd {
req := NewIntCmd("ZCOUNT", key, min, max)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) ZIncrBy(key string, increment float64, member string) *FloatCmd {
req := NewFloatCmd("ZINCRBY", key, formatFloat(increment), member)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
func (c *Client) ZInterStore(
destination string,
store ZStore,
keys ...string,
2013-09-29 12:06:49 +04:00
) *IntCmd {
2013-07-02 15:17:31 +04:00
args := []string{"ZINTERSTORE", destination, strconv.FormatInt(int64(len(keys)), 10)}
args = append(args, keys...)
if len(store.Weights) > 0 {
args = append(args, "WEIGHTS")
for _, weight := range store.Weights {
args = append(args, strconv.FormatInt(weight, 10))
}
}
if store.Aggregate != "" {
args = append(args, "AGGREGATE", store.Aggregate)
}
2013-09-29 12:06:49 +04:00
req := NewIntCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) zRange(key string, start, stop int64, withScores bool) *StringSliceCmd {
2013-07-02 15:17:31 +04:00
args := []string{
"ZRANGE",
key,
strconv.FormatInt(start, 10),
strconv.FormatInt(stop, 10),
}
if withScores {
args = append(args, "WITHSCORES")
}
2013-09-29 12:06:49 +04:00
req := NewStringSliceCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) ZRange(key string, start, stop int64) *StringSliceCmd {
2013-07-02 15:17:31 +04:00
return c.zRange(key, start, stop, false)
}
2013-09-29 12:06:49 +04:00
func (c *Client) ZRangeWithScores(key string, start, stop int64) *StringSliceCmd {
2013-07-02 15:17:31 +04:00
return c.zRange(key, start, stop, true)
}
2013-09-29 12:06:49 +04:00
func (c *Client) ZRangeWithScoresMap(key string, start, stop int64) *StringFloatMapCmd {
2013-07-02 15:17:31 +04:00
args := []string{
"ZRANGE",
key,
strconv.FormatInt(start, 10),
strconv.FormatInt(stop, 10),
"WITHSCORES",
}
2013-09-29 12:06:49 +04:00
req := NewStringFloatMapCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-10-15 12:18:16 +04:00
type ZRangeByScore struct {
Min, Max string
Offset, Count int64
}
func (c *Client) zRangeByScore(key string, opt ZRangeByScore, withScores bool) *StringSliceCmd {
args := []string{"ZRANGEBYSCORE", key, opt.Min, opt.Max}
2013-07-02 15:17:31 +04:00
if withScores {
args = append(args, "WITHSCORES")
}
if opt.Offset != 0 || opt.Count != 0 {
2013-07-02 15:17:31 +04:00
args = append(
args,
"LIMIT",
strconv.FormatInt(opt.Offset, 10),
strconv.FormatInt(opt.Count, 10),
2013-07-02 15:17:31 +04:00
)
}
2013-09-29 12:06:49 +04:00
req := NewStringSliceCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
func (c *Client) ZRangeByScore(key string, opt ZRangeByScore) *StringSliceCmd {
return c.zRangeByScore(key, opt, false)
2013-07-02 15:17:31 +04:00
}
func (c *Client) ZRangeByScoreWithScores(key string, opt ZRangeByScore) *StringSliceCmd {
return c.zRangeByScore(key, opt, true)
2013-07-02 15:17:31 +04:00
}
func (c *Client) ZRangeByScoreWithScoresMap(key string, opt ZRangeByScore) *StringFloatMapCmd {
args := []string{"ZRANGEBYSCORE", key, opt.Min, opt.Max, "WITHSCORES"}
if opt.Offset != 0 || opt.Count != 0 {
2013-07-02 15:17:31 +04:00
args = append(
args,
"LIMIT",
strconv.FormatInt(opt.Offset, 10),
strconv.FormatInt(opt.Count, 10),
2013-07-02 15:17:31 +04:00
)
}
2013-09-29 12:06:49 +04:00
req := NewStringFloatMapCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) ZRank(key, member string) *IntCmd {
req := NewIntCmd("ZRANK", key, member)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) ZRem(key string, members ...string) *IntCmd {
2013-07-02 15:17:31 +04:00
args := append([]string{"ZREM", key}, members...)
2013-09-29 12:06:49 +04:00
req := NewIntCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) ZRemRangeByRank(key string, start, stop int64) *IntCmd {
req := NewIntCmd(
2013-07-02 15:17:31 +04:00
"ZREMRANGEBYRANK",
key,
strconv.FormatInt(start, 10),
strconv.FormatInt(stop, 10),
)
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) ZRemRangeByScore(key, min, max string) *IntCmd {
req := NewIntCmd("ZREMRANGEBYSCORE", key, min, max)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) zRevRange(key, start, stop string, withScores bool) *StringSliceCmd {
2013-07-02 15:17:31 +04:00
args := []string{"ZREVRANGE", key, start, stop}
if withScores {
args = append(args, "WITHSCORES")
}
2013-09-29 12:06:49 +04:00
req := NewStringSliceCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) ZRevRange(key, start, stop string) *StringSliceCmd {
2013-07-02 15:17:31 +04:00
return c.zRevRange(key, start, stop, false)
}
2013-09-29 12:06:49 +04:00
func (c *Client) ZRevRangeWithScores(key, start, stop string) *StringSliceCmd {
2013-07-02 15:17:31 +04:00
return c.zRevRange(key, start, stop, true)
}
2013-09-29 12:06:49 +04:00
func (c *Client) ZRevRangeWithScoresMap(key, start, stop string) *StringFloatMapCmd {
2013-07-02 15:17:31 +04:00
args := []string{"ZREVRANGE", key, start, stop, "WITHSCORES"}
2013-09-29 12:06:49 +04:00
req := NewStringFloatMapCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
func (c *Client) zRevRangeByScore(key string, opt ZRangeByScore, withScores bool) *StringSliceCmd {
args := []string{"ZREVRANGEBYSCORE", key, opt.Max, opt.Min}
2013-07-02 15:17:31 +04:00
if withScores {
args = append(args, "WITHSCORES")
}
if opt.Offset != 0 || opt.Count != 0 {
2013-07-02 15:17:31 +04:00
args = append(
args,
"LIMIT",
strconv.FormatInt(opt.Offset, 10),
strconv.FormatInt(opt.Count, 10),
2013-07-02 15:17:31 +04:00
)
}
2013-09-29 12:06:49 +04:00
req := NewStringSliceCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
func (c *Client) ZRevRangeByScore(key string, opt ZRangeByScore) *StringSliceCmd {
return c.zRevRangeByScore(key, opt, false)
2013-07-02 15:17:31 +04:00
}
func (c *Client) ZRevRangeByScoreWithScores(key string, opt ZRangeByScore) *StringSliceCmd {
return c.zRevRangeByScore(key, opt, true)
2013-07-02 15:17:31 +04:00
}
func (c *Client) ZRevRangeByScoreWithScoresMap(key string, opt ZRangeByScore) *StringFloatMapCmd {
args := []string{"ZREVRANGEBYSCORE", key, opt.Max, opt.Min, "WITHSCORES"}
if opt.Offset != 0 || opt.Count != 0 {
2013-07-02 15:17:31 +04:00
args = append(
args,
"LIMIT",
strconv.FormatInt(opt.Offset, 10),
strconv.FormatInt(opt.Count, 10),
2013-07-02 15:17:31 +04:00
)
}
2013-09-29 12:06:49 +04:00
req := NewStringFloatMapCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) ZRevRank(key, member string) *IntCmd {
req := NewIntCmd("ZREVRANK", key, member)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) ZScore(key, member string) *FloatCmd {
req := NewFloatCmd("ZSCORE", key, member)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
func (c *Client) ZUnionStore(
destination string,
store ZStore,
keys ...string,
2013-09-29 12:06:49 +04:00
) *IntCmd {
2013-07-02 15:17:31 +04:00
args := []string{"ZUNIONSTORE", destination, strconv.FormatInt(int64(len(keys)), 10)}
args = append(args, keys...)
if len(store.Weights) > 0 {
args = append(args, "WEIGHTS")
for _, weight := range store.Weights {
args = append(args, strconv.FormatInt(weight, 10))
}
}
if store.Aggregate != "" {
args = append(args, "AGGREGATE", store.Aggregate)
}
2013-09-29 12:06:49 +04:00
req := NewIntCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
//------------------------------------------------------------------------------
2013-09-29 12:06:49 +04:00
func (c *Client) BgRewriteAOF() *StatusCmd {
req := NewStatusCmd("BGREWRITEAOF")
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) BgSave() *StatusCmd {
req := NewStatusCmd("BGSAVE")
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) ClientKill(ipPort string) *StatusCmd {
req := NewStatusCmd("CLIENT", "KILL", ipPort)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) ClientList() *StringCmd {
req := NewStringCmd("CLIENT", "LIST")
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) ConfigGet(parameter string) *SliceCmd {
req := NewSliceCmd("CONFIG", "GET", parameter)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) ConfigResetStat() *StatusCmd {
req := NewStatusCmd("CONFIG", "RESETSTAT")
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) ConfigSet(parameter, value string) *StatusCmd {
req := NewStatusCmd("CONFIG", "SET", parameter, value)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) DbSize() *IntCmd {
req := NewIntCmd("DBSIZE")
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) FlushAll() *StatusCmd {
req := NewStatusCmd("FLUSHALL")
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) FlushDb() *StatusCmd {
req := NewStatusCmd("FLUSHDB")
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) Info() *StringCmd {
req := NewStringCmd("INFO")
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) LastSave() *IntCmd {
req := NewIntCmd("LASTSAVE")
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) Save() *StatusCmd {
req := NewStatusCmd("SAVE")
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) shutdown(modifier string) *StatusCmd {
2013-07-02 15:17:31 +04:00
var args []string
if modifier == "" {
args = []string{"SHUTDOWN"}
} else {
args = []string{"SHUTDOWN", modifier}
}
2013-09-29 12:06:49 +04:00
req := NewStatusCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
c.Close()
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) Shutdown() *StatusCmd {
2013-07-02 15:17:31 +04:00
return c.shutdown("")
}
2013-09-29 12:06:49 +04:00
func (c *Client) ShutdownSave() *StatusCmd {
2013-07-02 15:17:31 +04:00
return c.shutdown("SAVE")
}
2013-09-29 12:06:49 +04:00
func (c *Client) ShutdownNoSave() *StatusCmd {
2013-07-02 15:17:31 +04:00
return c.shutdown("NOSAVE")
}
2013-09-29 12:06:49 +04:00
func (c *Client) SlaveOf(host, port string) *StatusCmd {
req := NewStatusCmd("SLAVEOF", host, port)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
func (c *Client) SlowLog() {
panic("not implemented")
}
func (c *Client) Sync() {
panic("not implemented")
}
2013-09-29 12:06:49 +04:00
func (c *Client) Time() *StringSliceCmd {
req := NewStringSliceCmd("TIME")
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
//------------------------------------------------------------------------------
2013-09-29 12:06:49 +04:00
func (c *Client) Eval(script string, keys []string, args []string) *Cmd {
2013-07-02 15:17:31 +04:00
reqArgs := []string{"EVAL", script, strconv.FormatInt(int64(len(keys)), 10)}
reqArgs = append(reqArgs, keys...)
reqArgs = append(reqArgs, args...)
2013-09-29 12:06:49 +04:00
req := NewCmd(reqArgs...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) EvalSha(sha1 string, keys []string, args []string) *Cmd {
2013-07-02 15:17:31 +04:00
reqArgs := []string{"EVALSHA", sha1, strconv.FormatInt(int64(len(keys)), 10)}
reqArgs = append(reqArgs, keys...)
reqArgs = append(reqArgs, args...)
2013-09-29 12:06:49 +04:00
req := NewCmd(reqArgs...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) ScriptExists(scripts ...string) *BoolSliceCmd {
2013-07-02 15:17:31 +04:00
args := append([]string{"SCRIPT", "EXISTS"}, scripts...)
2013-09-29 12:06:49 +04:00
req := NewBoolSliceCmd(args...)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) ScriptFlush() *StatusCmd {
req := NewStatusCmd("SCRIPT", "FLUSH")
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) ScriptKill() *StatusCmd {
req := NewStatusCmd("SCRIPT", "KILL")
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2013-09-29 12:06:49 +04:00
func (c *Client) ScriptLoad(script string) *StringCmd {
req := NewStringCmd("SCRIPT", "LOAD", script)
2013-07-02 15:17:31 +04:00
c.Process(req)
return req
}
2014-01-06 15:46:10 +04:00
//------------------------------------------------------------------------------
func (c *Client) DebugObject(key string) *StringCmd {
req := NewStringCmd("DEBUG", "OBJECT", key)
c.Process(req)
return req
}