Skip to content

Commit

Permalink
Add ExpireTime function
Browse files Browse the repository at this point in the history
Signed-off-by: Devansh Singh <[email protected]>
  • Loading branch information
Devansh3712 committed Feb 6, 2024
1 parent c98282e commit bd26634
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
9 changes: 9 additions & 0 deletions server/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
21 changes: 12 additions & 9 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down
13 changes: 13 additions & 0 deletions store/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit bd26634

Please sign in to comment.