Skip to content

Commit

Permalink
Merge pull request #6 from kumparan/plain_get
Browse files Browse the repository at this point in the history
Plain get
  • Loading branch information
ivanp authored Apr 16, 2019
2 parents 5a2c078 + a8e63d2 commit 5fba574
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
15 changes: 15 additions & 0 deletions keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
30 changes: 30 additions & 0 deletions keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 5fba574

Please sign in to comment.