From a8e63d2e5c17f496f2a044f8db03c0b8db5fba70 Mon Sep 17 00:00:00 2001 From: Ivan Date: Tue, 16 Apr 2019 10:16:43 +0700 Subject: [PATCH] Plain get --- keeper.go | 15 +++++++++++++++ keeper_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/keeper.go b/keeper.go index dab294f..68a43f5 100644 --- a/keeper.go +++ b/keeper.go @@ -23,6 +23,7 @@ type ( // Keeper responsible for managing cache Keeper interface { + Get(string) (interface{}, error) GetOrLock(string) (interface{}, *redsync.Mutex, error) GetOrSet(string, CacheGeneratorFn, time.Duration) (interface{}, error) Store(*redsync.Mutex, Item) error @@ -68,6 +69,20 @@ func NewKeeper() Keeper { } } +// Get :nodoc: +func (k *keeper) Get(key string) (cachedItem interface{}, err error) { + if k.disableCaching { + return + } + + cachedItem, err = k.getCachedItem(key) + if err != nil && err != redigo.ErrNil || cachedItem != nil { + return + } + + return nil, nil +} + // GetOrLock :nodoc: func (k *keeper) GetOrLock(key string) (cachedItem interface{}, mutex *redsync.Mutex, err error) { if k.disableCaching { diff --git a/keeper_test.go b/keeper_test.go index 9fb3a15..de9cad3 100644 --- a/keeper_test.go +++ b/keeper_test.go @@ -30,6 +30,36 @@ func newRedisConn(url string) *redigo.Pool { } } +func TestGet(t *testing.T) { + // Initialize new cache keeper + k := NewKeeper() + + m, err := miniredis.Run() + assert.NoError(t, err) + + r := newRedisConn(m.Addr()) + k.SetConnectionPool(r) + k.SetLockConnectionPool(r) + k.SetWaitTime(1 * time.Second) // override wait time to 1 second + + testKey := "test-key" + + t.Run("Not Exist", func(t *testing.T) { + assert.False(t, m.Exists(testKey)) + result, err := k.Get(testKey) + assert.NoError(t, err) + assert.Nil(t, result) + }) + + t.Run("Exist", func(t *testing.T) { + val := "something-something-here" + m.Set(testKey, val) + result, err := k.Get(testKey) + assert.NoError(t, err) + assert.EqualValues(t, result, val) + }) +} + func TestGetLockStore(t *testing.T) { // Initialize new cache keeper k := NewKeeper()