From cb384844860a46a0de737d57cb9957c60b317b71 Mon Sep 17 00:00:00 2001 From: Bruno Carlin Date: Fri, 17 Jan 2025 10:33:39 +0100 Subject: [PATCH] fix: use pointers for expiration date --- cache.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/cache.go b/cache.go index ccb6a9a..cd8387b 100644 --- a/cache.go +++ b/cache.go @@ -1,6 +1,6 @@ // 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. package cache @@ -10,7 +10,7 @@ import ( ) type entry struct { - expirationDate time.Time + expirationDate *time.Time value interface{} } @@ -33,7 +33,7 @@ func (c *Cache) Put(key string, val interface{}) { c.mu.Lock() 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 @@ -52,12 +52,12 @@ func (c *Cache) PutTTL(key string, val interface{}, ttl time.Duration) { c.mu.Lock() 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. // 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() v, ok := c.data[key] c.mu.RUnlock() @@ -66,7 +66,7 @@ func (c Cache) Get(key string) (interface{}, bool) { return nil, false } - if !v.expirationDate.IsZero() && v.expirationDate.Before(time.Now()) { + if v.expirationDate != nil && v.expirationDate.Before(time.Now()) { c.Del(key) 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). -func (c Cache) Count() int { +func (c *Cache) Count() int { c.mu.RLock() defer c.mu.RUnlock()