From 37f0263976c3dea219748022c73ce7656e458b4a Mon Sep 17 00:00:00 2001 From: Ivan Date: Mon, 8 Apr 2019 16:51:09 +0700 Subject: [PATCH] Bugfix for redis complaining "value is not an integer or out of range": replace float64 with int64 --- item.go | 8 ++++---- keeper.go | 8 ++++---- keeper_test.go | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/item.go b/item.go index ff328a1..42a4b1d 100644 --- a/item.go +++ b/item.go @@ -7,7 +7,7 @@ import ( type ( // Item :nodoc: Item interface { - GetTTLFloat64() float64 + GetTTLInt64() int64 GetKey() string GetValue() interface{} } @@ -36,9 +36,9 @@ func NewItemWithCustomTTL(key string, value interface{}, customTTL time.Duration } } -// GetTTLFloat64 :nodoc: -func (i *item) GetTTLFloat64() float64 { - return i.ttl.Seconds() +// GetTTLInt64 :nodoc: +func (i *item) GetTTLInt64() int64 { + return int64(i.ttl.Seconds()) } // GetKey :nodoc: diff --git a/keeper.go b/keeper.go index 17cf117..45c1c69 100644 --- a/keeper.go +++ b/keeper.go @@ -251,12 +251,12 @@ func (k *keeper) AcquireLock(key string) (*redsync.Mutex, error) { return m, m.Lock() } -func (k *keeper) decideCacheTTL(c Item) float64 { - if c.GetTTLFloat64() > 0 { - return c.GetTTLFloat64() +func (k *keeper) decideCacheTTL(c Item) (ttl int64) { + if ttl = c.GetTTLInt64(); ttl > 0 { + return } - return k.defaultTTL.Seconds() + return int64(k.defaultTTL.Seconds()) } func (k *keeper) getCachedItem(key string) (value interface{}, err error) { diff --git a/keeper_test.go b/keeper_test.go index 3da63cd..7d1ca03 100644 --- a/keeper_test.go +++ b/keeper_test.go @@ -160,11 +160,11 @@ func TestDecideCacheTTL(t *testing.T) { // It should use keeper's default TTL when new cache item didn't specify the TTL i := NewItem(testKey, nil) - assert.Equal(t, k.defaultTTL.Seconds(), k.decideCacheTTL(i)) + assert.Equal(t, int64(k.defaultTTL.Seconds()), k.decideCacheTTL(i)) // It should use specified TTL when new cache item specify the TTL i2 := NewItemWithCustomTTL(testKey, nil, 10*time.Second) - assert.Equal(t, i2.GetTTLFloat64(), k.decideCacheTTL(i)) + assert.Equal(t, i2.GetTTLInt64(), k.decideCacheTTL(i)) } func TestIncreaseCachedValueByOne(t *testing.T) {