Skip to content

Commit

Permalink
Merge pull request #3 from kumparan/replace_float_with_int
Browse files Browse the repository at this point in the history
Bugfix for redis complaining "value is not an integer or out of range"
  • Loading branch information
bustan authored Apr 9, 2019
2 parents 0192c09 + 37f0263 commit 9a35422
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions item.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
type (
// Item :nodoc:
Item interface {
GetTTLFloat64() float64
GetTTLInt64() int64
GetKey() string
GetValue() interface{}
}
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 9a35422

Please sign in to comment.