Skip to content

Commit

Permalink
Merge 2390564 into 51598f3
Browse files Browse the repository at this point in the history
  • Loading branch information
tyzhnenko committed Oct 15, 2019
2 parents 51598f3 + 2390564 commit dafa636
Show file tree
Hide file tree
Showing 7 changed files with 401 additions and 24 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@

The library adds a thin layer on top of [lru cache](https://github.com/hashicorp/golang-lru) and [patrickmn/go-cache](https://github.com/patrickmn/go-cache).

| Cache name | Constructor | Defaults | Description |
| -------------- | --------------------- | ----------------- | --------------------- |
| LruCache | lcw.NewLruCache | keys=1000 | LRU cache with limits |
| ExpirableCache | lcw.NewExpirableCache | keys=1000, ttl=5m | TTL cache with limits |
| Nop | lcw.NewNopCache | | Do-nothing cache |
| Cache name | Constructor | Defaults | Description |
| -------------- | --------------------- | ----------------- | ----------------------- |
| LruCache | lcw.NewLruCache | keys=1000 | LRU cache with limits |
| ExpirableCache | lcw.NewExpirableCache | keys=1000, ttl=5m | TTL cache with limits |
| RedisCache | lcw.NewRedisCache | ttl=5m | Redis cache with limits |
| Nop | lcw.NewNopCache | | Do-nothing cache |


Main features:

- LoadingCache (guava style)
- Limit maximum cache size (in bytes)
- Limit maximum key size
- Limit maximum size of a value
- Limit maximum size of a value
- Limit number of keys
- TTL support (`ExpirableCache` only)
- Callback on eviction event
- TTL support (`ExpirableCache` and `RedisCache`)
- Callback on eviction event (not supported in `RedisCache`)
- Functional style invalidation
- Functional options
- Sane defaults

## Install and update

`go get -u github.com/go-pkgz/lcw`
Expand Down
86 changes: 73 additions & 13 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"
"time"

redis "github.com/go-redis/redis/v7"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -112,19 +113,28 @@ func TestCache_MaxValueSize(t *testing.T) {
res, err = c.Get("key-Z", func() (Value, error) {
return sizedString("result-Zzzz"), nil
})
if s, ok := res.(string); ok {
res = sizedString(s)
}
assert.NoError(t, err)
assert.Equal(t, sizedString("result-Z"), res.(sizedString), "got cached value")

// put too big value to cache and make sure it is not cached
res, err = c.Get("key-Big", func() (Value, error) {
return sizedString("1234567890"), nil
})
if s, ok := res.(string); ok {
res = sizedString(s)
}
assert.NoError(t, err)
assert.Equal(t, sizedString("1234567890"), res.(sizedString))

res, err = c.Get("key-Big", func() (Value, error) {
return sizedString("result-big"), nil
})
if s, ok := res.(string); ok {
res = sizedString(s)
}
assert.NoError(t, err)
assert.Equal(t, sizedString("result-big"), res.(sizedString), "got not cached value")

Expand Down Expand Up @@ -154,28 +164,39 @@ func TestCache_MaxCacheSize(t *testing.T) {
return sizedString("result-Z"), nil
})
assert.NoError(t, err)
if s, ok := res.(string); ok {
res = sizedString(s)
}
assert.Equal(t, sizedString("result-Z"), res.(sizedString))

res, err = c.Get("key-Z", func() (Value, error) {
return sizedString("result-Zzzz"), nil
})
if s, ok := res.(string); ok {
res = sizedString(s)
}
assert.NoError(t, err)
assert.Equal(t, sizedString("result-Z"), res.(sizedString), "got cached value")
assert.Equal(t, int64(8), c.size())

if _, ok := c.(*RedisCache); !ok {
assert.Equal(t, int64(8), c.size())
}
_, err = c.Get("key-Z2", func() (Value, error) {
return sizedString("result-Y"), nil
})
assert.Nil(t, err)
assert.Equal(t, int64(16), c.size())
if _, ok := c.(*RedisCache); !ok {
assert.Equal(t, int64(16), c.size())
}

// this will cause removal
_, err = c.Get("key-Z3", func() (Value, error) {
return sizedString("result-Z"), nil
})
assert.Nil(t, err)
assert.Equal(t, int64(16), c.size())
assert.Equal(t, 2, c.keys())
if _, ok := c.(*RedisCache); !ok {
assert.Equal(t, int64(16), c.size())
// Due RedisCache does not support MaxCacheSize this assert should be skipped
assert.Equal(t, 2, c.keys())
}
})
}
}
Expand Down Expand Up @@ -371,11 +392,14 @@ func TestCache_Delete(t *testing.T) {
require.Nil(t, err)
}
assert.Equal(t, 1000, c.Stat().Keys)
assert.Equal(t, int64(9890), c.Stat().Size)

if _, ok := c.(*RedisCache); !ok {
assert.Equal(t, int64(9890), c.Stat().Size)
}
c.Delete("key-2")
assert.Equal(t, 999, c.Stat().Keys)
assert.Equal(t, int64(9890-8), c.Stat().Size)
if _, ok := c.(*RedisCache); !ok {
assert.Equal(t, int64(9890-8), c.Stat().Size)
}
})
}
}
Expand All @@ -392,8 +416,12 @@ func TestCache_DeleteWithEvent(t *testing.T) {

caches := cachesTestList(t, OnEvicted(onEvict))
for _, c := range caches {

evKey, evVal, evCount = "", "", 0
t.Run(strings.Replace(fmt.Sprintf("%T", c), "*lcw.", "", 1), func(t *testing.T) {
if _, ok := c.(*RedisCache); ok {
t.Skip("RedisCache doesn't support delete events")
}
// fill cache
for i := 0; i < 1000; i++ {
_, err := c.Get(fmt.Sprintf("key-%d", i), func() (Value, error) {
Expand Down Expand Up @@ -425,25 +453,45 @@ func TestCache_Stats(t *testing.T) {
require.Nil(t, err)
}
stats := c.Stat()
assert.Equal(t, CacheStat{Hits: 0, Misses: 100, Keys: 100, Size: 890}, stats)
switch c.(type) {
case *RedisCache:
assert.Equal(t, CacheStat{Hits: 0, Misses: 100, Keys: 100, Size: 0}, stats)
default:
assert.Equal(t, CacheStat{Hits: 0, Misses: 100, Keys: 100, Size: 890}, stats)
}

_, err := c.Get("key-1", func() (Value, error) {
return "xyz", nil
})
require.NoError(t, err)
assert.Equal(t, CacheStat{Hits: 1, Misses: 100, Keys: 100, Size: 890}, c.Stat())
switch c.(type) {
case *RedisCache:
assert.Equal(t, CacheStat{Hits: 1, Misses: 100, Keys: 100, Size: 0}, c.Stat())
default:
assert.Equal(t, CacheStat{Hits: 1, Misses: 100, Keys: 100, Size: 890}, c.Stat())
}

_, err = c.Get("key-1123", func() (Value, error) {
return sizedString("xyz"), nil
})
require.NoError(t, err)
assert.Equal(t, CacheStat{Hits: 1, Misses: 101, Keys: 101, Size: 893}, c.Stat())
switch c.(type) {
case *RedisCache:
assert.Equal(t, CacheStat{Hits: 1, Misses: 101, Keys: 101, Size: 0}, c.Stat())
default:
assert.Equal(t, CacheStat{Hits: 1, Misses: 101, Keys: 101, Size: 893}, c.Stat())
}

_, err = c.Get("key-9999", func() (Value, error) {
return nil, errors.New("err")
})
require.NotNil(t, err)
assert.Equal(t, CacheStat{Hits: 1, Misses: 101, Keys: 101, Size: 893, Errors: 1}, c.Stat())
switch c.(type) {
case *RedisCache:
assert.Equal(t, CacheStat{Hits: 1, Misses: 101, Keys: 101, Size: 0, Errors: 1}, c.Stat())
default:
assert.Equal(t, CacheStat{Hits: 1, Misses: 101, Keys: 101, Size: 893, Errors: 1}, c.Stat())
}
})

}
Expand Down Expand Up @@ -515,9 +563,21 @@ func cachesTestList(t *testing.T, opts ...Option) []countedCache {
lc, err := NewLruCache(opts...)
require.NoError(t, err, "can't make lru cache")
caches = append(caches, lc)

server := newTestRedisServer()
client := redis.NewClient(&redis.Options{
Addr: server.Addr()})
rc, err := NewRedisCache(client, opts...)
require.NoError(t, err, "can't make redis cache")
caches = append(caches, rc)

return caches
}

type sizedString string

func (s sizedString) Size() int { return len(s) }

func (s sizedString) MarshalBinary() (data []byte, err error) {
return []byte(s), nil
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
module github.com/go-pkgz/lcw

require (
github.com/alicebob/gopher-json v0.0.0-20180125190556-5a6b3ba71ee6 // indirect
github.com/alicebob/miniredis v2.5.0+incompatible
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-redis/redis/v7 v7.0.0-beta.4
github.com/gomodule/redigo v2.0.0+incompatible // indirect
github.com/hashicorp/golang-lru v0.5.0
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pkg/errors v0.8.1
github.com/stretchr/testify v1.3.0
github.com/yuin/gopher-lua v0.0.0-20190514113301-1cd887cd7036 // indirect
)

go 1.13
28 changes: 28 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
github.com/alicebob/gopher-json v0.0.0-20180125190556-5a6b3ba71ee6 h1:45bxf7AZMwWcqkLzDAQugVEwedisr5nRJ1r+7LYnv0U=
github.com/alicebob/gopher-json v0.0.0-20180125190556-5a6b3ba71ee6/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc=
github.com/alicebob/miniredis v2.5.0+incompatible h1:yBHoLpsyjupjz3NL3MhKMVkR41j82Yjf3KFv7ApYzUI=
github.com/alicebob/miniredis v2.5.0+incompatible/go.mod h1:8HZjEj4yU0dwhYHky+DxYx+6BMjkBbe5ONFIF1MXffk=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-redis/redis/v7 v7.0.0-beta.4 h1:p6z7Pde69EGRWvlC++y8aFcaWegyrKHzOBGo0zUACTQ=
github.com/go-redis/redis/v7 v7.0.0-beta.4/go.mod h1:xhhSbUMTsleRPur+Vgx9sUHtyN33bdjxY+9/0n9Ig8s=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/gomodule/redigo v2.0.0+incompatible h1:K/R+8tc58AaqLkqG2Ol3Qk+DR/TlNuhuh457pBFPtt0=
github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
github.com/hashicorp/golang-lru v0.5.0 h1:CL2msUPvZTLb5O648aiLNJw3hnBxN2+1Jq8rCOH9wdo=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
Expand All @@ -13,3 +30,14 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/yuin/gopher-lua v0.0.0-20190514113301-1cd887cd7036 h1:1b6PAtenNyhsmo/NKXVe34h7JEZKva1YB/ne7K7mqKM=
github.com/yuin/gopher-lua v0.0.0-20190514113301-1cd887cd7036/go.mod h1:gqRgreBUhTSL0GeU64rtZ3Uq3wtjOa/TB2YfrtkCbVQ=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190204203706-41f3e6584952/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
2 changes: 1 addition & 1 deletion lru_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/pkg/errors"
)

// LruCache wraps lru.LruCache with laoding cache Get and size limits
// LruCache wraps lru.LruCache with loading cache Get and size limits
type LruCache struct {
options
CacheStat
Expand Down
Loading

0 comments on commit dafa636

Please sign in to comment.