diff --git a/keeper.go b/keeper.go index dc1162f..11a9202 100644 --- a/keeper.go +++ b/keeper.go @@ -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) @@ -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 @@ -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) @@ -61,6 +65,7 @@ type ( keeper struct { connPool *redigo.Pool + nilTTL time.Duration defaultTTL time.Duration waitTime time.Duration disableCaching bool @@ -75,6 +80,7 @@ type ( func NewKeeper() Keeper { return &keeper{ defaultTTL: defaultTTL, + nilTTL: defaultNilTTL, lockDuration: defaultLockDuration, lockTries: defaultLockTries, waitTime: defaultWaitTime, @@ -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 } @@ -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 diff --git a/keeper_test.go b/keeper_test.go index a7b923a..539b56c 100644 --- a/keeper_test.go +++ b/keeper_test.go @@ -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)