Skip to content

Commit

Permalink
combine interface and common tests, add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Feb 28, 2019
1 parent 24b58ea commit 4bd6761
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 50 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Main features:
- Limit maximum size of a value
- Limit number of keys
- TTL support (`ExpirableCache` only)
- Callback on eviction event
- Functional style invalidation
- Functional options
- Sane defaults
Expand Down
6 changes: 6 additions & 0 deletions interface.go → cache.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Package lcw adds a thin layer on top of lru cache and go-cache providing more limits and common interface.
// The primary method to get (and set) data to/from the cache is LoadingCache.Get retruning stored data for a given key or
// call provided func to retrive and store, similar to Guava loading cache.
// Limits allow max values for key size, number of keys, value size and total size of values in the cache.
// CacheStat gives general stats on cache performance.
// 3 flavours of cache provided - NoP (do-nothing cache), ExpirableCache (TTL based), and LruCache
package lcw

import "fmt"
Expand Down
42 changes: 42 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,48 @@ import (
"github.com/stretchr/testify/require"
)

func TestNop_Get(t *testing.T) {
var coldCalls int32
var c LoadingCache = NewNopCache()
res, err := c.Get("key1", func() (Value, error) {
atomic.AddInt32(&coldCalls, 1)
return "result", nil
})
assert.Nil(t, err)
assert.Equal(t, "result", res.(string))
assert.Equal(t, int32(1), atomic.LoadInt32(&coldCalls))

res, err = c.Get("key1", func() (Value, error) {
atomic.AddInt32(&coldCalls, 1)
return "result2", nil
})
assert.Nil(t, err)
assert.Equal(t, "result2", res.(string))
assert.Equal(t, int32(2), atomic.LoadInt32(&coldCalls))

assert.Equal(t, CacheStat{}, c.Stat())
}

func TestNop_Peek(t *testing.T) {
var coldCalls int32
c := NewNopCache()
res, err := c.Get("key1", func() (Value, error) {
atomic.AddInt32(&coldCalls, 1)
return "result", nil
})
assert.Nil(t, err)
assert.Equal(t, "result", res.(string))
assert.Equal(t, int32(1), atomic.LoadInt32(&coldCalls))

_, ok := c.Peek("key1")
assert.False(t, ok)
}

func TestStat_String(t *testing.T) {
s := CacheStat{Keys: 100, Hits: 60, Misses: 10, Size: 12345, Errors: 5}
assert.Equal(t, "{hits:60, misses:10, ratio:85.7%, keys:100, size:12345, errors:5}", s.String())
}

func TestCache_Get(t *testing.T) {

caches := cachesTestList(t)
Expand Down
50 changes: 0 additions & 50 deletions interface_test.go

This file was deleted.

0 comments on commit 4bd6761

Please sign in to comment.