Remove need for slice when setting cache tags. Require cache key for get/set ops.

This commit is contained in:
mikestefanello 2022-01-14 13:07:19 -05:00
parent bfbb9669aa
commit 3f053711ba
5 changed files with 20 additions and 11 deletions

View file

@ -887,7 +887,7 @@ err := c.Cache.
```go ```go
err := c.Cache. err := c.Cache.
Set(). Set().
Group("my-group") Group("my-group").
Key("my-key"). Key("my-key").
Data(myData). Data(myData).
Save(ctx) Save(ctx)
@ -899,7 +899,7 @@ err := c.Cache.
err := c.Cache. err := c.Cache.
Set(). Set().
Key("my-key"). Key("my-key").
Tags([]string{"tag1", "tag2"}) Tags("tag1", "tag2").
Data(myData). Data(myData).
Save(ctx) Save(ctx)
``` ```
@ -910,7 +910,7 @@ err := c.Cache.
err := c.Cache. err := c.Cache.
Set(). Set().
Key("my-key"). Key("my-key").
Expiration(time.Hour * 2) Expiration(time.Hour * 2).
Data(myData). Data(myData).
Save(ctx) Save(ctx)
``` ```
@ -945,7 +945,7 @@ This will flush all cache entries that were tagged with the given tags.
```go ```go
err := c.Cache. err := c.Cache.
Flush(). Flush().
Tags([]string{"tag1"}). Tags("tag1", "tag2").
Exec(ctx) 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: For example, to render a file located in `static/picture.png`, you would use:
```go ```go
<img src="{{File "picture.png"}"/> <img src="{{File "picture.png"}}"/>
``` ```
Which would result in: Which would result in:

View file

@ -138,7 +138,7 @@ func (c *Controller) cachePage(ctx echo.Context, page Page, html *bytes.Buffer)
Set(). Set().
Group(middleware.CachedPageGroup). Group(middleware.CachedPageGroup).
Key(key). Key(key).
Tags(page.Cache.Tags). Tags(page.Cache.Tags...).
Expiration(page.Cache.Expiration). Expiration(page.Cache.Expiration).
Data(cp). Data(cp).
Save(ctx.Request().Context()) Save(ctx.Request().Context())

View file

@ -166,7 +166,7 @@ func TestController_RenderPage(t *testing.T) {
// Clear the tag // Clear the tag
err = c.Cache. err = c.Cache.
Flush(). Flush().
Tags([]string{p.Cache.Tags[0]}). Tags(p.Cache.Tags[0]).
Exec(context.Background()) Exec(context.Background())
require.NoError(t, err) require.NoError(t, err)

View file

@ -2,6 +2,7 @@ package services
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"time" "time"
@ -124,13 +125,17 @@ func (c *cacheSet) Expiration(expiration time.Duration) *cacheSet {
} }
// Tags sets the cache tags // Tags sets the cache tags
func (c *cacheSet) Tags(tags []string) *cacheSet { func (c *cacheSet) Tags(tags ...string) *cacheSet {
c.tags = tags c.tags = tags
return c return c
} }
// Save saves the data in the cache // Save saves the data in the cache
func (c *cacheSet) Save(ctx context.Context) error { func (c *cacheSet) Save(ctx context.Context) error {
if c.key == "" {
return errors.New("no cache key specified")
}
opts := &store.Options{ opts := &store.Options{
Expiration: c.expiration, Expiration: c.expiration,
Tags: c.tags, Tags: c.tags,
@ -161,6 +166,10 @@ func (c *cacheGet) Type(expectedType interface{}) *cacheGet {
// Fetch fetches the data from the cache // Fetch fetches the data from the cache
func (c *cacheGet) Fetch(ctx context.Context) (interface{}, error) { 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( return marshaler.New(c.client.cache).Get(
ctx, ctx,
c.client.cacheKey(c.group, c.key), c.client.cacheKey(c.group, c.key),
@ -181,7 +190,7 @@ func (c *cacheFlush) Group(group string) *cacheFlush {
} }
// Tags sets the cache tags // Tags sets the cache tags
func (c *cacheFlush) Tags(tags []string) *cacheFlush { func (c *cacheFlush) Tags(tags ...string) *cacheFlush {
c.tags = tags c.tags = tags
return c return c
} }

View file

@ -73,14 +73,14 @@ func TestCacheClient(t *testing.T) {
Group(group). Group(group).
Key(key). Key(key).
Data(data). Data(data).
Tags([]string{"tag1"}). Tags("tag1").
Save(context.Background()) Save(context.Background())
require.NoError(t, err) require.NoError(t, err)
// Flush the tag // Flush the tag
err = c.Cache. err = c.Cache.
Flush(). Flush().
Tags([]string{"tag1"}). Tags("tag1").
Exec(context.Background()) Exec(context.Background())
require.NoError(t, err) require.NoError(t, err)