diff --git a/Makefile b/Makefile index 48701ff..1df7725 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,15 @@ .PHONY: mocks mocks: + # mocks mockery -case=snake -name=CacheInterface -dir=cache/ -output test/mocks/cache/ mockery -case=snake -name=CodecInterface -dir=codec/ -output test/mocks/codec/ mockery -case=snake -name=SetterCacheInterface -dir=cache/ -output test/mocks/cache/ mockery -case=snake -name=MetricsInterface -dir=metrics/ -output test/mocks/metrics/ mockery -case=snake -name=StoreInterface -dir=store/ -output test/mocks/store/ - mockery -case=snake -name=BigcacheClientInterface -dir=store/ -output test/mocks/store/ - mockery -case=snake -name=MemcacheClientInterface -dir=store/ -output test/mocks/store/ - mockery -case=snake -name=RedisClientInterface -dir=store/ -output test/mocks/store/ - mockery -case=snake -name=RistrettoClientInterface -dir=store/ -output test/mocks/store/ + + # in package store clients mocks + mockery -case=snake -inpkg -name=BigcacheClientInterface -dir=store/ -output store/ + mockery -case=snake -inpkg -name=MemcacheClientInterface -dir=store/ -output store/ + mockery -case=snake -inpkg -name=RedisClientInterface -dir=store/ -output store/ + mockery -case=snake -inpkg -name=RistrettoClientInterface -dir=store/ -output store/ diff --git a/README.md b/README.md index b7676b2..1307b19 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ Here is what it brings in detail: * ✅ A loadable cache: allow you to call a callback function to put your data back in cache * ✅ A metric cache to let you store metrics about your caches usage (hits, miss, set success, set error, ...) * ✅ A marshaler to automatically marshal/unmarshal your cache values as a struct +* ✅ Define default values in stores and override them when setting data ## Built-in stores @@ -40,10 +41,15 @@ Here is a simple cache instanciation with Redis but you can also look at other a ```go memcacheStore := store.NewMemcache( memcache.New("10.0.0.1:11211", "10.0.0.2:11211", "10.0.0.3:11212"), + &store.Options{ + Expiration: 10*time.Second, + }, ) -cacheManager := cache.New(memcacheStore, &cache.Options{Expiration: 15*time.Second}) -err := cacheManager.Set("my-key", []byte("my-value)) +cacheManager := cache.New(memcacheStore) +err := cacheManager.Set("my-key", []byte("my-value), &cache.Options{ + Expiration: 15*time.Second, // Override default value of 10 seconds defined in the store +}) if err != nil { panic(err) } @@ -55,12 +61,10 @@ value := cacheManager.Get("my-key") ```go bigcacheClient, _ := bigcache.NewBigCache(bigcache.DefaultConfig(5 * time.Minute)) -bigcacheStore := store.NewBigcache( - bigcacheClient, -) +bigcacheStore := store.NewBigcache(bigcacheClient, nil) // No otions provided (as second argument) -cacheManager := cache.New(bigcacheStore, nil) -err := cacheManager.Set("my-key", "my-value") +cacheManager := cache.New(bigcacheStore) +err := cacheManager.Set("my-key", "my-value", nil) if err != nil { panic(err) } @@ -71,14 +75,18 @@ value := cacheManager.Get("my-key") #### Memory (using Ristretto) ```go -ristrettoCache, err := ristretto.NewCache(&ristretto.Config{NumCounters: 1000, MaxCost: 100, BufferItems: 64}) +ristrettoCache, err := ristretto.NewCache(&ristretto.Config{ + NumCounters: 1000, + MaxCost: 100, + BufferItems: 64, +}) if err != nil { panic(err) } -ristrettoStore := store.NewRistretto(ristrettoCache) +ristrettoStore := store.NewRistretto(ristrettoCache, nil) -cacheManager := cache.New(ristrettoStore, nil) -err := cacheManager.Set("my-key", "my-value") +cacheManager := cache.New(ristrettoStore) +err := cacheManager.Set("my-key", "my-value", &cache.Options{Cost: 2}) if err != nil { panic(err) } @@ -89,10 +97,12 @@ value := cacheManager.Get("my-key") #### Redis ```go -redisStore := store.NewRedis(redis.NewClient(&redis.Options{Addr: "127.0.0.1:6379"})) +redisStore := store.NewRedis(redis.NewClient(&redis.Options{ + Addr: "127.0.0.1:6379", +}), nil) -cacheManager := cache.New(redisStore, &cache.Options{Expiration: 15*time.Second}) -err := cacheManager.Set("my-key", "my-value") +cacheManager := cache.New(redisStore) +err := cacheManager.Set("my-key", "my-value", &cache.Options{Expiration: 15*time.Second}) if err != nil { panic(err) } @@ -114,13 +124,13 @@ if err != nil { redisClient := redis.NewClient(&redis.Options{Addr: "127.0.0.1:6379"}) // Initialize stores -ristrettoStore := store.NewRistretto(ristrettoCache) -redisStore := store.NewRedis(redisClient) +ristrettoStore := store.NewRistretto(ristrettoCache, nil) +redisStore := store.NewRedis(redisClient, &cache.Optiobs{Expiration: 5*time.Second}) // Initialize chained cache cacheManager := cache.NewChain( - cache.New(ristrettoStore, nil), - cache.New(redisStore, &cache.Options{Expiration: 15*time.Second}), + cache.New(ristrettoStore), + cache.New(redisStore), ) // ... Then, do what you want with your cache @@ -135,7 +145,7 @@ This cache will provide a load function that acts as a callable function and wil ```go // Initialize Redis client and store redisClient := redis.NewClient(&redis.Options{Addr: "127.0.0.1:6379"}) -redisStore := store.NewRedis(redisClient) +redisStore := store.NewRedis(redisClient, nil) // Initialize a load function that loads your data from a custom source loadFunction := func(key interface{}) (interface{}, error) { @@ -146,7 +156,7 @@ loadFunction := func(key interface{}) (interface{}, error) { // Initialize loadable cache cacheManager := cache.NewLoadable( loadFunction, - cache.New(redisStore, &cache.Options{Expiration: 15*time.Second}), + cache.New(redisStore), ) // ... Then, you can get your data and your function will automatically put them in cache(s) @@ -161,7 +171,7 @@ This cache will record metrics depending on the metric provider you pass to it. ```go // Initialize Redis client and store redisClient := redis.NewClient(&redis.Options{Addr: "127.0.0.1:6379"}) -redisStore := store.NewRedis(redisClient) +redisStore := store.NewRedis(redisClient, nil) // Initializes Prometheus metrics service promMetrics := metrics.NewPrometheus("my-test-app") @@ -169,7 +179,7 @@ promMetrics := metrics.NewPrometheus("my-test-app") // Initialize metric cache cacheManager := cache.NewMetric( promMetrics, - cache.New(redisStore, &cache.Options{Expiration: 15*time.Second}), + cache.New(redisStore), ) // ... Then, you can get your data and metrics will be observed by Prometheus @@ -182,12 +192,12 @@ Some caches like Redis stores and returns the value as a string so you have to m ```go // Initialize Redis client and store redisClient := redis.NewClient(&redis.Options{Addr: "127.0.0.1:6379"}) -redisStore := store.NewRedis(redisClient) +redisStore := store.NewRedis(redisClient, nil) // Initialize chained cache cacheManager := cache.NewMetric( promMetrics, - cache.New(redisStore, &cache.Options{Expiration: 15*time.Second}), + cache.New(redisStore), ) // Initializes marshaler @@ -242,13 +252,20 @@ func main() { // Initialize Prometheus metrics collector promMetrics := metrics.NewPrometheus("my-test-app") - ristrettoCache, err := ristretto.NewCache(&ristretto.Config{NumCounters: 1000, MaxCost: 100, BufferItems: 64}) + // Initialize Ristretto store + ristrettoCache, err := ristretto.NewCache(&ristretto.Config{ + NumCounters: 1000, + MaxCost: 100, + BufferItems: 64, + }) if err != nil { panic(err) } - ristrettoStore := store.NewRistretto(ristrettoCache) - redisStore := store.NewRedis(redis.NewClient(&redis.Options{Addr: "127.0.0.1:6379"})) + ristrettoStore := store.NewRistretto(ristrettoCache, &cache.Options{Cost: 4}) + + // Initialize Redis store + redisStore := store.NewRedis(redis.NewClient(&redis.Options{Addr: "127.0.0.1:6379"}), &cache.Options{Expiration: 5*time.Second}) // Initialize a load function that loads your data from a custom source loadFunction := func(key interface{}) (interface{}, error) { @@ -260,8 +277,8 @@ func main() { // and a load function that will put data back into caches if none has the value cacheManager := cache.NewMetric(promMetrics, cache.NewLoadable(loadFunction, cache.NewChain( - cache.New(ristrettoStore, nil), - cache.New(redisStore, &cache.Options{Expiration: 15*time.Second}), + cache.New(ristrettoStore), + cache.New(redisStore), ), )) @@ -270,7 +287,7 @@ func main() { key := Book{Slug: "my-test-amazing-book"} value := Book{ID: 1, Name: "My test amazing book", Slug: "my-test-amazing-book"} - err = marshaller.Set(key, value) + err = marshaller.Set(key, value, nil) if err != nil { panic(err) } diff --git a/cache/cache.go b/cache/cache.go index 009ac5b..d4493ad 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -14,18 +14,12 @@ const ( // Cache represents the configuration needed by a cache type Cache struct { codec codec.CodecInterface - options *Options } // New instanciates a new cache entry -func New(store store.StoreInterface, options *Options) *Cache { - if options == nil { - options = &Options{} - } - +func New(store store.StoreInterface) *Cache { return &Cache{ codec: codec.New(store), - options: options, } } @@ -36,9 +30,9 @@ func (c *Cache) Get(key interface{}) (interface{}, error) { } // Set populates the cache item using the given key -func (c *Cache) Set(key, object interface{}) error { +func (c *Cache) Set(key, object interface{}, options *store.Options) error { cacheKey := c.getCacheKey(key) - return c.codec.Set(cacheKey, object, c.options.ExpirationValue()) + return c.codec.Set(cacheKey, object, options) } // GetCodec returns the current codec diff --git a/cache/cache_test.go b/cache/cache_test.go index d3acdc8..b63a7c2 100644 --- a/cache/cache_test.go +++ b/cache/cache_test.go @@ -6,6 +6,7 @@ import ( "time" "github.com/eko/gache/codec" + "github.com/eko/gache/store" mocksStore "github.com/eko/gache/test/mocks/store" "github.com/stretchr/testify/assert" @@ -14,24 +15,20 @@ import ( func TestNew(t *testing.T) { // Given store := &mocksStore.StoreInterface{} - options := &Options{ - Expiration: 5 * time.Second, - } // When - cache := New(store, options) + cache := New(store) // Then assert.IsType(t, new(Cache), cache) assert.IsType(t, new(codec.Codec), cache.codec) assert.Equal(t, store, cache.codec.GetStore()) - assert.Equal(t, options, cache.options) } func TestCacheSet(t *testing.T) { // Given - options := &Options{ + options := &store.Options{ Expiration: 5 * time.Second, } @@ -42,19 +39,19 @@ func TestCacheSet(t *testing.T) { } store := &mocksStore.StoreInterface{} - store.On("Set", "9b1ac8a6e8ca8ca9477c0a252eb37756", value, options.ExpirationValue()). + store.On("Set", "9b1ac8a6e8ca8ca9477c0a252eb37756", value, options). Return(nil) - cache := New(store, options) + cache := New(store) // When - err := cache.Set("my-key", value) + err := cache.Set("my-key", value, options) assert.Nil(t, err) } func TestCacheSetWhenErrorOccurs(t *testing.T) { // Given - options := &Options{ + options := &store.Options{ Expiration: 5 * time.Second, } @@ -67,22 +64,18 @@ func TestCacheSetWhenErrorOccurs(t *testing.T) { storeErr := errors.New("An error has occured while inserting data into store") store := &mocksStore.StoreInterface{} - store.On("Set", "9b1ac8a6e8ca8ca9477c0a252eb37756", value, options.ExpirationValue()). + store.On("Set", "9b1ac8a6e8ca8ca9477c0a252eb37756", value, options). Return(storeErr) - cache := New(store, options) + cache := New(store) // When - err := cache.Set("my-key", value) + err := cache.Set("my-key", value, options) assert.Equal(t, storeErr, err) } func TestCacheGet(t *testing.T) { // Given - options := &Options{ - Expiration: 5 * time.Second, - } - cacheValue := &struct { Hello string }{ @@ -92,7 +85,7 @@ func TestCacheGet(t *testing.T) { store := &mocksStore.StoreInterface{} store.On("Get", "9b1ac8a6e8ca8ca9477c0a252eb37756").Return(cacheValue, nil) - cache := New(store, options) + cache := New(store) // When value, err := cache.Get("my-key") @@ -104,16 +97,12 @@ func TestCacheGet(t *testing.T) { func TestCacheGetWhenNotFound(t *testing.T) { // Given - options := &Options{ - Expiration: 5 * time.Second, - } - returnedErr := errors.New("Unable to find item in store") store := &mocksStore.StoreInterface{} store.On("Get", "9b1ac8a6e8ca8ca9477c0a252eb37756").Return(nil, returnedErr) - cache := New(store, options) + cache := New(store) // When value, err := cache.Get("my-key") @@ -126,11 +115,8 @@ func TestCacheGetWhenNotFound(t *testing.T) { func TestCacheGetCodec(t *testing.T) { // Given store := &mocksStore.StoreInterface{} - options := &Options{ - Expiration: 5 * time.Second, - } - cache := New(store, options) + cache := New(store) // When value := cache.GetCodec() @@ -143,11 +129,8 @@ func TestCacheGetCodec(t *testing.T) { func TestCacheGetType(t *testing.T) { // Given store := &mocksStore.StoreInterface{} - options := &Options{ - Expiration: 5 * time.Second, - } - cache := New(store, options) + cache := New(store) // When - Then assert.Equal(t, CacheType, cache.GetType()) diff --git a/cache/chain.go b/cache/chain.go index e981d33..2a26828 100644 --- a/cache/chain.go +++ b/cache/chain.go @@ -2,8 +2,9 @@ package cache import ( "fmt" - "log" + + "github.com/eko/gache/store" ) const ( @@ -43,9 +44,9 @@ func (c *ChainCache) Get(key interface{}) (interface{}, error) { } // Set sets a value in available caches -func (c *ChainCache) Set(key, object interface{}) error { +func (c *ChainCache) Set(key, object interface{}, options *store.Options) error { for _, cache := range c.caches { - err := cache.Set(key, object) + err := cache.Set(key, object, options) if err != nil { storeType := cache.GetCodec().GetStore().GetType() return fmt.Errorf("Unable to set item into cache with store '%s': %v", storeType, err) @@ -62,7 +63,7 @@ func (c *ChainCache) setUntil(key, object interface{}, until *string) error { break } - err := cache.Set(key, object) + err := cache.Set(key, object, nil) if err != nil { storeType := cache.GetCodec().GetStore().GetType() return fmt.Errorf("Unable to set item into cache with store '%s': %v", storeType, err) diff --git a/cache/chain_test.go b/cache/chain_test.go index 32e3067..59c51ad 100644 --- a/cache/chain_test.go +++ b/cache/chain_test.go @@ -4,6 +4,7 @@ import ( "errors" "testing" + "github.com/eko/gache/store" mocksCache "github.com/eko/gache/test/mocks/cache" mocksCodec "github.com/eko/gache/test/mocks/codec" mocksStore "github.com/eko/gache/test/mocks/store" @@ -93,7 +94,7 @@ func TestChainGetWhenAvailableInSecondCache(t *testing.T) { cache1 := &mocksCache.SetterCacheInterface{} cache1.On("GetCodec").Return(codec1) cache1.On("Get", "my-key").Return(nil, errors.New("Unable to find in cache 1")) - cache1.On("Set", "my-key", cacheValue).Return(nil) + cache1.On("Set", "my-key", cacheValue, (*store.Options)(nil)).Return(nil) // Cache 2 store2 := &mocksStore.StoreInterface{} diff --git a/cache/interface.go b/cache/interface.go index 1e6b3ce..ba3fb3f 100644 --- a/cache/interface.go +++ b/cache/interface.go @@ -2,12 +2,13 @@ package cache import ( "github.com/eko/gache/codec" + "github.com/eko/gache/store" ) // CacheInterface represents the interface for all caches (aggregates, metric, memory, redis, ...) type CacheInterface interface { Get(key interface{}) (interface{}, error) - Set(key, object interface{}) error + Set(key, object interface{}, options *store.Options) error GetType() string } diff --git a/cache/loadable.go b/cache/loadable.go index 6f63efb..65a6744 100644 --- a/cache/loadable.go +++ b/cache/loadable.go @@ -2,6 +2,7 @@ package cache import ( "log" + "github.com/eko/gache/store" ) const ( @@ -41,14 +42,14 @@ func (c *LoadableCache) Get(key interface{}) (interface{}, error) { } // Then, put it back in cache - go c.Set(key, object) + go c.Set(key, object, nil) return object, err } // Set sets a value in available caches -func (c *LoadableCache) Set(key, object interface{}) error { - return c.cache.Set(key, object) +func (c *LoadableCache) Set(key, object interface{}, options *store.Options) error { + return c.cache.Set(key, object, options) } // GetType returns the cache type diff --git a/cache/loadable_test.go b/cache/loadable_test.go index 931393b..48a4a2e 100644 --- a/cache/loadable_test.go +++ b/cache/loadable_test.go @@ -4,6 +4,7 @@ import ( "errors" "testing" + "github.com/eko/gache/store" mocksCache "github.com/eko/gache/test/mocks/cache" mocksCodec "github.com/eko/gache/test/mocks/codec" mocksStore "github.com/eko/gache/test/mocks/store" @@ -73,7 +74,7 @@ func TestLoadableGetWhenAvailableInLoadFunc(t *testing.T) { cache1 := &mocksCache.SetterCacheInterface{} cache1.On("GetCodec").Return(codec1) cache1.On("Get", "my-key").Return(nil, errors.New("Unable to find in cache 1")) - cache1.On("Set", "my-key", cacheValue).Return(nil) + cache1.On("Set", "my-key", cacheValue, (*store.Options)(nil)).Return(nil) loadFunc := func(key interface{}) (interface{}, error) { return cacheValue, nil diff --git a/cache/metric.go b/cache/metric.go index 09c61f3..2e80dc2 100644 --- a/cache/metric.go +++ b/cache/metric.go @@ -2,6 +2,7 @@ package cache import ( "github.com/eko/gache/metrics" + "github.com/eko/gache/store" ) const ( @@ -32,8 +33,8 @@ func (c *MetricCache) Get(key interface{}) (interface{}, error) { } // Set sets a value in cache and also records set metric -func (c *MetricCache) Set(key, object interface{}) error { - return c.cache.Set(key, object) +func (c *MetricCache) Set(key, object interface{}, options *store.Options) error { + return c.cache.Set(key, object, options) } // Get obtains a value from cache and also records metrics diff --git a/cache/options.go b/cache/options.go deleted file mode 100644 index 050d8a5..0000000 --- a/cache/options.go +++ /dev/null @@ -1,15 +0,0 @@ -package cache - -import ( - "time" -) - -// Options represents the cache available options -type Options struct { - Expiration time.Duration -} - -// ExpirationValue returns the expiration option value -func (o Options) ExpirationValue() time.Duration { - return o.Expiration -} diff --git a/codec/codec.go b/codec/codec.go index 66c29fa..26eaaf5 100644 --- a/codec/codec.go +++ b/codec/codec.go @@ -1,8 +1,6 @@ package codec import ( - "time" - "github.com/eko/gache/store" ) @@ -43,8 +41,8 @@ func (c *Codec) Get(key interface{}) (interface{}, error) { // 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{}, expiration time.Duration) error { - err := c.store.Set(key, value, expiration) +func (c *Codec) Set(key interface{}, value interface{}, options *store.Options) error { + err := c.store.Set(key, value, options) if err == nil { c.stats.SetSuccess++ diff --git a/codec/codec_test.go b/codec/codec_test.go index a40e6b3..465954b 100644 --- a/codec/codec_test.go +++ b/codec/codec_test.go @@ -5,6 +5,7 @@ import ( "testing" "time" + "github.com/eko/gache/store" mocksStore "github.com/eko/gache/test/mocks/store" "github.com/stretchr/testify/assert" ) @@ -76,15 +77,17 @@ func TestSetWhenSuccess(t *testing.T) { Hello: "world", } - expiration := 5 * time.Second + options := &store.Options{ + Expiration: 5 * time.Second, + } store := &mocksStore.StoreInterface{} - store.On("Set", "my-key", cacheValue, expiration).Return(nil) + store.On("Set", "my-key", cacheValue, options).Return(nil) codec := New(store) // When - err := codec.Set("my-key", cacheValue, expiration) + err := codec.Set("my-key", cacheValue, options) // Then assert.Nil(t, err) @@ -103,17 +106,19 @@ func TestSetWhenError(t *testing.T) { Hello: "world", } - expiration := 5 * time.Second + options := &store.Options{ + Expiration: 5 * time.Second, + } expectedErr := errors.New("Unable to set value in store") store := &mocksStore.StoreInterface{} - store.On("Set", "my-key", cacheValue, expiration).Return(expectedErr) + store.On("Set", "my-key", cacheValue, options).Return(expectedErr) codec := New(store) // When - err := codec.Set("my-key", cacheValue, expiration) + err := codec.Set("my-key", cacheValue, options) // Then assert.Equal(t, expectedErr, err) diff --git a/codec/interface.go b/codec/interface.go index 85ca526..c3304af 100644 --- a/codec/interface.go +++ b/codec/interface.go @@ -1,15 +1,13 @@ package codec import ( - "time" - "github.com/eko/gache/store" ) // CodecInterface represents an instance of a cache codec type CodecInterface interface { Get(key interface{}) (interface{}, error) - Set(key interface{}, value interface{}, expiration time.Duration) error + Set(key interface{}, value interface{}, options *store.Options) error GetStore() store.StoreInterface GetStats() *Stats diff --git a/marshaler/marshaler.go b/marshaler/marshaler.go index e8ed00e..379767c 100644 --- a/marshaler/marshaler.go +++ b/marshaler/marshaler.go @@ -2,6 +2,7 @@ package marshaler import ( "github.com/eko/gache/cache" + "github.com/eko/gache/store" "github.com/vmihailenco/msgpack" ) @@ -40,11 +41,11 @@ func (c *Marshaler) Get(key interface{}, returnObj interface{}) (interface{}, er } // Set sets a value in cache by marshaling value -func (c *Marshaler) Set(key, object interface{}) error { +func (c *Marshaler) Set(key, object interface{}, options *store.Options) error { bytes, err := msgpack.Marshal(object) if err != nil { return err } - return c.cache.Set(key, bytes) + return c.cache.Set(key, bytes, options) } diff --git a/marshaler/marshaler_test.go b/marshaler/marshaler_test.go index f4e4713..b18849c 100644 --- a/marshaler/marshaler_test.go +++ b/marshaler/marshaler_test.go @@ -3,7 +3,9 @@ package marshaler import ( "errors" "testing" + "time" + "github.com/eko/gache/store" mocksCache "github.com/eko/gache/test/mocks/cache" "github.com/stretchr/testify/assert" "github.com/vmihailenco/msgpack" @@ -111,13 +113,17 @@ func TestSetWhenStruct(t *testing.T) { Hello: "world", } + options := &store.Options{ + Expiration: 5 * time.Second, + } + cache := &mocksCache.CacheInterface{} - cache.On("Set", "my-key", []byte{0x81, 0xa5, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0xa5, 0x77, 0x6f, 0x72, 0x6c, 0x64}).Return(nil) + cache.On("Set", "my-key", []byte{0x81, 0xa5, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0xa5, 0x77, 0x6f, 0x72, 0x6c, 0x64}, options).Return(nil) marshaler := New(cache) // When - err := marshaler.Set("my-key", cacheValue) + err := marshaler.Set("my-key", cacheValue, options) // Then assert.Nil(t, err) @@ -127,13 +133,17 @@ func TestSetWhenString(t *testing.T) { // Given cacheValue := "test" + options := &store.Options{ + Expiration: 5 * time.Second, + } + cache := &mocksCache.CacheInterface{} - cache.On("Set", "my-key", []byte{0xa4, 0x74, 0x65, 0x73, 0x74}).Return(nil) + cache.On("Set", "my-key", []byte{0xa4, 0x74, 0x65, 0x73, 0x74}, options).Return(nil) marshaler := New(cache) // When - err := marshaler.Set("my-key", cacheValue) + err := marshaler.Set("my-key", cacheValue, options) // Then assert.Nil(t, err) diff --git a/store/bigcache.go b/store/bigcache.go index 849820b..5d17b4c 100644 --- a/store/bigcache.go +++ b/store/bigcache.go @@ -2,7 +2,6 @@ package store import ( "errors" - "time" ) // BigcacheClientInterface represents a allegro/bigcache client @@ -18,12 +17,18 @@ const ( // BigcacheStore is a store for Redis type BigcacheStore struct { client BigcacheClientInterface + options *Options } // NewBigcache creates a new store to Bigcache instance(s) -func NewBigcache(client BigcacheClientInterface) *BigcacheStore { +func NewBigcache(client BigcacheClientInterface, options *Options) *BigcacheStore { + if options == nil { + options = &Options{} + } + return &BigcacheStore{ client: client, + options: options, } } @@ -41,7 +46,7 @@ func (s *BigcacheStore) Get(key interface{}) (interface{}, error) { } // Set defines data in Redis for given key idntifier -func (s *BigcacheStore) Set(key interface{}, value interface{}, expiration time.Duration) error { +func (s *BigcacheStore) Set(key interface{}, value interface{}, options *Options) error { return s.client.Set(key.(string), value.([]byte)) } diff --git a/store/bigcache_test.go b/store/bigcache_test.go index 63abfbe..40d35b5 100644 --- a/store/bigcache_test.go +++ b/store/bigcache_test.go @@ -2,22 +2,21 @@ package store import ( "testing" - "time" - mocksStore "github.com/eko/gache/test/mocks/store" "github.com/stretchr/testify/assert" ) func TestNewBigcache(t *testing.T) { // Given - client := &mocksStore.BigcacheClientInterface{} + client := &MockBigcacheClientInterface{} // When - store := NewBigcache(client) + store := NewBigcache(client, nil) // Then assert.IsType(t, new(BigcacheStore), store) assert.Equal(t, client, store.client) + assert.IsType(t, new(Options), store.options) } func TestBigcacheGet(t *testing.T) { @@ -25,10 +24,10 @@ func TestBigcacheGet(t *testing.T) { cacheKey := "my-key" cacheValue := []byte("my-cache-value") - client := &mocksStore.BigcacheClientInterface{} + client := &MockBigcacheClientInterface{} client.On("Get", cacheKey).Return(cacheValue, nil) - store := NewBigcache(client) + store := NewBigcache(client, nil) // When value, err := store.Get(cacheKey) @@ -42,15 +41,14 @@ func TestBigcacheSet(t *testing.T) { // Given cacheKey := "my-key" cacheValue := []byte("my-cache-value") - expiration := 5 * time.Second - client := &mocksStore.BigcacheClientInterface{} + client := &MockBigcacheClientInterface{} client.On("Set", cacheKey, cacheValue).Return(nil) - store := NewBigcache(client) + store := NewBigcache(client, nil) // When - err := store.Set(cacheKey, cacheValue, expiration) + err := store.Set(cacheKey, cacheValue, nil) // Then assert.Nil(t, err) @@ -58,9 +56,9 @@ func TestBigcacheSet(t *testing.T) { func TestBigcacheGetType(t *testing.T) { // Given - client := &mocksStore.BigcacheClientInterface{} + client := &MockBigcacheClientInterface{} - store := NewBigcache(client) + store := NewBigcache(client, nil) // When - Then assert.Equal(t, BigcacheType, store.GetType()) diff --git a/store/interface.go b/store/interface.go index 40bd09f..565d950 100644 --- a/store/interface.go +++ b/store/interface.go @@ -1,12 +1,8 @@ package store -import ( - "time" -) - // StoreInterface is the interface for all available stores type StoreInterface interface { Get(key interface{}) (interface{}, error) - Set(key interface{}, value interface{}, expiration time.Duration) error + Set(key interface{}, value interface{}, options *Options) error GetType() string } diff --git a/store/memcache.go b/store/memcache.go index 42d1769..40165c2 100644 --- a/store/memcache.go +++ b/store/memcache.go @@ -2,7 +2,6 @@ package store import ( "errors" - "time" "github.com/bradfitz/gomemcache/memcache" ) @@ -20,12 +19,18 @@ const ( // MemcacheStore is a store for Redis type MemcacheStore struct { client MemcacheClientInterface + options *Options } // NewMemcache creates a new store to Memcache instance(s) -func NewMemcache(client MemcacheClientInterface) *MemcacheStore { +func NewMemcache(client MemcacheClientInterface, options *Options) *MemcacheStore { + if options == nil { + options = &Options{} + } + return &MemcacheStore{ client: client, + options: options, } } @@ -43,11 +48,15 @@ func (s *MemcacheStore) Get(key interface{}) (interface{}, error) { } // Set defines data in Redis for given key idntifier -func (s *MemcacheStore) Set(key interface{}, value interface{}, expiration time.Duration) error { +func (s *MemcacheStore) Set(key interface{}, value interface{}, options *Options) error { + if options == nil { + options = s.options + } + item := &memcache.Item{ Key: key.(string), Value: value.([]byte), - Expiration: int32(expiration.Seconds()), + Expiration: int32(options.ExpirationValue().Seconds()), } return s.client.Set(item) diff --git a/store/memcache_test.go b/store/memcache_test.go index ac831f5..bf70ddb 100644 --- a/store/memcache_test.go +++ b/store/memcache_test.go @@ -5,33 +5,36 @@ import ( "time" "github.com/bradfitz/gomemcache/memcache" - mocksStore "github.com/eko/gache/test/mocks/store" "github.com/stretchr/testify/assert" ) func TestNewMemcache(t *testing.T) { // Given - client := &mocksStore.MemcacheClientInterface{} + client := &MockMemcacheClientInterface{} + options := &Options{Expiration: 3*time.Second} // When - store := NewMemcache(client) + store := NewMemcache(client, options) // Then assert.IsType(t, new(MemcacheStore), store) assert.Equal(t, client, store.client) + assert.Equal(t, options, store.options) } func TestMemcacheGet(t *testing.T) { // Given + options := &Options{Expiration: 3*time.Second} + cacheKey := "my-key" cacheValue := []byte("my-cache-value") - client := &mocksStore.MemcacheClientInterface{} + client := &MockMemcacheClientInterface{} client.On("Get", cacheKey).Return(&memcache.Item{ Value: cacheValue, }, nil) - store := NewMemcache(client) + store := NewMemcache(client, options) // When value, err := store.Get(cacheKey) @@ -43,21 +46,24 @@ func TestMemcacheGet(t *testing.T) { func TestMemcacheSet(t *testing.T) { // Given + options := &Options{Expiration: 3*time.Second} + cacheKey := "my-key" cacheValue := []byte("my-cache-value") - expiration := 5 * time.Second - client := &mocksStore.MemcacheClientInterface{} + client := &MockMemcacheClientInterface{} client.On("Set", &memcache.Item{ Key: cacheKey, Value: cacheValue, - Expiration: int32(expiration.Seconds()), + Expiration: int32(5), }).Return(nil) - store := NewMemcache(client) + store := NewMemcache(client, options) // When - err := store.Set(cacheKey, cacheValue, expiration) + err := store.Set(cacheKey, cacheValue, &Options{ + Expiration: 5 * time.Second, + }) // Then assert.Nil(t, err) @@ -65,9 +71,9 @@ func TestMemcacheSet(t *testing.T) { func TestMemcacheGetType(t *testing.T) { // Given - client := &mocksStore.MemcacheClientInterface{} + client := &MockMemcacheClientInterface{} - store := NewMemcache(client) + store := NewMemcache(client, nil) // When - Then assert.Equal(t, MemcacheType, store.GetType()) diff --git a/test/mocks/store/bigcache_client_interface.go b/store/mock_bigcache_client_interface.go similarity index 70% rename from test/mocks/store/bigcache_client_interface.go rename to store/mock_bigcache_client_interface.go index 46eecf8..95843ee 100644 --- a/test/mocks/store/bigcache_client_interface.go +++ b/store/mock_bigcache_client_interface.go @@ -1,16 +1,16 @@ // Code generated by mockery v1.0.0. DO NOT EDIT. -package mocks +package store import mock "github.com/stretchr/testify/mock" -// BigcacheClientInterface is an autogenerated mock type for the BigcacheClientInterface type -type BigcacheClientInterface struct { +// MockBigcacheClientInterface is an autogenerated mock type for the BigcacheClientInterface type +type MockBigcacheClientInterface struct { mock.Mock } // Get provides a mock function with given fields: key -func (_m *BigcacheClientInterface) Get(key string) ([]byte, error) { +func (_m *MockBigcacheClientInterface) Get(key string) ([]byte, error) { ret := _m.Called(key) var r0 []byte @@ -33,7 +33,7 @@ func (_m *BigcacheClientInterface) Get(key string) ([]byte, error) { } // Set provides a mock function with given fields: key, entry -func (_m *BigcacheClientInterface) Set(key string, entry []byte) error { +func (_m *MockBigcacheClientInterface) Set(key string, entry []byte) error { ret := _m.Called(key, entry) var r0 error diff --git a/test/mocks/store/memcache_client_interface.go b/store/mock_memcache_client_interface.go similarity index 71% rename from test/mocks/store/memcache_client_interface.go rename to store/mock_memcache_client_interface.go index 96a7471..0f87d8d 100644 --- a/test/mocks/store/memcache_client_interface.go +++ b/store/mock_memcache_client_interface.go @@ -1,17 +1,17 @@ // Code generated by mockery v1.0.0. DO NOT EDIT. -package mocks +package store import memcache "github.com/bradfitz/gomemcache/memcache" import mock "github.com/stretchr/testify/mock" -// MemcacheClientInterface is an autogenerated mock type for the MemcacheClientInterface type -type MemcacheClientInterface struct { +// MockMemcacheClientInterface is an autogenerated mock type for the MemcacheClientInterface type +type MockMemcacheClientInterface struct { mock.Mock } // Get provides a mock function with given fields: key -func (_m *MemcacheClientInterface) Get(key string) (*memcache.Item, error) { +func (_m *MockMemcacheClientInterface) Get(key string) (*memcache.Item, error) { ret := _m.Called(key) var r0 *memcache.Item @@ -34,7 +34,7 @@ func (_m *MemcacheClientInterface) Get(key string) (*memcache.Item, error) { } // Set provides a mock function with given fields: item -func (_m *MemcacheClientInterface) Set(item *memcache.Item) error { +func (_m *MockMemcacheClientInterface) Set(item *memcache.Item) error { ret := _m.Called(item) var r0 error diff --git a/test/mocks/store/redis_client_interface.go b/store/mock_redis_client_interface.go similarity index 71% rename from test/mocks/store/redis_client_interface.go rename to store/mock_redis_client_interface.go index a87c219..f18fe51 100644 --- a/test/mocks/store/redis_client_interface.go +++ b/store/mock_redis_client_interface.go @@ -1,19 +1,18 @@ // Code generated by mockery v1.0.0. DO NOT EDIT. -package mocks +package store import mock "github.com/stretchr/testify/mock" import redis "github.com/go-redis/redis/v7" - import time "time" -// RedisClientInterface is an autogenerated mock type for the RedisClientInterface type -type RedisClientInterface struct { +// MockRedisClientInterface is an autogenerated mock type for the RedisClientInterface type +type MockRedisClientInterface struct { mock.Mock } // Get provides a mock function with given fields: key -func (_m *RedisClientInterface) Get(key string) *redis.StringCmd { +func (_m *MockRedisClientInterface) Get(key string) *redis.StringCmd { ret := _m.Called(key) var r0 *redis.StringCmd @@ -29,7 +28,7 @@ func (_m *RedisClientInterface) Get(key string) *redis.StringCmd { } // Set provides a mock function with given fields: key, value, expiration -func (_m *RedisClientInterface) Set(key string, value interface{}, expiration time.Duration) *redis.StatusCmd { +func (_m *MockRedisClientInterface) Set(key string, value interface{}, expiration time.Duration) *redis.StatusCmd { ret := _m.Called(key, value, expiration) var r0 *redis.StatusCmd diff --git a/test/mocks/store/ristretto_client_interface.go b/store/mock_ristretto_client_interface.go similarity index 69% rename from test/mocks/store/ristretto_client_interface.go rename to store/mock_ristretto_client_interface.go index de84648..ccf17b5 100644 --- a/test/mocks/store/ristretto_client_interface.go +++ b/store/mock_ristretto_client_interface.go @@ -1,16 +1,16 @@ // Code generated by mockery v1.0.0. DO NOT EDIT. -package mocks +package store import mock "github.com/stretchr/testify/mock" -// RistrettoClientInterface is an autogenerated mock type for the RistrettoClientInterface type -type RistrettoClientInterface struct { +// MockRistrettoClientInterface is an autogenerated mock type for the RistrettoClientInterface type +type MockRistrettoClientInterface struct { mock.Mock } // Get provides a mock function with given fields: key -func (_m *RistrettoClientInterface) Get(key interface{}) (interface{}, bool) { +func (_m *MockRistrettoClientInterface) Get(key interface{}) (interface{}, bool) { ret := _m.Called(key) var r0 interface{} @@ -33,7 +33,7 @@ func (_m *RistrettoClientInterface) Get(key interface{}) (interface{}, bool) { } // Set provides a mock function with given fields: key, value, cost -func (_m *RistrettoClientInterface) Set(key interface{}, value interface{}, cost int64) bool { +func (_m *MockRistrettoClientInterface) Set(key interface{}, value interface{}, cost int64) bool { ret := _m.Called(key, value, cost) var r0 bool diff --git a/store/options.go b/store/options.go new file mode 100644 index 0000000..c5fa66b --- /dev/null +++ b/store/options.go @@ -0,0 +1,25 @@ +package store + +import ( + "time" +) + +// Options represents the cache store available options +type Options struct { + // Cost corresponds to the memory capacity used by the item when setting a value + // Actually it seems to be used by Ristretto library only + Cost int64 + + // Expiration allows to specify an expiration time when setting a value + Expiration time.Duration +} + +// CostValue returns the allocated memory capacity +func (o Options) CostValue() int64 { + return o.Cost +} + +// ExpirationValue returns the expiration option value +func (o Options) ExpirationValue() time.Duration { + return o.Expiration +} diff --git a/cache/options_test.go b/store/options_test.go similarity index 60% rename from cache/options_test.go rename to store/options_test.go index dbcbfc8..701a4dc 100644 --- a/cache/options_test.go +++ b/store/options_test.go @@ -1,4 +1,4 @@ -package cache +package store import ( "testing" @@ -7,6 +7,16 @@ import ( "github.com/stretchr/testify/assert" ) +func TestOptionsCostValue(t *testing.T) { + // Given + options := Options{ + Cost: 7, + } + + // When - Then + assert.Equal(t, int64(7), options.CostValue()) +} + func TestOptionsExpirationValue(t *testing.T) { // Given options := Options{ diff --git a/store/redis.go b/store/redis.go index abff2a8..2599ddc 100644 --- a/store/redis.go +++ b/store/redis.go @@ -19,12 +19,18 @@ const ( // RedisStore is a store for Redis type RedisStore struct { client RedisClientInterface + options *Options } // NewRedis creates a new store to Redis instance(s) -func NewRedis(client RedisClientInterface) *RedisStore { +func NewRedis(client RedisClientInterface, options *Options) *RedisStore { + if options == nil { + options = &Options{} + } + return &RedisStore{ client: client, + options: options, } } @@ -34,8 +40,12 @@ func (s *RedisStore) Get(key interface{}) (interface{}, error) { } // Set defines data in Redis for given key idntifier -func (s *RedisStore) Set(key interface{}, value interface{}, expiration time.Duration) error { - return s.client.Set(key.(string), value, expiration).Err() +func (s *RedisStore) Set(key interface{}, value interface{}, options *Options) error { + if options == nil { + options = s.options + } + + return s.client.Set(key.(string), value, options.ExpirationValue()).Err() } // GetType returns the store type diff --git a/store/redis_test.go b/store/redis_test.go index c34c6b3..4d14868 100644 --- a/store/redis_test.go +++ b/store/redis_test.go @@ -4,29 +4,32 @@ import ( "testing" "time" - mocksStore "github.com/eko/gache/test/mocks/store" "github.com/go-redis/redis/v7" "github.com/stretchr/testify/assert" ) func TestNewRedis(t *testing.T) { // Given - client := &mocksStore.RedisClientInterface{} + client := &MockRedisClientInterface{} + options := &Options{ + Expiration: 6 * time.Second, + } // When - store := NewRedis(client) + store := NewRedis(client, options) // Then assert.IsType(t, new(RedisStore), store) assert.Equal(t, client, store.client) + assert.Equal(t, options, store.options) } func TestRedisGet(t *testing.T) { // Given - client := &mocksStore.RedisClientInterface{} + client := &MockRedisClientInterface{} client.On("Get", "my-key").Return(&redis.StringCmd{}) - store := NewRedis(client) + store := NewRedis(client, nil) // When value, err := store.Get("my-key") @@ -40,15 +43,19 @@ func TestRedisSet(t *testing.T) { // Given cacheKey := "my-key" cacheValue := "my-cache-value" - expiration := 5 * time.Second + options := &Options{ + Expiration: 6 * time.Second, + } - client := &mocksStore.RedisClientInterface{} - client.On("Set", "my-key", cacheValue, expiration).Return(&redis.StatusCmd{}) + client := &MockRedisClientInterface{} + client.On("Set", "my-key", cacheValue, 5*time.Second).Return(&redis.StatusCmd{}) - store := NewRedis(client) + store := NewRedis(client, options) // When - err := store.Set(cacheKey, cacheValue, expiration) + err := store.Set(cacheKey, cacheValue, &Options{ + Expiration: 5 * time.Second, + }) // Then assert.Nil(t, err) @@ -56,9 +63,9 @@ func TestRedisSet(t *testing.T) { func TestRedisGetType(t *testing.T) { // Given - client := &mocksStore.RedisClientInterface{} + client := &MockRedisClientInterface{} - store := NewRedis(client) + store := NewRedis(client, nil) // When - Then assert.Equal(t, RedisType, store.GetType()) diff --git a/store/ristretto.go b/store/ristretto.go index 5b8a165..c2a1f84 100644 --- a/store/ristretto.go +++ b/store/ristretto.go @@ -3,7 +3,6 @@ package store import ( "errors" "fmt" - "time" ) const ( @@ -19,12 +18,18 @@ type RistrettoClientInterface interface { // RistrettoStore is a store for Ristretto (memory) library type RistrettoStore struct { client RistrettoClientInterface + options *Options } // NewRistretto creates a new store to Ristretto (memory) library instance -func NewRistretto(client RistrettoClientInterface) *RistrettoStore { +func NewRistretto(client RistrettoClientInterface, options *Options) *RistrettoStore { + if options == nil { + options = &Options{} + } + return &RistrettoStore{ client: client, + options: options, } } @@ -41,10 +46,14 @@ func (s *RistrettoStore) Get(key interface{}) (interface{}, error) { } // Set defines data in Ristretto memoey cache for given key idntifier -func (s *RistrettoStore) Set(key interface{}, value interface{}, expiration time.Duration) error { +func (s *RistrettoStore) Set(key interface{}, value interface{}, options *Options) error { var err error - if set := s.client.Set(key, value, 1); !set { + if options == nil { + options = s.options + } + + if set := s.client.Set(key, value, options.CostValue()); !set { err = fmt.Errorf("An error has occured while setting value '%v' on key '%v'", value, key) } diff --git a/store/ristretto_test.go b/store/ristretto_test.go index 0a6b772..3f9ec65 100644 --- a/store/ristretto_test.go +++ b/store/ristretto_test.go @@ -2,22 +2,24 @@ package store import ( "testing" - "time" - mocksStore "github.com/eko/gache/test/mocks/store" "github.com/stretchr/testify/assert" ) func TestNewRistretto(t *testing.T) { // Given - client := &mocksStore.RistrettoClientInterface{} + client := &MockRistrettoClientInterface{} + options := &Options{ + Cost: 8, + } // When - store := NewRistretto(client) + store := NewRistretto(client, options) // Then assert.IsType(t, new(RistrettoStore), store) assert.Equal(t, client, store.client) + assert.Equal(t, options, store.options) } func TestRistrettoGet(t *testing.T) { @@ -25,10 +27,10 @@ func TestRistrettoGet(t *testing.T) { cacheKey := "my-key" cacheValue := "my-cache-value" - client := &mocksStore.RistrettoClientInterface{} + client := &MockRistrettoClientInterface{} client.On("Get", cacheKey).Return(cacheValue, true) - store := NewRistretto(client) + store := NewRistretto(client, nil) // When value, err := store.Get(cacheKey) @@ -42,15 +44,19 @@ func TestRistrettoSet(t *testing.T) { // Given cacheKey := "my-key" cacheValue := "my-cache-value" - expiration := 5 * time.Second + options := &Options{ + Cost: 7, + } - client := &mocksStore.RistrettoClientInterface{} - client.On("Set", cacheKey, cacheValue, int64(1)).Return(true) + client := &MockRistrettoClientInterface{} + client.On("Set", cacheKey, cacheValue, int64(4)).Return(true) - store := NewRistretto(client) + store := NewRistretto(client, options) // When - err := store.Set(cacheKey, cacheValue, expiration) + err := store.Set(cacheKey, cacheValue, &Options{ + Cost: 4, + }) // Then assert.Nil(t, err) @@ -58,9 +64,9 @@ func TestRistrettoSet(t *testing.T) { func TestRistrettoGetType(t *testing.T) { // Given - client := &mocksStore.RistrettoClientInterface{} + client := &MockRistrettoClientInterface{} - store := NewRistretto(client) + store := NewRistretto(client, nil) // When - Then assert.Equal(t, RistrettoType, store.GetType()) diff --git a/test/mocks/cache/cache_interface.go b/test/mocks/cache/cache_interface.go index 6b865b7..296547d 100644 --- a/test/mocks/cache/cache_interface.go +++ b/test/mocks/cache/cache_interface.go @@ -3,6 +3,7 @@ package mocks import mock "github.com/stretchr/testify/mock" +import store "github.com/eko/gache/store" // CacheInterface is an autogenerated mock type for the CacheInterface type type CacheInterface struct { @@ -46,13 +47,13 @@ func (_m *CacheInterface) GetType() string { return r0 } -// Set provides a mock function with given fields: key, object -func (_m *CacheInterface) Set(key interface{}, object interface{}) error { - ret := _m.Called(key, object) +// Set provides a mock function with given fields: key, object, options +func (_m *CacheInterface) Set(key interface{}, object interface{}, options *store.Options) error { + ret := _m.Called(key, object, options) var r0 error - if rf, ok := ret.Get(0).(func(interface{}, interface{}) error); ok { - r0 = rf(key, object) + if rf, ok := ret.Get(0).(func(interface{}, interface{}, *store.Options) error); ok { + r0 = rf(key, object, options) } else { r0 = ret.Error(0) } diff --git a/test/mocks/cache/setter_cache_interface.go b/test/mocks/cache/setter_cache_interface.go index 4695c2b..694f2b7 100644 --- a/test/mocks/cache/setter_cache_interface.go +++ b/test/mocks/cache/setter_cache_interface.go @@ -4,6 +4,7 @@ package mocks import codec "github.com/eko/gache/codec" import mock "github.com/stretchr/testify/mock" +import store "github.com/eko/gache/store" // SetterCacheInterface is an autogenerated mock type for the SetterCacheInterface type type SetterCacheInterface struct { @@ -63,13 +64,13 @@ func (_m *SetterCacheInterface) GetType() string { return r0 } -// Set provides a mock function with given fields: key, object -func (_m *SetterCacheInterface) Set(key interface{}, object interface{}) error { - ret := _m.Called(key, object) +// Set provides a mock function with given fields: key, object, options +func (_m *SetterCacheInterface) Set(key interface{}, object interface{}, options *store.Options) error { + ret := _m.Called(key, object, options) var r0 error - if rf, ok := ret.Get(0).(func(interface{}, interface{}) error); ok { - r0 = rf(key, object) + if rf, ok := ret.Get(0).(func(interface{}, interface{}, *store.Options) error); ok { + r0 = rf(key, object, options) } else { r0 = ret.Error(0) } diff --git a/test/mocks/codec/codec_interface.go b/test/mocks/codec/codec_interface.go index 526632b..df28541 100644 --- a/test/mocks/codec/codec_interface.go +++ b/test/mocks/codec/codec_interface.go @@ -5,7 +5,6 @@ package mocks import codec "github.com/eko/gache/codec" import mock "github.com/stretchr/testify/mock" import store "github.com/eko/gache/store" -import time "time" // CodecInterface is an autogenerated mock type for the CodecInterface type type CodecInterface struct { @@ -67,13 +66,13 @@ func (_m *CodecInterface) GetStore() store.StoreInterface { return r0 } -// Set provides a mock function with given fields: key, value, expiration -func (_m *CodecInterface) Set(key interface{}, value interface{}, expiration time.Duration) error { - ret := _m.Called(key, value, expiration) +// Set provides a mock function with given fields: key, value, options +func (_m *CodecInterface) Set(key interface{}, value interface{}, options *store.Options) error { + ret := _m.Called(key, value, options) var r0 error - if rf, ok := ret.Get(0).(func(interface{}, interface{}, time.Duration) error); ok { - r0 = rf(key, value, expiration) + if rf, ok := ret.Get(0).(func(interface{}, interface{}, *store.Options) error); ok { + r0 = rf(key, value, options) } else { r0 = ret.Error(0) } diff --git a/test/mocks/store/store_interface.go b/test/mocks/store/store_interface.go index 5160e5c..492a85d 100644 --- a/test/mocks/store/store_interface.go +++ b/test/mocks/store/store_interface.go @@ -3,8 +3,7 @@ package mocks import mock "github.com/stretchr/testify/mock" - -import time "time" +import store "github.com/eko/gache/store" // StoreInterface is an autogenerated mock type for the StoreInterface type type StoreInterface struct { @@ -48,13 +47,13 @@ func (_m *StoreInterface) GetType() string { return r0 } -// Set provides a mock function with given fields: key, value, expiration -func (_m *StoreInterface) Set(key interface{}, value interface{}, expiration time.Duration) error { - ret := _m.Called(key, value, expiration) +// Set provides a mock function with given fields: key, value, options +func (_m *StoreInterface) Set(key interface{}, value interface{}, options *store.Options) error { + ret := _m.Called(key, value, options) var r0 error - if rf, ok := ret.Get(0).(func(interface{}, interface{}, time.Duration) error); ok { - r0 = rf(key, value, expiration) + if rf, ok := ret.Get(0).(func(interface{}, interface{}, *store.Options) error); ok { + r0 = rf(key, value, options) } else { r0 = ret.Error(0) }