Skip to content

Commit

Permalink
Merge pull request #12 from kumparan/feature/not-found-cache
Browse files Browse the repository at this point in the history
Implement store nil cache
  • Loading branch information
BlinfoldKing authored Oct 1, 2019
2 parents 276042f + 52829aa commit e43e456
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/gomodule/redigo v2.0.0+incompatible
github.com/jpillora/backoff v0.0.0-20180909062703-3050d21c67d7
github.com/kumparan/go-lib v1.0.0
github.com/kumparan/tapao v1.0.0
github.com/stretchr/testify v1.3.0
github.com/yuin/gopher-lua v0.0.0-20190206043414-8bfc7677f583 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,13 @@ github.com/jpillora/backoff v0.0.0-20180909062703-3050d21c67d7/go.mod h1:2iMrUgb
github.com/kelseyhightower/envconfig v1.3.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kumparan/go-lib v1.0.0 h1:DrMvpjy+l4n5iSX1I7xeUpM7wauk9AiA6lXBJWYNJU4=
github.com/kumparan/go-lib v1.0.0/go.mod h1:HcO/9dlk4k+SKxDQsJb83XIaofg0jxVt0nlotPFVVq8=
github.com/kumparan/tapao v1.0.0 h1:hw1Lp8ZMPRZMG+PqCo1LNe/Gc5Zsp0wXMuXhb1AH54Q=
github.com/kumparan/tapao v1.0.0/go.mod h1:wP3kT/KnEmnJYytGKi3MT5btOGcp4Lq1SyZ4P1RqaCM=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
Expand Down Expand Up @@ -89,6 +94,8 @@ github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stvp/tempredis v0.0.0-20181119212430-b82af8480203 h1:QVqDTf3h2WHt08YuiTGPZLls0Wq99X9bWd0Q5ZSBesM=
github.com/stvp/tempredis v0.0.0-20181119212430-b82af8480203/go.mod h1:oqN97ltKNihBbwlX8dLpwxCl3+HnXKV/R0e+sRLd9C8=
github.com/vmihailenco/msgpack v4.0.3+incompatible h1:g+G529Dqo4BY2Gxn5GKENa/3NVK+mu/6hM7G3jEWszQ=
github.com/vmihailenco/msgpack v4.0.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/yuin/gopher-lua v0.0.0-20181212084658-d1ab6d058001/go.mod h1:fFiAh+CowNFr0NK5VASokuwKwkbacRmHsVA7Yb1Tqac=
github.com/yuin/gopher-lua v0.0.0-20190206043414-8bfc7677f583 h1:SZPG5w7Qxq7bMcMVl6e3Ht2X7f+AAGQdzjkbyOnNNZ8=
github.com/yuin/gopher-lua v0.0.0-20190206043414-8bfc7677f583/go.mod h1:gqRgreBUhTSL0GeU64rtZ3Uq3wtjOa/TB2YfrtkCbVQ=
Expand All @@ -102,17 +109,20 @@ golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73r
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/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=
golang.org/x/tools v0.0.0-20180828015842-6cd1fcedba52/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.3.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/grpc v1.17.0 h1:TRJYBgMclJvGYn2rIMjj+h9KtMt5r1Ij7ODVRIZkwhk=
google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/vmihailenco/msgpack.v2 v2.9.1/go.mod h1:/3Dn1Npt9+MYyLpYYXjInO/5jvMLamn+AEGwNEOatn8=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
18 changes: 18 additions & 0 deletions keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -30,6 +33,7 @@ type (
StoreWithoutBlocking(Item) error
StoreMultiWithoutBlocking([]Item) error
StoreMultiPersist([]Item) error
StoreNil(cacheKey string) error
Expire(string, time.Duration) error
ExpireMulti(map[string]time.Duration) error
Purge(string) error
Expand All @@ -38,6 +42,7 @@ type (

AcquireLock(string) (*redsync.Mutex, error)
SetDefaultTTL(time.Duration)
SetNilTTL(time.Duration)
SetConnectionPool(*redigo.Pool)
SetLockConnectionPool(*redigo.Pool)
SetLockDuration(time.Duration)
Expand All @@ -60,6 +65,7 @@ type (

keeper struct {
connPool *redigo.Pool
nilTTL time.Duration
defaultTTL time.Duration
waitTime time.Duration
disableCaching bool
Expand All @@ -74,6 +80,7 @@ type (
func NewKeeper() Keeper {
return &keeper{
defaultTTL: defaultTTL,
nilTTL: defaultNilTTL,
lockDuration: defaultLockDuration,
lockTries: defaultLockTries,
waitTime: defaultWaitTime,
Expand Down Expand Up @@ -192,6 +199,13 @@ func (k *keeper) StoreWithoutBlocking(c Item) error {
return err
}

// StoreNil :nodoc:
func (k *keeper) StoreNil(cacheKey string) error {
item := NewItemWithCustomTTL(cacheKey, nilJSON, k.nilTTL)
err := k.StoreWithoutBlocking(item)
return err
}

// Purge :nodoc:
func (k *keeper) Purge(matchString string) error {
if k.disableCaching {
Expand Down Expand Up @@ -250,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
Expand Down
23 changes: 23 additions & 0 deletions keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,3 +540,26 @@ func TestGetTTL(t *testing.T) {
assert.IsType(t, typeInt64, ttl)

}

func TestStoreNil(t *testing.T) {
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)

testKey := "test-key"

err = k.StoreNil(testKey)
assert.NoError(t, err)

reply, mu, err := k.GetOrLock(testKey)

assert.NoError(t, err)
assert.Equal(t, []byte("null"), reply)
assert.Nil(t, mu)
}

0 comments on commit e43e456

Please sign in to comment.