fix: use pointers for expiration date
This commit is contained in:
parent
a3b5b9b1d9
commit
cb38484486
1 changed files with 7 additions and 7 deletions
14
cache.go
14
cache.go
|
@ -1,6 +1,6 @@
|
||||||
// Package cache defines an in-memory key-value store.
|
// Package cache defines an in-memory key-value store.
|
||||||
//
|
//
|
||||||
// It supports exîration dates andcan store arbitrary values of any type.
|
// It supports exîration dates and can store arbitrary values of any type.
|
||||||
// Keys must be strings.
|
// Keys must be strings.
|
||||||
package cache
|
package cache
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type entry struct {
|
type entry struct {
|
||||||
expirationDate time.Time
|
expirationDate *time.Time
|
||||||
value interface{}
|
value interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ func (c *Cache) Put(key string, val interface{}) {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
defer c.mu.Unlock()
|
defer c.mu.Unlock()
|
||||||
|
|
||||||
c.data[key] = entry{time.Time{}, val}
|
c.data[key] = entry{nil, val}
|
||||||
}
|
}
|
||||||
|
|
||||||
// PutTTL stores a value in the cache under the given key. The value will
|
// PutTTL stores a value in the cache under the given key. The value will
|
||||||
|
@ -52,12 +52,12 @@ func (c *Cache) PutTTL(key string, val interface{}, ttl time.Duration) {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
defer c.mu.Unlock()
|
defer c.mu.Unlock()
|
||||||
|
|
||||||
c.data[key] = entry{exp, val}
|
c.data[key] = entry{&exp, val}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns the value asspciated with the given key.
|
// Get returns the value asspciated with the given key.
|
||||||
// The second return values indicates if the cache hs been hit or not.
|
// The second return values indicates if the cache hs been hit or not.
|
||||||
func (c Cache) Get(key string) (interface{}, bool) {
|
func (c *Cache) Get(key string) (interface{}, bool) {
|
||||||
c.mu.RLock()
|
c.mu.RLock()
|
||||||
v, ok := c.data[key]
|
v, ok := c.data[key]
|
||||||
c.mu.RUnlock()
|
c.mu.RUnlock()
|
||||||
|
@ -66,7 +66,7 @@ func (c Cache) Get(key string) (interface{}, bool) {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
if !v.expirationDate.IsZero() && v.expirationDate.Before(time.Now()) {
|
if v.expirationDate != nil && v.expirationDate.Before(time.Now()) {
|
||||||
c.Del(key)
|
c.Del(key)
|
||||||
|
|
||||||
return nil, false
|
return nil, false
|
||||||
|
@ -85,7 +85,7 @@ func (c *Cache) Del(key string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Count returns the total number of entries in the cache (vamid and expired).
|
// Count returns the total number of entries in the cache (vamid and expired).
|
||||||
func (c Cache) Count() int {
|
func (c *Cache) Count() int {
|
||||||
c.mu.RLock()
|
c.mu.RLock()
|
||||||
defer c.mu.RUnlock()
|
defer c.mu.RUnlock()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue