diff --git a/server/cmd_hash.go b/server/cmd_hash.go index b86e339..bce1be2 100644 --- a/server/cmd_hash.go +++ b/server/cmd_hash.go @@ -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) } diff --git a/server/cmd_kv.go b/server/cmd_kv.go index 1c42cec..f70a508 100644 --- a/server/cmd_kv.go +++ b/server/cmd_kv.go @@ -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) } diff --git a/server/cmd_list.go b/server/cmd_list.go index 193e27d..58d64f3 100644 --- a/server/cmd_list.go +++ b/server/cmd_list.go @@ -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) } diff --git a/server/cmd_ttl_test.go b/server/cmd_ttl_test.go new file mode 100644 index 0000000..da90d6c --- /dev/null +++ b/server/cmd_ttl_test.go @@ -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) + } + +} diff --git a/server/cmd_zset.go b/server/cmd_zset.go index c72fa71..dfc24f8 100644 --- a/server/cmd_zset.go +++ b/server/cmd_zset.go @@ -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) }