I’m stuck on Stage #YZ1.
I’ve tried … (mention what you’ve tried so far).
I create a tester of my own in golang and tried to test if this is working or not it is working well and the i am able to set the Expiry time also but when codecrafter tester is setting the key the expiry time is becoming 0
Here are my logs:
[Uploading: image.png…]
In the logs you can observe the expiry time is being set to 0000 and the expiry time is not being added
And here’s a snippet of my code:
func executeSet(output *Command, s *Store) ([]byte, error) {
s.mu.Lock()
defer s.mu.Unlock()
log.Println(output)
if len(output.Args) < 2 {
return nil, fmt.Errorf("either the key or value is missing")
}
key := output.Args[0]
value := output.Args[1]
record := &Record{
value: value,
createdAt: time.Now(),
}
log.Println(output.Args)
if len(output.Args) == 4 && output.Args[2] == "PX" {
expiration, err := strconv.Atoi(output.Args[3])
if err != nil {
fmt.Println(err)
return nil, fmt.Errorf("-ERR wrong expiration time provided for the record")
}
record.expiryAt = time.Now().Add(time.Duration(expiration) * time.Millisecond) // Converting milliseconds to seconds
}
s.kv[key] = record
log.Println("Expiry time",s.kv[key].expiryAt)
return respOK, nil
}
func executeGet(output *Command, s *Store) ([]byte, error) {
// s.mu.RLock()
// defer s.mu.RUnlock()
if len(output.Args) < 1 {
return nil, fmt.Errorf("Key is missing")
}
key := output.Args[0]
val, ok := s.kv[key]
if ok {
if val.expiryAt.IsZero() || val.expiryAt.After(time.Now()) {
return respString(val.value), nil
}
// If expired, delete the key and return null
delete(s.kv, key)
return respNull, nil
} else {
return respNull, nil
}
}