Skip to content

Commit

Permalink
implement interface function
Browse files Browse the repository at this point in the history
  • Loading branch information
corinapurcarea committed Aug 12, 2020
1 parent b1c560c commit 46a7306
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 5 deletions.
9 changes: 8 additions & 1 deletion cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"reflect"
"strings"
"time"

"github.com/eko/gocache/codec"
"github.com/eko/gocache/store"
Expand All @@ -20,7 +21,7 @@ type Cache struct {
codec codec.CodecInterface
}

// New instanciates a new cache entry
// New instantiates a new cache entry
func New(store store.StoreInterface) *Cache {
return &Cache{
codec: codec.New(store),
Expand All @@ -33,6 +34,12 @@ func (c *Cache) Get(key interface{}) (interface{}, error) {
return c.codec.Get(cacheKey)
}

// GetWithTTL returns the object stored in cache and its corresponding TTL
func (c *Cache) GetWithTTL(key interface{}) (interface{}, time.Duration, error) {
cacheKey := c.getCacheKey(key)
return c.codec.GetWithTTL(cacheKey)
}

// Set populates the cache item using the given key
func (c *Cache) Set(key, object interface{}, options *store.Options) error {
cacheKey := c.getCacheKey(key)
Expand Down
15 changes: 15 additions & 0 deletions codec/codec.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package codec

import (
"time"

"github.com/eko/gocache/store"
)

Expand Down Expand Up @@ -45,6 +47,19 @@ func (c *Codec) Get(key interface{}) (interface{}, error) {
return val, err
}

// GetWithTTL allows to retrieve the value from a given key identifier and its corresponding TTL
func (c *Codec) GetWithTTL(key interface{}) (interface{}, time.Duration, error) {
val, ttl, err := c.store.GetWithTTL(key)

if err == nil {
c.stats.Hits++
} else {
c.stats.Miss++
}

return val, ttl, err
}

// Set allows to set a value for a given key identifier and also allows to specify
// an expiration time
func (c *Codec) Set(key interface{}, value interface{}, options *store.Options) error {
Expand Down
3 changes: 3 additions & 0 deletions codec/interface.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package codec

import (
"time"

"github.com/eko/gocache/store"
)

// CodecInterface represents an instance of a cache codec
type CodecInterface interface {
Get(key interface{}) (interface{}, error)
GetWithTTL(key interface{}) (interface{}, time.Duration, error)
Set(key interface{}, value interface{}, options *store.Options) error
Delete(key interface{}) error
Invalidate(options store.InvalidateOptions) error
Expand Down
2 changes: 1 addition & 1 deletion store/bigcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (s *BigcacheStore) Get(key interface{}) (interface{}, error) {
return item, err
}

// GetWithTTL returns data stored from a given key
// GetWithTTL returns data stored from a given key and its corresponding TTL
func (s *BigcacheStore) GetWithTTL(key interface{}) (interface{}, time.Duration, error) {
item, err := s.Get(key)
return item, 0, err
Expand Down
2 changes: 1 addition & 1 deletion store/memcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (s *MemcacheStore) Get(key interface{}) (interface{}, error) {
return item.Value, err
}

// Get returns data stored from a given key
// GetWithTTL returns data stored from a given key and its corresponding TTL
func (s *MemcacheStore) GetWithTTL(key interface{}) (interface{}, time.Duration, error) {
item, err := s.client.Get(key.(string))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion store/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ 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
// GetWithTTL returns data stored from a given key and its corresponding TTL
func (s *RedisStore) GetWithTTL(key interface{}) (interface{}, time.Duration, error) {
object, err := s.client.Get(key.(string)).Result()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion store/ristretto.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (s *RistrettoStore) Get(key interface{}) (interface{}, error) {
return value, err
}

// GetWithTTL returns data stored from a given key
// GetWithTTL returns data stored from a given key and its corresponding TTL
func (s *RistrettoStore) GetWithTTL(key interface{}) (interface{}, time.Duration, error) {
value, err := s.Get(key)
return value, 0, err
Expand Down
17 changes: 17 additions & 0 deletions test/mocks/codec/codec_interface.go

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

0 comments on commit 46a7306

Please sign in to comment.