Skip to content

Commit

Permalink
propagate TTL
Browse files Browse the repository at this point in the history
  • Loading branch information
corinapurcarea committed May 12, 2020
1 parent 7030b81 commit ab7dc64
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 12 deletions.
9 changes: 6 additions & 3 deletions cache/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cache
import (
"fmt"
"log"
"time"

"github.com/eko/gocache/store"
)
Expand All @@ -15,6 +16,7 @@ const (
type chainKeyValue struct {
key interface{}
value interface{}
ttl time.Duration
storeType *string
}

Expand Down Expand Up @@ -44,7 +46,7 @@ func (c *ChainCache) setter() {
break
}

cache.Set(item.key, item.value, nil)
cache.Set(item.key, item.value, &store.Options{Expiration: item.ttl})
}
}
}
Expand All @@ -53,13 +55,14 @@ func (c *ChainCache) setter() {
func (c *ChainCache) Get(key interface{}) (interface{}, error) {
var object interface{}
var err error
var ttl time.Duration

for _, cache := range c.caches {
storeType := cache.GetCodec().GetStore().GetType()
object, err = cache.Get(key)
object, ttl, err = cache.GetWithTTL(key)
if err == nil {
// Set the value back until this cache layer
c.setChannel <- &chainKeyValue{key, object, &storeType}
c.setChannel <- &chainKeyValue{key, object, ttl, &storeType}
return object, nil
}

Expand Down
3 changes: 3 additions & 0 deletions cache/interface.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cache

import (
"time"

"github.com/eko/gocache/codec"
"github.com/eko/gocache/store"
)
Expand All @@ -19,6 +21,7 @@ type CacheInterface interface {
// storage (for instance: memory, redis, ...)
type SetterCacheInterface interface {
CacheInterface
GetWithTTL(key interface{}) (interface{}, time.Duration, error)

GetCodec() codec.CodecInterface
}
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
github.com/golang/protobuf v1.4.0 h1:oOuy+ugB+P/kBdUnG5QaMXSIyJ1q38wWSojYCb3z5VQ=
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
Expand Down Expand Up @@ -108,6 +109,7 @@ github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/procfs v0.0.8 h1:+fpWZdT24pJBiqJdAwYBjPSk+5YmQzYNPYzQsdzLkt8=
github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A=
github.com/prometheus/procfs v0.0.11 h1:DhHlBtkHWPYi8O2y31JkK0TF+DGM+51OopZjH/Ia5qI=
github.com/prometheus/procfs v0.0.11/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
Expand Down Expand Up @@ -154,6 +156,7 @@ golang.org/x/sys v0.0.0-20191010194322-b09406accb47/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200122134326-e047566fdf82 h1:ywK/j/KkyTHcdyYSZNXGjMwgmDSfjglYZ3vStQ/gSCU=
golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f h1:gWF768j/LaZugp8dyS4UwsslYCYz9XgFxvlgsn0n9H8=
golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand All @@ -171,6 +174,7 @@ google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLY
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
google.golang.org/protobuf v1.21.0 h1:qdOKuR/EIArgaWNjetjgTzgVTAZ+S/WXVrq9HW9zimw=
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
Expand Down
6 changes: 6 additions & 0 deletions store/bigcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ func (s *BigcacheStore) Get(key interface{}) (interface{}, error) {
return item, err
}

// GetWithTTL returns data stored from a given key
func (s *BigcacheStore) GetWithTTL(key interface{}) (interface{}, time.Duration, error) {
item, err := s.Get(key)
return item, 0, err
}

// Set defines data in Redis for given key identifier
func (s *BigcacheStore) Set(key interface{}, value interface{}, options *Options) error {
if options == nil {
Expand Down
4 changes: 4 additions & 0 deletions store/interface.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package store

import (
"time"
)
// StoreInterface is the interface for all available stores
type StoreInterface interface {
Get(key interface{}) (interface{}, error)
GetWithTTL(key interface{}) (interface{}, time.Duration, error)
Set(key interface{}, value interface{}, options *Options) error
Delete(key interface{}) error
Invalidate(options InvalidateOptions) error
Expand Down
13 changes: 13 additions & 0 deletions store/memcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ func (s *MemcacheStore) Get(key interface{}) (interface{}, error) {
return item.Value, err
}

// Get returns data stored from a given key
func (s *MemcacheStore) GetWithTTL(key interface{}) (interface{}, time.Duration, error) {
item, err := s.client.Get(key.(string))
if err != nil {
return nil, 0, err
}
if item == nil {
return nil, 0, errors.New("Unable to retrieve data from memcache")
}

return item.Value, time.Duration(item.Expiration), err
}

// Set defines data in Memcache for given key identifier
func (s *MemcacheStore) Set(key interface{}, value interface{}, options *Options) error {
if options == nil {
Expand Down
16 changes: 16 additions & 0 deletions store/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
// RedisClientInterface represents a go-redis/redis client
type RedisClientInterface interface {
Get(key string) *redis.StringCmd
TTL(key string) *redis.DurationCmd
Set(key string, value interface{}, expiration time.Duration) *redis.StatusCmd
Del(keys ...string) *redis.IntCmd
FlushAll() *redis.StatusCmd
Expand Down Expand Up @@ -46,6 +47,21 @@ func (s *RedisStore) Get(key interface{}) (interface{}, error) {
return s.client.Get(key.(string)).Result()
}

// GetWithTTL returns data stored from a given key and the TTL associated
func (s *RedisStore) GetWithTTL(key interface{}) (interface{}, time.Duration, error) {
object, err := s.client.Get(key.(string)).Result()
if err != nil {
return nil, 0, err
}

ttl, err := s.client.TTL(key.(string)).Result()
if err != nil {
return nil, 0, err
}

return object, ttl, err
}

// Set defines data in Redis for given key identifier
func (s *RedisStore) Set(key interface{}, value interface{}, options *Options) error {
if options == nil {
Expand Down
7 changes: 7 additions & 0 deletions store/ristretto.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ func (s *RistrettoStore) Get(key interface{}) (interface{}, error) {
return value, err
}

// GetWithTTL returns data stored from a given key
func (s *RistrettoStore) GetWithTTL(key interface{}) (interface{}, time.Duration, error) {
item, err := s.Get(key)
return item, 0, err
}


// Set defines data in Ristretto memoey cache for given key identifier
func (s *RistrettoStore) Set(key interface{}, value interface{}, options *Options) error {
var err error
Expand Down
33 changes: 33 additions & 0 deletions test/mocks/cache/cache_interface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 23 additions & 9 deletions test/mocks/store/clients/redis_interface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions test/mocks/store/store_interface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ab7dc64

Please sign in to comment.