diff options
author | GitLab Renovate Bot <gitlab-bot@gitlab.com> | 2023-09-29 20:20:50 +0300 |
---|---|---|
committer | GitLab Renovate Bot <gitlab-bot@gitlab.com> | 2023-09-29 20:20:50 +0300 |
commit | 7e8c103110fdbfa8ffa907fbdc3b36872efd93d8 (patch) | |
tree | dca0a956f800bf1103d540e502d34d4ca4d341d5 | |
parent | 6d25650a5475d632e6602aef15ade2fd1c8bdeb1 (diff) |
go: Update module github.com/hashicorp/golang-lru/v2 to v2.0.7
-rw-r--r-- | NOTICE | 121 | ||||
-rw-r--r-- | go.mod | 2 | ||||
-rw-r--r-- | go.sum | 4 |
3 files changed, 119 insertions, 8 deletions
@@ -11669,8 +11669,10 @@ const ( // head. The ARCCache is similar, but does not require setting any // parameters. type TwoQueueCache[K comparable, V any] struct { - size int - recentSize int + size int + recentSize int + recentRatio float64 + ghostRatio float64 recent simplelru.LRUCache[K, V] frequent simplelru.LRUCache[K, V] @@ -11719,6 +11721,8 @@ func New2QParams[K comparable, V any](size int, recentRatio, ghostRatio float64) c := &TwoQueueCache[K, V]{ size: size, recentSize: recentSize, + recentRatio: recentRatio, + ghostRatio: ghostRatio, recent: recent, frequent: frequent, recentEvict: recentEvict, @@ -11810,6 +11814,34 @@ func (c *TwoQueueCache[K, V]) Len() int { return c.recent.Len() + c.frequent.Len() } +// Resize changes the cache size. +func (c *TwoQueueCache[K, V]) Resize(size int) (evicted int) { + c.lock.Lock() + defer c.lock.Unlock() + + // Recalculate the sub-sizes + recentSize := int(float64(size) * c.recentRatio) + evictSize := int(float64(size) * c.ghostRatio) + c.size = size + c.recentSize = recentSize + + // ensureSpace + diff := c.recent.Len() + c.frequent.Len() - size + if diff < 0 { + diff = 0 + } + for i := 0; i < diff; i++ { + c.ensureSpace(true) + } + + // Reallocate the LRUs + c.recent.Resize(size) + c.frequent.Resize(size) + c.recentEvict.Resize(evictSize) + + return diff +} + // Keys returns a slice of the keys in the cache. // The frequently used keys are first in the returned slice. func (c *TwoQueueCache[K, V]) Keys() []K { @@ -12095,6 +12127,75 @@ func Test2Q_Add_RecentEvict(t *testing.T) { } } +func Test2Q_Resize(t *testing.T) { + l, err := New2Q[int, int](100) + if err != nil { + t.Fatalf("err: %v", err) + } + + // Touch all the entries, should be in t1 + for i := 0; i < 100; i++ { + l.Add(i, i) + } + + evicted := l.Resize(50) + if evicted != 50 { + t.Fatalf("bad: %d", evicted) + } + + if n := l.recent.Len(); n != 50 { + t.Fatalf("bad: %d", n) + } + if n := l.frequent.Len(); n != 0 { + t.Fatalf("bad: %d", n) + } + + l, err = New2Q[int, int](100) + if err != nil { + t.Fatalf("err: %v", err) + } + for i := 0; i < 100; i++ { + l.Add(i, i) + } + + for i := 0; i < 50; i++ { + l.Add(i, i) + } + + evicted = l.Resize(50) + if evicted != 50 { + t.Fatalf("bad: %d", evicted) + } + + if n := l.recent.Len(); n != 12 { + t.Fatalf("bad: %d", n) + } + if n := l.frequent.Len(); n != 38 { + t.Fatalf("bad: %d", n) + } + + l, err = New2Q[int, int](100) + if err != nil { + t.Fatalf("err: %v", err) + } + for i := 0; i < 100; i++ { + l.Add(i, i) + l.Add(i, i) + } + + evicted = l.Resize(50) + if evicted != 50 { + t.Fatalf("bad: %d", evicted) + } + + if n := l.recent.Len(); n != 0 { + t.Fatalf("bad: %d", n) + } + if n := l.frequent.Len(); n != 50 { + t.Fatalf("bad: %d", n) + } +} + func Test2Q(t *testing.T) { l, err := New2Q[int, int](128) if err != nil { @@ -12815,7 +12916,7 @@ func (c *LRU[K, V]) Get(key K) (value V, ok bool) { if ent, ok = c.items[key]; ok { // Expired item check if time.Now().After(ent.ExpiresAt) { - return + return value, false } c.evictList.MoveToFront(ent) return ent.Value, true @@ -12841,7 +12942,7 @@ func (c *LRU[K, V]) Peek(key K) (value V, ok bool) { if ent, ok = c.items[key]; ok { // Expired item check if time.Now().After(ent.ExpiresAt) { - return + return value, false } return ent.Value, true } @@ -13393,7 +13494,17 @@ func TestLoadingExpired(t *testing.T) { t.Fatalf("should be true") } - time.Sleep(time.Millisecond * 100) // wait for entry to expire + for { + result, ok := lc.Get("key1") + if ok && result == "" { + t.Fatalf("ok should return a result") + } + if !ok { + break + } + } + + time.Sleep(time.Millisecond * 100) // wait for expiration reaper if lc.Len() != 0 { t.Fatalf("length differs from expected") } @@ -20,7 +20,7 @@ require ( github.com/google/uuid v1.3.1 github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 - github.com/hashicorp/golang-lru/v2 v2.0.6 + github.com/hashicorp/golang-lru/v2 v2.0.7 github.com/hashicorp/yamux v0.1.2-0.20220728231024-8f49b6f63f18 github.com/jackc/pgx/v5 v5.4.3 github.com/kelseyhightower/envconfig v1.4.0 @@ -391,8 +391,8 @@ github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/C github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= 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/v2 v2.0.6 h1:3xi/Cafd1NaoEnS/yDssIiuVeDVywU0QdFGl3aQaQHM= -github.com/hashicorp/golang-lru/v2 v2.0.6/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= +github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= +github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= github.com/hashicorp/yamux v0.1.2-0.20220728231024-8f49b6f63f18 h1:IVujPV6DRIu1fYF4zUHrfhkngJzmYjelXa+iSUiFZSI= github.com/hashicorp/yamux v0.1.2-0.20220728231024-8f49b6f63f18/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ= github.com/hhatto/gorst v0.0.0-20181029133204-ca9f730cac5b h1:Jdu2tbAxkRouSILp2EbposIb8h4gO+2QuZEn3d9sKAc= |