diff --git a/go.mod b/go.mod index 066ffda..b580254 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( flamingo.me/flamingo/v3 v3.5.1 github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da github.com/gomodule/redigo v1.8.9 - github.com/hashicorp/golang-lru v0.6.0 + github.com/hashicorp/golang-lru/v2 v2.0.2 github.com/pkg/errors v0.9.1 github.com/stretchr/testify v1.8.2 github.com/testcontainers/testcontainers-go v0.19.0 diff --git a/go.sum b/go.sum index 667c77b..edf96d9 100644 --- a/go.sum +++ b/go.sum @@ -297,8 +297,8 @@ github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/b github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= -github.com/hashicorp/golang-lru v0.6.0 h1:uL2shRDx7RTrOrTCUZEGP/wJUFiUI8QT6E7z5o8jga4= -github.com/hashicorp/golang-lru v0.6.0/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= +github.com/hashicorp/golang-lru/v2 v2.0.2 h1:Dwmkdr5Nc/oBiXgJS3CDHNhJtIHkuZ3DZF5twqnfBdU= +github.com/hashicorp/golang-lru/v2 v2.0.2/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64= github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ= diff --git a/inMemoryBackend.go b/inMemoryBackend.go index a3ed006..c1b890f 100644 --- a/inMemoryBackend.go +++ b/inMemoryBackend.go @@ -4,7 +4,7 @@ import ( "fmt" "time" - lru "github.com/hashicorp/golang-lru" + lru "github.com/hashicorp/golang-lru/v2" ) const lurkerPeriod = 1 * time.Minute @@ -13,7 +13,7 @@ type ( // MemoryBackend implements the cache backend interface with an "in memory" solution MemoryBackend struct { cacheMetrics Metrics - pool *lru.TwoQueueCache + pool *lru.TwoQueueCache[string, inMemoryCacheEntry] } // MemoryBackendConfig config @@ -49,7 +49,7 @@ func (f *InMemoryBackendFactory) SetFrontendName(frontendName string) *InMemoryB // Build the instance func (f *InMemoryBackendFactory) Build() (Backend, error) { - cache, _ := lru.New2Q(f.config.Size) + cache, _ := lru.New2Q[string, inMemoryCacheEntry](f.config.Size) memoryBackend := &MemoryBackend{ pool: cache, @@ -62,7 +62,7 @@ func (f *InMemoryBackendFactory) Build() (Backend, error) { // SetSize creates a new underlying cache of the given size func (m *MemoryBackend) SetSize(size int) error { - cache, err := lru.New2Q(size) + cache, err := lru.New2Q[string, inMemoryCacheEntry](size) if err != nil { return fmt.Errorf("lru cant create new TwoQueueCache: %w", err) } @@ -82,12 +82,7 @@ func (m *MemoryBackend) Get(key string) (Entry, bool) { m.cacheMetrics.countHit() - inMemoryEntry, ok := entry.(inMemoryCacheEntry) - if !ok { - return Entry{}, false - } - - data, ok := inMemoryEntry.data.(Entry) + data, ok := entry.data.(Entry) if !ok { return Entry{}, false } @@ -124,12 +119,10 @@ func (m *MemoryBackend) lurker() { m.cacheMetrics.recordEntries(int64(m.pool.Len())) for _, key := range m.pool.Keys() { - item, found := m.pool.Peek(key) - if found { - if inMemoryEntry, ok := item.(inMemoryCacheEntry); ok && inMemoryEntry.valid.Before(time.Now()) { - m.pool.Remove(key) - break - } + entry, found := m.pool.Peek(key) + if found && entry.valid.Before(time.Now()) { + m.pool.Remove(key) + break } } }