1
0
Fork 0
mirror of https://github.com/alice-lg/birdwatcher.git synced 2025-03-09 00:00:05 +01:00

fixed handling serialized TTLs

This commit is contained in:
Annika Hannig 2020-10-28 16:03:22 +01:00
parent 5518ddf7e5
commit 45c1453831
No known key found for this signature in database
GPG key ID: 62E226E47DDCE58D

View file

@ -49,8 +49,8 @@ func (self *RedisCache) Get(key string) (Parsed, error) {
parsed := Parsed{}
err = json.Unmarshal([]byte(data), &parsed)
ttl, correct := parsed["ttl"].(time.Time)
if !correct {
ttl, err := parseCacheTTL(parsed["ttl"])
if err != nil {
return NilParse, fmt.Errorf("invalid TTL value for key: %s", key)
}
@ -89,3 +89,25 @@ func (self *RedisCache) Expire() int {
log.Printf("Cannot expire entries in RedisCache backend, redis does this automatically")
return 0
}
// Helperfunction to decode the cache ttl stored
// in the cache - which will most likely just be
// RFC3339 timestamp.
func parseCacheTTL(cacheTTL interface{}) (time.Time, error) {
if cacheTTL == nil {
// We preseve the nil value as a zero value
return time.Time{}, nil
}
switch cacheTTL.(type) {
case string:
ttl, err := time.Parse(time.RFC3339, cacheTTL.(string))
if err != nil {
return time.Time{}, err
}
return ttl, nil
case time.Time:
return cacheTTL.(time.Time), nil
}
return time.Time{}, nil
}