Skip to content

Commit

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

* chore: Use generics in lru cache

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Carsten Dietrich <[email protected]>
  • Loading branch information
renovate[bot] and carstendietrich authored Apr 13, 2023
1 parent a1ec687 commit 207f2f8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 19 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
25 changes: 9 additions & 16 deletions inMemoryBackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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)
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
}
}
Expand Down

0 comments on commit 207f2f8

Please sign in to comment.