From 3f053711ba95b272dc6a028608e1c6c965757c16 Mon Sep 17 00:00:00 2001 From: mikestefanello Date: Fri, 14 Jan 2022 13:07:19 -0500 Subject: [PATCH] Remove need for slice when setting cache tags. Require cache key for get/set ops. --- README.md | 10 +++++----- controller/controller.go | 2 +- controller/controller_test.go | 2 +- services/cache.go | 13 +++++++++++-- services/cache_test.go | 4 ++-- 5 files changed, 20 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 0034aa6..1fcbf03 100644 --- a/README.md +++ b/README.md @@ -887,7 +887,7 @@ err := c.Cache. ```go err := c.Cache. Set(). - Group("my-group") + Group("my-group"). Key("my-key"). Data(myData). Save(ctx) @@ -899,7 +899,7 @@ err := c.Cache. err := c.Cache. Set(). Key("my-key"). - Tags([]string{"tag1", "tag2"}) + Tags("tag1", "tag2"). Data(myData). Save(ctx) ``` @@ -910,7 +910,7 @@ err := c.Cache. err := c.Cache. Set(). Key("my-key"). - Expiration(time.Hour * 2) + Expiration(time.Hour * 2). Data(myData). Save(ctx) ``` @@ -945,7 +945,7 @@ This will flush all cache entries that were tagged with the given tags. ```go err := c.Cache. Flush(). - Tags([]string{"tag1"}). + Tags("tag1", "tag2"). Exec(ctx) ``` @@ -965,7 +965,7 @@ While it's ideal to use cache control headers on your static files so browsers c For example, to render a file located in `static/picture.png`, you would use: ```go - + ``` Which would result in: diff --git a/controller/controller.go b/controller/controller.go index 0dba313..577f887 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -138,7 +138,7 @@ func (c *Controller) cachePage(ctx echo.Context, page Page, html *bytes.Buffer) Set(). Group(middleware.CachedPageGroup). Key(key). - Tags(page.Cache.Tags). + Tags(page.Cache.Tags...). Expiration(page.Cache.Expiration). Data(cp). Save(ctx.Request().Context()) diff --git a/controller/controller_test.go b/controller/controller_test.go index 40567a5..9248fe8 100644 --- a/controller/controller_test.go +++ b/controller/controller_test.go @@ -166,7 +166,7 @@ func TestController_RenderPage(t *testing.T) { // Clear the tag err = c.Cache. Flush(). - Tags([]string{p.Cache.Tags[0]}). + Tags(p.Cache.Tags[0]). Exec(context.Background()) require.NoError(t, err) diff --git a/services/cache.go b/services/cache.go index 90dbc63..4e3f514 100644 --- a/services/cache.go +++ b/services/cache.go @@ -2,6 +2,7 @@ package services import ( "context" + "errors" "fmt" "time" @@ -124,13 +125,17 @@ func (c *cacheSet) Expiration(expiration time.Duration) *cacheSet { } // Tags sets the cache tags -func (c *cacheSet) Tags(tags []string) *cacheSet { +func (c *cacheSet) Tags(tags ...string) *cacheSet { c.tags = tags return c } // Save saves the data in the cache func (c *cacheSet) Save(ctx context.Context) error { + if c.key == "" { + return errors.New("no cache key specified") + } + opts := &store.Options{ Expiration: c.expiration, Tags: c.tags, @@ -161,6 +166,10 @@ func (c *cacheGet) Type(expectedType interface{}) *cacheGet { // Fetch fetches the data from the cache func (c *cacheGet) Fetch(ctx context.Context) (interface{}, error) { + if c.key == "" { + return nil, errors.New("no cache key specified") + } + return marshaler.New(c.client.cache).Get( ctx, c.client.cacheKey(c.group, c.key), @@ -181,7 +190,7 @@ func (c *cacheFlush) Group(group string) *cacheFlush { } // Tags sets the cache tags -func (c *cacheFlush) Tags(tags []string) *cacheFlush { +func (c *cacheFlush) Tags(tags ...string) *cacheFlush { c.tags = tags return c } diff --git a/services/cache_test.go b/services/cache_test.go index 2debf6f..41003fd 100644 --- a/services/cache_test.go +++ b/services/cache_test.go @@ -73,14 +73,14 @@ func TestCacheClient(t *testing.T) { Group(group). Key(key). Data(data). - Tags([]string{"tag1"}). + Tags("tag1"). Save(context.Background()) require.NoError(t, err) // Flush the tag err = c.Cache. Flush(). - Tags([]string{"tag1"}). + Tags("tag1"). Exec(context.Background()) require.NoError(t, err)