From 65401f8be45f97c376fdc810ba3ba52f77316b75 Mon Sep 17 00:00:00 2001 From: Ganesha Danu Date: Mon, 30 Sep 2019 14:05:30 +0700 Subject: [PATCH 1/3] implement store nil cache --- go.mod | 1 + go.sum | 10 ++++++++++ keeper.go | 9 +++++++++ keeper_test.go | 23 +++++++++++++++++++++++ 4 files changed, 43 insertions(+) diff --git a/go.mod b/go.mod index 9717e50..8039f48 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index b3552c1..210f658 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -102,6 +109,7 @@ 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= @@ -109,10 +117,12 @@ 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= diff --git a/keeper.go b/keeper.go index 59f03a7..70e1f31 100644 --- a/keeper.go +++ b/keeper.go @@ -30,6 +30,7 @@ type ( StoreWithoutBlocking(Item) error StoreMultiWithoutBlocking([]Item) error StoreMultiPersist([]Item) error + StoreNil(cacheKey string, ttl time.Duration) error Expire(string, time.Duration) error ExpireMulti(map[string]time.Duration) error Purge(string) error @@ -192,6 +193,14 @@ func (k *keeper) StoreWithoutBlocking(c Item) error { return err } +func (k *keeper) StoreNil(cacheKey string, ttl time.Duration) error { + nilJSON := []byte("null") + + item := NewItemWithCustomTTL(cacheKey, nilJSON, ttl) + err := k.StoreWithoutBlocking(item) + return err +} + // Purge :nodoc: func (k *keeper) Purge(matchString string) error { if k.disableCaching { diff --git a/keeper_test.go b/keeper_test.go index 50698b0..a7b923a 100644 --- a/keeper_test.go +++ b/keeper_test.go @@ -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, 5*time.Minute) + assert.NoError(t, err) + + reply, mu, err := k.GetOrLock(testKey) + + assert.NoError(t, err) + assert.Equal(t, []byte("null"), reply) + assert.Nil(t, mu) +} From d75a6a7d361c6cc312559272592cf9377a6c4620 Mon Sep 17 00:00:00 2001 From: Ganesha Danu Date: Mon, 30 Sep 2019 14:12:32 +0700 Subject: [PATCH 2/3] add nodoc for store nil --- keeper.go | 1 + 1 file changed, 1 insertion(+) diff --git a/keeper.go b/keeper.go index 70e1f31..dc1162f 100644 --- a/keeper.go +++ b/keeper.go @@ -193,6 +193,7 @@ func (k *keeper) StoreWithoutBlocking(c Item) error { return err } +// StoreNil :nodoc: func (k *keeper) StoreNil(cacheKey string, ttl time.Duration) error { nilJSON := []byte("null") From 52829aa9936961295284e57050f2d60086638f19 Mon Sep 17 00:00:00 2001 From: Ganesha Danu Date: Mon, 30 Sep 2019 14:28:39 +0700 Subject: [PATCH 3/3] add setting for nil ttl --- keeper.go | 18 +++++++++++++----- keeper_test.go | 2 +- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/keeper.go b/keeper.go index dc1162f..11a9202 100644 --- a/keeper.go +++ b/keeper.go @@ -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) @@ -30,7 +33,7 @@ type ( StoreWithoutBlocking(Item) error StoreMultiWithoutBlocking([]Item) error StoreMultiPersist([]Item) error - StoreNil(cacheKey string, ttl time.Duration) error + StoreNil(cacheKey string) error Expire(string, time.Duration) error ExpireMulti(map[string]time.Duration) error Purge(string) error @@ -39,6 +42,7 @@ type ( AcquireLock(string) (*redsync.Mutex, error) SetDefaultTTL(time.Duration) + SetNilTTL(time.Duration) SetConnectionPool(*redigo.Pool) SetLockConnectionPool(*redigo.Pool) SetLockDuration(time.Duration) @@ -61,6 +65,7 @@ type ( keeper struct { connPool *redigo.Pool + nilTTL time.Duration defaultTTL time.Duration waitTime time.Duration disableCaching bool @@ -75,6 +80,7 @@ type ( func NewKeeper() Keeper { return &keeper{ defaultTTL: defaultTTL, + nilTTL: defaultNilTTL, lockDuration: defaultLockDuration, lockTries: defaultLockTries, waitTime: defaultWaitTime, @@ -194,10 +200,8 @@ func (k *keeper) StoreWithoutBlocking(c Item) error { } // StoreNil :nodoc: -func (k *keeper) StoreNil(cacheKey string, ttl time.Duration) error { - nilJSON := []byte("null") - - item := NewItemWithCustomTTL(cacheKey, nilJSON, ttl) +func (k *keeper) StoreNil(cacheKey string) error { + item := NewItemWithCustomTTL(cacheKey, nilJSON, k.nilTTL) err := k.StoreWithoutBlocking(item) return err } @@ -260,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 diff --git a/keeper_test.go b/keeper_test.go index a7b923a..539b56c 100644 --- a/keeper_test.go +++ b/keeper_test.go @@ -554,7 +554,7 @@ func TestStoreNil(t *testing.T) { testKey := "test-key" - err = k.StoreNil(testKey, 5*time.Minute) + err = k.StoreNil(testKey) assert.NoError(t, err) reply, mu, err := k.GetOrLock(testKey)