add server commands of expire/ttl

This commit is contained in:
silentsai 2014-06-16 19:24:37 +08:00
parent 7cfd84b20b
commit 852fce9f4c
5 changed files with 300 additions and 2 deletions

View File

@ -207,6 +207,61 @@ func hclearCommand(c *client) error {
return nil
}
func hexpireCommand(c *client) error {
args := c.args
if len(args) == 0 {
return ErrCmdParams
}
duration, err := ledis.StrInt64(args[1], nil)
if err != nil {
return err
}
if v, err := c.db.HExpire(args[0], duration); err != nil {
return err
} else {
c.writeInteger(v)
}
return nil
}
func hexpireAtCommand(c *client) error {
args := c.args
if len(args) == 0 {
return ErrCmdParams
}
when, err := ledis.StrInt64(args[1], nil)
if err != nil {
return err
}
if v, err := c.db.HExpireAt(args[0], when); err != nil {
return err
} else {
c.writeInteger(v)
}
return nil
}
func httlCommand(c *client) error {
args := c.args
if len(args) == 0 {
return ErrCmdParams
}
if v, err := c.db.HTTL(args[0]); err != nil {
return err
} else {
c.writeInteger(v)
}
return nil
}
func init() {
register("hdel", hdelCommand)
register("hexists", hexistsCommand)
@ -223,4 +278,7 @@ func init() {
//ledisdb special command
register("hclear", hclearCommand)
register("hexpire", hexpireCommand)
register("hexpireat", hexpireAtCommand)
register("httl", httlCommand)
}

View File

@ -203,6 +203,65 @@ func mgetCommand(c *client) error {
return nil
}
func expireCommand(c *client) error {
args := c.args
if len(args) == 0 {
return ErrCmdParams
}
duration, err := ledis.StrInt64(args[1], nil)
if err != nil {
return err
}
if v, err := c.db.Expire(args[0], duration); err != nil {
return err
} else {
c.writeInteger(v)
}
return nil
}
func expireAtCommand(c *client) error {
args := c.args
if len(args) == 0 {
return ErrCmdParams
}
when, err := ledis.StrInt64(args[1], nil)
if err != nil {
return err
}
if v, err := c.db.ExpireAt(args[0], when); err != nil {
return err
} else {
c.writeInteger(v)
}
return nil
}
func ttlCommand(c *client) error {
args := c.args
if len(args) == 0 {
return ErrCmdParams
}
if v, err := c.db.TTL(args[0]); err != nil {
return err
} else {
c.writeInteger(v)
}
return nil
}
// func (db *DB) Expire(key []byte, duration int6
// func (db *DB) ExpireAt(key []byte, when int64)
// func (db *DB) TTL(key []byte) (int64, error)
func init() {
register("decr", decrCommand)
register("decrby", decrbyCommand)
@ -216,4 +275,7 @@ func init() {
register("mset", msetCommand)
register("set", setCommand)
register("setnx", setnxCommand)
register("expire", expireCommand)
register("expireat", expireAtCommand)
register("ttl", ttlCommand)
}

View File

@ -143,6 +143,61 @@ func lclearCommand(c *client) error {
return nil
}
func lexpireCommand(c *client) error {
args := c.args
if len(args) == 0 {
return ErrCmdParams
}
duration, err := ledis.StrInt64(args[1], nil)
if err != nil {
return err
}
if v, err := c.db.LExpire(args[0], duration); err != nil {
return err
} else {
c.writeInteger(v)
}
return nil
}
func lexpireAtCommand(c *client) error {
args := c.args
if len(args) == 0 {
return ErrCmdParams
}
when, err := ledis.StrInt64(args[1], nil)
if err != nil {
return err
}
if v, err := c.db.LExpireAt(args[0], when); err != nil {
return err
} else {
c.writeInteger(v)
}
return nil
}
func lttlCommand(c *client) error {
args := c.args
if len(args) == 0 {
return ErrCmdParams
}
if v, err := c.db.LTTL(args[0]); err != nil {
return err
} else {
c.writeInteger(v)
}
return nil
}
func init() {
register("lindex", lindexCommand)
register("llen", llenCommand)
@ -155,5 +210,7 @@ func init() {
//ledisdb special command
register("lclear", lclearCommand)
register("lexpire", lexpireCommand)
register("lexpireat", lexpireAtCommand)
register("lttl", lttlCommand)
}

63
server/cmd_ttl_test.go Normal file
View File

@ -0,0 +1,63 @@
package server
import (
"github.com/garyburd/redigo/redis"
"testing"
"time"
)
func now() int64 {
return time.Now().Unix()
}
func TestKVExpire(t *testing.T) {
c := getTestConn()
defer c.Close()
k := "a_ttl"
c.Do("set", k, "123")
// expire + ttl
exp := int64(10)
if n, err := redis.Int(c.Do("expire", k, exp)); err != nil {
t.Fatal(err)
} else if n != 1 {
t.Fatal(n)
}
if ttl, err := redis.Int64(c.Do("ttl", k)); err != nil {
t.Fatal(err)
} else if ttl != exp {
t.Fatal(ttl)
}
// expireat + ttl
tm := now() + 3
if n, err := redis.Int(c.Do("expireat", k, tm)); err != nil {
t.Fatal(err)
} else if n != 1 {
t.Fatal(n)
}
if ttl, err := redis.Int64(c.Do("ttl", k)); err != nil {
t.Fatal(err)
} else if ttl != 3 {
t.Fatal(ttl)
}
kErr := "not_exist_ttl"
// err - expire, expireat
if n, err := redis.Int(c.Do("expire", kErr, tm)); err != nil || n != 0 {
t.Fatal(false)
}
if n, err := redis.Int(c.Do("expireat", kErr, tm)); err != nil || n != 0 {
t.Fatal(false)
}
if n, err := redis.Int(c.Do("ttl", kErr)); err != nil || n != -1 {
t.Fatal(false)
}
}

View File

@ -421,6 +421,61 @@ func zclearCommand(c *client) error {
return nil
}
func zexpireCommand(c *client) error {
args := c.args
if len(args) == 0 {
return ErrCmdParams
}
duration, err := ledis.StrInt64(args[1], nil)
if err != nil {
return err
}
if v, err := c.db.ZExpire(args[0], duration); err != nil {
return err
} else {
c.writeInteger(v)
}
return nil
}
func zexpireAtCommand(c *client) error {
args := c.args
if len(args) == 0 {
return ErrCmdParams
}
when, err := ledis.StrInt64(args[1], nil)
if err != nil {
return err
}
if v, err := c.db.ZExpireAt(args[0], when); err != nil {
return err
} else {
c.writeInteger(v)
}
return nil
}
func zttlCommand(c *client) error {
args := c.args
if len(args) == 0 {
return ErrCmdParams
}
if v, err := c.db.ZTTL(args[0]); err != nil {
return err
} else {
c.writeInteger(v)
}
return nil
}
func init() {
register("zadd", zaddCommand)
register("zcard", zcardCommand)
@ -438,6 +493,9 @@ func init() {
register("zscore", zscoreCommand)
//ledisdb special command
register("zclear", zclearCommand)
register("zclear", zclearCommand)
register("zexpire", zexpireCommand)
register("zexpireat", zexpireAtCommand)
register("zttl", zttlCommand)
}