diff --git a/server/generic.go b/server/generic.go index 113290a..be95de3 100644 --- a/server/generic.go +++ b/server/generic.go @@ -80,3 +80,12 @@ func (s *Server) persist(cmd Command) { cmd.error(err) } } + +func (s *Server) expireTime(cmd Command) { + exp, err := s.DB.ExpireTime(cmd.Args[0]) + if err != nil { + cmd.error(err) + return + } + cmd.write(strconv.FormatInt(exp, 10)) +} diff --git a/server/server.go b/server/server.go index a735cab..d9a461b 100644 --- a/server/server.go +++ b/server/server.go @@ -12,15 +12,16 @@ import ( const ( // Generic commands - CMD_GET = "GET" - CMD_DEL = "DEL" - CMD_SET = "SET" - CMD_KEYS = "KEYS" - CMD_MGET = "MGET" - CMD_SETEX = "SETEX" - CMD_EXISTS = "EXISTS" - CMD_EXPIRE = "EXPIRE" - CMD_PERSIST = "PERSIST" + CMD_GET = "GET" + CMD_DEL = "DEL" + CMD_SET = "SET" + CMD_KEYS = "KEYS" + CMD_MGET = "MGET" + CMD_SETEX = "SETEX" + CMD_EXISTS = "EXISTS" + CMD_EXPIRE = "EXPIRE" + CMD_PERSIST = "PERSIST" + CMD_EXPIRE_TIME = "EXPIRETIME" // Set commands CMD_SADD = "SADD" CMD_SCARD = "SCARD" @@ -112,6 +113,8 @@ func (s *Server) HandleCommand() { s.expire(cmd) case CMD_PERSIST: s.persist(cmd) + case CMD_EXPIRE_TIME: + s.expireTime(cmd) case CMD_SADD: s.sAdd(cmd) case CMD_SCARD: diff --git a/store/generic.go b/store/generic.go index 21fb76d..119e87c 100644 --- a/store/generic.go +++ b/store/generic.go @@ -125,3 +125,16 @@ func (s *Store) Keys() []string { } return keys } + +// Returns the expiration time of a key as Unix timestamp. +func (s *Store) ExpireTime(key string) (int64, error) { + s.Mutex.RLock() + defer s.Mutex.RUnlock() + + value, ok := s.Records[key] + if !ok { + return 0, ErrKeyNotExists + } + expireTime := value.Timestamp.Add(value.Expiration) + return expireTime.Unix(), nil +}