Skip to content

Commit

Permalink
chore(deps): update module github.com/hashicorp/golang-lru to v2 (#314)
Browse files Browse the repository at this point in the history
* chore(deps): update module github.com/hashicorp/golang-lru to v2

* chore: Mark cache module as deprecated, update to generic version of LRU

* chore: replace singleflight with implementation from golang/x/sync

* chore: fix lint

* chore: go mod tidy

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Carsten Dietrich <carsten.dietrich@omnevo.net>
  • Loading branch information
renovate[bot] and carstendietrich authored Aug 24, 2023
1 parent 9e8909c commit 29db3cf
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 63 deletions.
3 changes: 3 additions & 0 deletions core/cache/Readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
**DEPRECATED:** We refactored the caching of HTTP requests, added more backends and moved it into a separate module: [flamingo.me/httpcache](https://flamingo.me/httpcache)

# Cache module

The Cache module provides an easy interface to cache things in flamingo.

The basic concept is, that there is a so called "cache frontend" - that offers an interface to cache certain types, and a "cache backend" that takes care about storing(persiting) the cache entry.
Expand Down
5 changes: 2 additions & 3 deletions core/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ type (

// Backend describes a cache backend, responsible for storing, flushing, setting and getting entries
Backend interface {
Get(key string) (entry *Entry, found bool) // Get a cache entry
Set(key string, entry *Entry) error // Set a cache entry
//Peek(key string) (entry *CacheEntry, found bool) // Peek for a cache entry, this should not trigger key-updates or weight/priorities to be changed
Get(key string) (entry *Entry, found bool)
Set(key string, entry *Entry) error
Purge(key string) error
PurgeTags(tags []string) error
Flush() error
Expand Down
13 changes: 5 additions & 8 deletions core/cache/httpFrontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"net/http"
"time"

"github.com/golang/groupcache/singleflight"
"go.opencensus.io/trace"
"golang.org/x/sync/singleflight"

"flamingo.me/flamingo/v3/framework/flamingo"
)
Expand All @@ -20,6 +20,7 @@ type (
HTTPLoader func(context.Context) (*http.Response, *Meta, error)

// HTTPFrontend stores and caches http responses
// Deprecated: Please use the dedicated httpcache flamingo module, see here: flamingo.me/httpcache
HTTPFrontend struct {
singleflight.Group
backend Backend
Expand Down Expand Up @@ -117,7 +118,7 @@ func (hf *HTTPFrontend) load(ctx context.Context, key string, loader HTTPLoader,
span.Annotate(nil, key)
defer span.End()

data, err := hf.Do(key, func() (res interface{}, resultErr error) {
data, err, _ := hf.Do(key, func() (res interface{}, resultErr error) {
ctx, fetchRoutineSpan := trace.StartSpan(newContextWithSpan, "flamingo/cache/httpFrontend/fetchRoutine")
fetchRoutineSpan.Annotate(nil, key)
defer fetchRoutineSpan.End()
Expand All @@ -127,7 +128,8 @@ func (hf *HTTPFrontend) load(ctx context.Context, key string, loader HTTPLoader,
if err2, ok := err.(error); ok {
resultErr = fmt.Errorf("httpfrontend load: %w", err2)
} else {
resultErr = fmt.Errorf("httpfrontend load: %v", err2)
//nolint:goerr113 // not worth introducing a dedicated error for this edge case
resultErr = fmt.Errorf("httpfrontend load: %v", err)
}
}
}()
Expand Down Expand Up @@ -198,11 +200,6 @@ func (hf *HTTPFrontend) load(ctx context.Context, key string, loader HTTPLoader,

span.AddAttributes(trace.StringAttribute("parenttrace", response.span.TraceID.String()))
span.AddAttributes(trace.StringAttribute("parentspan", response.span.SpanID.String()))
//span.AddLink(trace.Link{
// SpanID: data.(loaderResponse).span.SpanID,
// TraceID: data.(loaderResponse).span.TraceID,
// Type: trace.LinkTypeChild,
//})

return cached, err
}
21 changes: 14 additions & 7 deletions core/cache/inMemoryBackend.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package cache

import (
"errors"
"time"

lru "github.com/hashicorp/golang-lru"
lru "github.com/hashicorp/golang-lru/v2"
)

const lurkerPeriod = 1 * time.Minute
const cacheSize = 100

type (
inMemoryCache struct {
pool *lru.TwoQueueCache
pool *lru.TwoQueueCache[string, inMemoryCacheEntry]
}

inMemoryCacheEntry struct {
Expand All @@ -21,12 +23,13 @@ type (

// NewInMemoryCache creates a new lru TwoQueue backed cache backend
func NewInMemoryCache() Backend {
cache, _ := lru.New2Q(100)
cache, _ := lru.New2Q[string, inMemoryCacheEntry](cacheSize)

m := &inMemoryCache{
pool: cache,
}
go m.lurker()

return m
}

Expand All @@ -36,7 +39,10 @@ func (m *inMemoryCache) Get(key string) (*Entry, bool) {
if !ok {
return nil, ok
}
return entry.(inMemoryCacheEntry).data.(*Entry), ok

e, ok := entry.data.(*Entry)

return e, ok
}

// Set a cache entry with a key
Expand All @@ -57,8 +63,9 @@ func (m *inMemoryCache) Purge(key string) error {
}

// PurgeTags purges all entries with matching tags from the cache
func (m *inMemoryCache) PurgeTags(tags []string) error {
panic("implement me")
func (m *inMemoryCache) PurgeTags(_ []string) error {
//nolint:goerr113 // not worth introducing a dedicated error
return errors.New("not implemented")
}

// Flush purges all entries in the cache
Expand All @@ -72,7 +79,7 @@ func (m *inMemoryCache) lurker() {
for range time.Tick(lurkerPeriod) {
for _, key := range m.pool.Keys() {
item, ok := m.pool.Peek(key)
if ok && item.(inMemoryCacheEntry).valid.Before(time.Now()) {
if ok && item.valid.Before(time.Now()) {
m.pool.Remove(key)
break
}
Expand Down
99 changes: 99 additions & 0 deletions core/cache/inMemoryBackend_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package cache_test

import (
"testing"
"time"

"flamingo.me/flamingo/v3/core/cache"
"github.com/stretchr/testify/assert"
)

func Test_inMemoryCache_Flush(t *testing.T) {
t.Parallel()

inMemoryCache := cache.NewInMemoryCache()

err := inMemoryCache.Set("foo", &cache.Entry{
Meta: cache.Meta{
Tags: []string{"bar"},
Lifetime: 5 * time.Second,
Gracetime: 10 * time.Second,
},
Data: "test",
})

assert.NoError(t, err)

err = inMemoryCache.Set("baz", &cache.Entry{
Meta: cache.Meta{
Tags: []string{"bar"},
Lifetime: 5 * time.Second,
Gracetime: 10 * time.Second,
},
Data: "test",
})

assert.NoError(t, err)

_, found := inMemoryCache.Get("foo")
assert.True(t, found)

assert.NoError(t, inMemoryCache.Flush())

_, found = inMemoryCache.Get("foo")
assert.False(t, found)

_, found = inMemoryCache.Get("baz")
assert.False(t, found)
}

func Test_inMemoryCache_Purge(t *testing.T) {
t.Parallel()

inMemoryCache := cache.NewInMemoryCache()

err := inMemoryCache.Set("foo", &cache.Entry{
Meta: cache.Meta{
Tags: []string{"bar"},
Lifetime: 5 * time.Second,
Gracetime: 10 * time.Second,
},
Data: "test",
})

assert.NoError(t, err)

_, found := inMemoryCache.Get("foo")
assert.True(t, found)

assert.NoError(t, inMemoryCache.Purge("foo"))

_, found = inMemoryCache.Get("foo")
assert.False(t, found)
}

func Test_inMemoryCache_SetGet(t *testing.T) {
t.Parallel()

inMemoryCache := cache.NewInMemoryCache()

err := inMemoryCache.Set("foo", &cache.Entry{
Meta: cache.Meta{
Tags: []string{"bar"},
Lifetime: 5 * time.Second,
Gracetime: 10 * time.Second,
},
Data: "test",
})

assert.NoError(t, err)

entry, found := inMemoryCache.Get("foo")
assert.True(t, found)
assert.Equal(t, "test", entry.Data)
assert.Equal(t, cache.Meta{
Tags: []string{"bar"},
Lifetime: 5 * time.Second,
Gracetime: 10 * time.Second,
}, entry.Meta)
}
4 changes: 2 additions & 2 deletions core/cache/stringFrontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package cache
import (
"time"

"github.com/golang/groupcache/singleflight"
"go.opencensus.io/trace"
"golang.org/x/sync/singleflight"
)

type (
Expand Down Expand Up @@ -40,7 +40,7 @@ func (sf *StringFrontend) Get(key string, loader StringLoader) (string, error) {
}

func (sf *StringFrontend) load(key string, loader StringLoader) (string, error) {
data, err := sf.Do(key, func() (interface{}, error) {
data, err, _ := sf.Do(key, func() (interface{}, error) {
data, meta, err := loader()
if meta == nil {
meta = &Meta{
Expand Down
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ require (
github.com/ghodss/yaml v1.0.0
github.com/gofrs/uuid v4.3.1+incompatible
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da
github.com/google/go-cmp v0.5.9
github.com/gorilla/securecookie v1.1.1
github.com/gorilla/sessions v1.2.1
github.com/hashicorp/golang-lru v0.6.0
github.com/hashicorp/golang-lru/v2 v2.0.5
github.com/leekchan/accounting v0.3.1
github.com/nicksnyder/go-i18n v0.0.0-20180814031359-04f547cc50da
github.com/openzipkin/zipkin-go v0.4.2
Expand All @@ -31,6 +30,7 @@ require (
go.uber.org/automaxprocs v1.5.3
go.uber.org/zap v1.25.0
golang.org/x/oauth2 v0.11.0
golang.org/x/sync v0.3.0
)

require (
Expand All @@ -43,6 +43,7 @@ require (
github.com/go-jose/go-jose/v3 v3.0.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand All @@ -60,11 +61,9 @@ require (
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/uber/jaeger-client-go v2.25.0+incompatible // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f // indirect
Expand Down
Loading

0 comments on commit 29db3cf

Please sign in to comment.