From f739cf38b8bf1e8d252dedaf5f7055df4ed3f8e7 Mon Sep 17 00:00:00 2001 From: siddharth ravikumar Date: Mon, 6 Jun 2022 20:05:50 -0400 Subject: cache: update `item` Change type of value to `[]byte`. --- cache/cache.go | 12 ++++++------ cache/cache_test.go | 17 +++++++++-------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/cache/cache.go b/cache/cache.go index fbc763e..905aebe 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -8,7 +8,7 @@ import "time" // An item in the key-value cache store. type item struct { - value string + value []byte expires time.Time // Time when the key-value expires } @@ -29,7 +29,7 @@ func NewCache() *Cache { // // Cache.Get will return an empty string once `expires` is past the // current time. -func (c *Cache) Set(key, value string, expires time.Time) { +func (c *Cache) Set(key string, value []byte, expires time.Time) { c.store[key] = item{ value: value, expires: expires, @@ -38,17 +38,17 @@ func (c *Cache) Set(key, value string, expires time.Time) { // Get an (key,value) item from the cache store by key. // -// An empty string will be returned when if the key does not exist or +// An empty []byte will be returned when if the key does not exist or // if the item corresponding to the key has expired. An expired // (key,value) item will be removed from the cache store. -func (c *Cache) Get(key string) string { +func (c *Cache) Get(key string) []byte { if _, ok := c.store[key]; !ok { - return "" + return []byte{} } // Check if the item expired. if time.Until(c.store[key].expires).Seconds() < 0 { delete(c.store, key) - return "" + return []byte{} } return c.store[key].value } diff --git a/cache/cache_test.go b/cache/cache_test.go index 41e0d25..4751260 100644 --- a/cache/cache_test.go +++ b/cache/cache_test.go @@ -4,6 +4,7 @@ package cache import ( + "bytes" "testing" "time" ) @@ -20,10 +21,10 @@ func TestNewCache(t *testing.T) { } // Try manually adding an item. c.store["foo"] = item{ - value: "bar", + value: []byte("bar"), expires: time.Now().Add(time.Second * 10), } - if c.store["foo"].value != "bar" { + if bytes.Compare(c.store["foo"].value, []byte("bar")) != 0 { t.Errorf("cache.store['foo'] is not bar") return } @@ -36,8 +37,8 @@ func TestCacheSet(t *testing.T) { return } exp := time.Now().Add(time.Second * 10) - c.Set("foo", "bar", exp) - if c.store["foo"].value != "bar" { + c.Set("foo", []byte("bar"), exp) + if bytes.Compare(c.store["foo"].value, []byte("bar")) != 0 { t.Errorf("cache.store['foo'] is not bar") return } @@ -56,16 +57,16 @@ func TestCacheGet(t *testing.T) { // Test 1 exp := time.Now().Add(time.Second * 10) - c.Set("foo", "bar", exp) - if c.Get("foo") != "bar" { + c.Set("foo", []byte("bar"), exp) + if bytes.Compare(c.Get("foo"), []byte("bar")) != 0 { t.Errorf("cache.Get(foo) is not bar") return } // Test 2 exp = time.Now().Add(time.Second * -10) - c.Set("sna", "fu", exp) - if c.Get("sna") != "" { + c.Set("sna", []byte("fu"), exp) + if bytes.Compare(c.Get("sna"), []byte{}) != 0 { t.Errorf("cache.Get(sna) is not empty: %s", c.Get("sna")) return } -- cgit v1.2.3