Skip to content

Commit

Permalink
add setting for nil ttl
Browse files Browse the repository at this point in the history
  • Loading branch information
Ganesha Danu committed Sep 30, 2019
1 parent d75a6a7 commit 52829aa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
18 changes: 13 additions & 5 deletions keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ import (
const (
// Override these when constructing the cache keeper
defaultTTL = 10 * time.Second
defaultNilTTL = 5 * time.Minute
defaultLockDuration = 1 * time.Minute
defaultLockTries = 1
defaultWaitTime = 15 * time.Second
)

var nilJSON = []byte("null")

type (
// CacheGeneratorFn :nodoc:
CacheGeneratorFn func() (interface{}, error)
Expand All @@ -30,7 +33,7 @@ type (
StoreWithoutBlocking(Item) error
StoreMultiWithoutBlocking([]Item) error
StoreMultiPersist([]Item) error
StoreNil(cacheKey string, ttl time.Duration) error
StoreNil(cacheKey string) error
Expire(string, time.Duration) error
ExpireMulti(map[string]time.Duration) error
Purge(string) error
Expand All @@ -39,6 +42,7 @@ type (

AcquireLock(string) (*redsync.Mutex, error)
SetDefaultTTL(time.Duration)
SetNilTTL(time.Duration)
SetConnectionPool(*redigo.Pool)
SetLockConnectionPool(*redigo.Pool)
SetLockDuration(time.Duration)
Expand All @@ -61,6 +65,7 @@ type (

keeper struct {
connPool *redigo.Pool
nilTTL time.Duration
defaultTTL time.Duration
waitTime time.Duration
disableCaching bool
Expand All @@ -75,6 +80,7 @@ type (
func NewKeeper() Keeper {
return &keeper{
defaultTTL: defaultTTL,
nilTTL: defaultNilTTL,
lockDuration: defaultLockDuration,
lockTries: defaultLockTries,
waitTime: defaultWaitTime,
Expand Down Expand Up @@ -194,10 +200,8 @@ func (k *keeper) StoreWithoutBlocking(c Item) error {
}

// StoreNil :nodoc:
func (k *keeper) StoreNil(cacheKey string, ttl time.Duration) error {
nilJSON := []byte("null")

item := NewItemWithCustomTTL(cacheKey, nilJSON, ttl)
func (k *keeper) StoreNil(cacheKey string) error {
item := NewItemWithCustomTTL(cacheKey, nilJSON, k.nilTTL)
err := k.StoreWithoutBlocking(item)
return err
}
Expand Down Expand Up @@ -260,6 +264,10 @@ func (k *keeper) SetDefaultTTL(d time.Duration) {
k.defaultTTL = d
}

func (k *keeper) SetNilTTL(d time.Duration) {
k.nilTTL = d
}

// SetConnectionPool :nodoc:
func (k *keeper) SetConnectionPool(c *redigo.Pool) {
k.connPool = c
Expand Down
2 changes: 1 addition & 1 deletion keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ func TestStoreNil(t *testing.T) {

testKey := "test-key"

err = k.StoreNil(testKey, 5*time.Minute)
err = k.StoreNil(testKey)
assert.NoError(t, err)

reply, mu, err := k.GetOrLock(testKey)
Expand Down

0 comments on commit 52829aa

Please sign in to comment.