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 e0a65ca007
commit b269e7d264
5 changed files with 20 additions and 11 deletions

View File

@ -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
<img src="{{File "picture.png"}"/>
<img src="{{File "picture.png"}}"/>
```
Which would result in:

View File

@ -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())

View File

@ -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)

View File

@ -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
}

View File

@ -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)