Update go-redis to v9 and gocache to v4

This commit is contained in:
Stefan Aurori 2024-03-10 12:46:53 -04:00
parent 740aebd1a9
commit 472dc0c358
7 changed files with 44 additions and 220 deletions

View file

@ -1,6 +1,7 @@
package middleware
import (
"errors"
"fmt"
"net/http"
"time"
@ -8,8 +9,9 @@ import (
"github.com/mikestefanello/pagoda/pkg/context"
"github.com/mikestefanello/pagoda/pkg/services"
"github.com/go-redis/redis/v8"
lib_store "github.com/eko/gocache/lib/v4/store"
"github.com/labstack/echo/v4"
"github.com/redis/go-redis/v9"
)
// CachedPageGroup stores the cache group for cached pages
@ -56,7 +58,7 @@ func ServeCachedPage(ch *services.CacheClient) echo.MiddlewareFunc {
if err != nil {
switch {
case err == redis.Nil:
case errors.Is(err, &lib_store.NotFound{}) || err == redis.Nil:
c.Logger().Info("no cached page found")
case context.IsCanceledError(err):
return nil

View file

@ -6,11 +6,12 @@ import (
"fmt"
"time"
"github.com/eko/gocache/v2/cache"
"github.com/eko/gocache/v2/marshaler"
"github.com/eko/gocache/v2/store"
"github.com/go-redis/redis/v8"
"github.com/eko/gocache/lib/v4/cache"
"github.com/eko/gocache/lib/v4/marshaler"
lib_store "github.com/eko/gocache/lib/v4/store"
redis_store "github.com/eko/gocache/store/redis/v4"
"github.com/mikestefanello/pagoda/config"
"github.com/redis/go-redis/v9"
)
type (
@ -20,7 +21,7 @@ type (
Client *redis.Client
// cache stores the cache interface
cache *cache.Cache
cache *cache.Cache[any]
}
// cacheSet handles chaining a set operation
@ -76,8 +77,8 @@ func NewCacheClient(cfg *config.Config) (*CacheClient, error) {
}
}
cacheStore := store.NewRedis(c.Client, nil)
c.cache = cache.New(cacheStore)
cacheStore := redis_store.NewRedis(c.Client)
c.cache = cache.New[any](cacheStore)
return c, nil
}
@ -151,14 +152,14 @@ func (c *cacheSet) Save(ctx context.Context) error {
return errors.New("no cache key specified")
}
opts := &store.Options{
Expiration: c.expiration,
Tags: c.tags,
opts := []lib_store.Option{
lib_store.WithExpiration(c.expiration),
lib_store.WithTags(c.tags),
}
return marshaler.
New(c.client.cache).
Set(ctx, c.client.cacheKey(c.group, c.key), c.data, opts)
Set(ctx, c.client.cacheKey(c.group, c.key), c.data, opts...)
}
// Key sets the cache key
@ -213,9 +214,7 @@ func (c *cacheFlush) Tags(tags ...string) *cacheFlush {
// Execute flushes the data from the cache
func (c *cacheFlush) Execute(ctx context.Context) error {
if len(c.tags) > 0 {
if err := c.client.cache.Invalidate(ctx, store.InvalidateOptions{
Tags: c.tags,
}); err != nil {
if err := c.client.cache.Invalidate(ctx, lib_store.WithInvalidateTags(c.tags)); err != nil {
return err
}
}

View file

@ -2,10 +2,11 @@ package services
import (
"context"
"errors"
"testing"
"time"
"github.com/go-redis/redis/v8"
lib_store "github.com/eko/gocache/lib/v4/store"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@ -63,7 +64,7 @@ func TestCacheClient(t *testing.T) {
Key(key).
Type(new(cacheTest)).
Fetch(context.Background())
assert.Equal(t, redis.Nil, err)
assert.True(t, errors.Is(err, &lib_store.NotFound{}))
}
assertFlushed()