Skip to content

Commit

Permalink
Merge pull request #90 from eko/added-context
Browse files Browse the repository at this point in the history
Added context to cache methods
  • Loading branch information
eko committed May 28, 2021
2 parents 2a85009 + a61fbe3 commit 4914d74
Show file tree
Hide file tree
Showing 56 changed files with 1,566 additions and 1,395 deletions.
1 change: 0 additions & 1 deletion Makefile
Expand Up @@ -9,7 +9,6 @@ mocks:
mockgen -source=store/memcache.go -destination=test/mocks/store/clients/memcache_interface.go -package=mocks
mockgen -source=store/redis.go -destination=test/mocks/store/clients/redis_interface.go -package=mocks
mockgen -source=store/rediscluster.go -destination=test/mocks/store/clients/rediscluster_interface.go -package=mocks
mockgen -source=store/pegasus.go -destination=test/mocks/store/clients/pegasus_interface.go -package=mocks
mockgen -source=store/ristretto.go -destination=test/mocks/store/clients/ristretto_interface.go -package=mocks
mockgen -source=store/freecache.go -destination=test/mocks/store/clients/freecache_interface.go -package=mocks
mockgen -source=store/go_cache.go -destination=test/mocks/store/clients/go_cache_interface.go -package=mocks
Expand Down
62 changes: 32 additions & 30 deletions README.md
Expand Up @@ -52,18 +52,18 @@ memcacheStore := store.NewMemcache(
)

cacheManager := cache.New(memcacheStore)
err := cacheManager.Set("my-key", []byte("my-value"), &store.Options{
err := cacheManager.Set(ctx, "my-key", []byte("my-value"), &store.Options{
Expiration: 15*time.Second, // Override default value of 10 seconds defined in the store
})
if err != nil {
panic(err)
}

value := cacheManager.Get("my-key")
value := cacheManager.Get(ctx, "my-key")

cacheManager.Delete("my-key")
cacheManager.Delete(ctx, "my-key")

cacheManager.Clear() // Clears the entire cache, in case you want to flush all cache
cacheManager.Clear(ctx) // Clears the entire cache, in case you want to flush all cache
```

#### Memory (using Bigcache)
Expand All @@ -73,12 +73,12 @@ bigcacheClient, _ := bigcache.NewBigCache(bigcache.DefaultConfig(5 * time.Minute
bigcacheStore := store.NewBigcache(bigcacheClient, nil) // No otions provided (as second argument)

cacheManager := cache.New(bigcacheStore)
err := cacheManager.Set("my-key", []byte("my-value"), nil)
err := cacheManager.Set(ctx, "my-key", []byte("my-value"), nil)
if err != nil {
panic(err)
}

value := cacheManager.Get("my-key")
value := cacheManager.Get(ctx, "my-key")
```

#### Memory (using Ristretto)
Expand All @@ -95,14 +95,14 @@ if err != nil {
ristrettoStore := store.NewRistretto(ristrettoCache, nil)

cacheManager := cache.New(ristrettoStore)
err := cacheManager.Set("my-key", "my-value", &store.Options{Cost: 2})
err := cacheManager.Set(ctx, "my-key", "my-value", &store.Options{Cost: 2})
if err != nil {
panic(err)
}

value := cacheManager.Get("my-key")
value := cacheManager.Get(ctx, "my-key")

cacheManager.Delete("my-key")
cacheManager.Delete(ctx, "my-key")
```

#### Redis
Expand All @@ -118,7 +118,7 @@ if err != nil {
panic(err)
}

value, err := cacheManager.Get("my-key")
value, err := cacheManager.Get(ctx, "my-key")
switch err {
case nil:
fmt.Printf("Get the key '%s' from the redis cache. Result: %s", "my-key", value)
Expand All @@ -137,12 +137,12 @@ freecacheStore := store.NewFreecache(freecache.NewCache(1000), &Options{
})

cacheManager := cache.New(freecacheStore)
err := cacheManager.Set("by-key", []byte("my-value"), opts)
err := cacheManager.Set(ctx, "by-key", []byte("my-value"), opts)
if err != nil {
panic(err)
}

value := cacheManager.Get("my-key")
value := cacheManager.Get(ctx, "my-key")
```

#### Pegasus
Expand All @@ -158,14 +158,14 @@ if err != nil {
}

cacheManager := cache.New(pegasusStore)
err = cacheManager.Set("my-key", "my-value", &store.Options{
err = cacheManager.Set(ctx, "my-key", "my-value", &store.Options{
Expiration: 10 * time.Second,
})
if err != nil {
panic(err)
}

value, _ := cacheManager.Get("my-key")
value, _ := cacheManager.Get(ctx, "my-key")
```

### A chained cache
Expand Down Expand Up @@ -264,19 +264,19 @@ marshal := marshaler.New(cacheManager)
key := BookQuery{Slug: "my-test-amazing-book"}
value := Book{ID: 1, Name: "My test amazing book", Slug: "my-test-amazing-book"}

err = marshal.Set(key, value)
err = marshal.Set(ctx, key, value)
if err != nil {
panic(err)
}

returnedValue, err := marshal.Get(key, new(Book))
returnedValue, err := marshal.Get(ctx, key, new(Book))
if err != nil {
panic(err)
}

// Then, do what you want with the value

marshal.Delete("my-key")
marshal.Delete(ctx, "my-key")
```

The only thing you have to do is to specify the struct in which you want your value to be un-marshalled as a second argument when calling the `.Get()` method.
Expand Down Expand Up @@ -308,18 +308,18 @@ key := BookQuery{Slug: "my-test-amazing-book"}
value := Book{ID: 1, Name: "My test amazing book", Slug: "my-test-amazing-book"}

// Set an item in the cache and attach it a "book" tag
err = marshal.Set(key, value, store.Options{Tags: []string{"book"}})
err = marshal.Set(ctx, key, value, store.Options{Tags: []string{"book"}})
if err != nil {
panic(err)
}

// Remove all items that have the "book" tag
err := marshal.Invalidate(store.InvalidateOptions{Tags: []string{"book"}})
err := marshal.Invalidate(ctx, store.InvalidateOptions{Tags: []string{"book"}})
if err != nil {
panic(err)
}

returnedValue, err := marshal.Get(key, new(Book))
returnedValue, err := marshal.Get(ctx, key, new(Book))
if err != nil {
// Should be triggered because item has been deleted so it cannot be found.
panic(err)
Expand All @@ -334,11 +334,11 @@ Cache respect the following interface so you can write your own (proprietary?) c

```go
type CacheInterface interface {
Get(key interface{}) (interface{}, error)
Set(key, object interface{}, options *store.Options) error
Delete(key interface{}) error
Invalidate(options store.InvalidateOptions) error
Clear() error
Get(ctx context.Context, key interface{}) (interface{}, error)
Set(ctx context.Context, key, object interface{}, options *store.Options) error
Delete(ctx context.Context, key interface{}) error
Invalidate(ctx context.Context, options store.InvalidateOptions) error
Clear(ctx context.Context) error
GetType() string
}
```
Expand All @@ -348,6 +348,7 @@ Or, in case you use a setter cache, also implement the `GetCodec()` method:
```go
type SetterCacheInterface interface {
CacheInterface
GetWithTTL(ctx context.Context, key interface{}) (interface{}, time.Duration, error)

GetCodec() codec.CodecInterface
}
Expand All @@ -361,11 +362,12 @@ You also have the ability to write your own custom store by implementing the fol

```go
type StoreInterface interface {
Get(key interface{}) (interface{}, error)
Set(key interface{}, value interface{}, options *Options) error
Delete(key interface{}) error
Invalidate(options InvalidateOptions) error
Clear() error
Get(ctx context.Context, key interface{}) (interface{}, error)
GetWithTTL(ctx context.Context, key interface{}) (interface{}, time.Duration, error)
Set(ctx context.Context, key interface{}, value interface{}, options *Options) error
Delete(ctx context.Context, key interface{}) error
Invalidate(ctx context.Context, options InvalidateOptions) error
Clear(ctx context.Context) error
GetType() string
}
```
Expand Down
25 changes: 13 additions & 12 deletions cache/cache.go
@@ -1,6 +1,7 @@
package cache

import (
"context"
"crypto"
"fmt"
"reflect"
Expand Down Expand Up @@ -28,37 +29,37 @@ func New(store store.StoreInterface) *Cache {
}

// Get returns the object stored in cache if it exists
func (c *Cache) Get(key interface{}) (interface{}, error) {
func (c *Cache) Get(ctx context.Context, key interface{}) (interface{}, error) {
cacheKey := c.getCacheKey(key)
return c.codec.Get(cacheKey)
return c.codec.Get(ctx, cacheKey)
}

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

// Set populates the cache item using the given key
func (c *Cache) Set(key, object interface{}, options *store.Options) error {
func (c *Cache) Set(ctx context.Context, key, object interface{}, options *store.Options) error {
cacheKey := c.getCacheKey(key)
return c.codec.Set(cacheKey, object, options)
return c.codec.Set(ctx, cacheKey, object, options)
}

// Delete removes the cache item using the given key
func (c *Cache) Delete(key interface{}) error {
func (c *Cache) Delete(ctx context.Context, key interface{}) error {
cacheKey := c.getCacheKey(key)
return c.codec.Delete(cacheKey)
return c.codec.Delete(ctx, cacheKey)
}

// Invalidate invalidates cache item from given options
func (c *Cache) Invalidate(options store.InvalidateOptions) error {
return c.codec.Invalidate(options)
func (c *Cache) Invalidate(ctx context.Context, options store.InvalidateOptions) error {
return c.codec.Invalidate(ctx, options)
}

// Clear resets all cache data
func (c *Cache) Clear() error {
return c.codec.Clear()
func (c *Cache) Clear(ctx context.Context) error {
return c.codec.Clear(ctx)
}

// GetCodec returns the current codec
Expand Down

0 comments on commit 4914d74

Please sign in to comment.