diff options
author | GitLab Renovate Bot <gitlab-bot@gitlab.com> | 2022-11-14 23:19:58 +0300 |
---|---|---|
committer | GitLab Renovate Bot <gitlab-bot@gitlab.com> | 2022-11-14 23:19:58 +0300 |
commit | 58c35a1fbe188efe1e784bb863432e3ac05e45c8 (patch) | |
tree | 51d90bdbed368b5aeb1c6e8248513ff71cb42e3c /NOTICE | |
parent | 4b21b45f897007e0d3428353913edac8d1ecc422 (diff) |
go: Update module github.com/hashicorp/golang-lru to v0.6.0
Diffstat (limited to 'NOTICE')
-rw-r--r-- | NOTICE | 261 |
1 files changed, 214 insertions, 47 deletions
@@ -10135,6 +10135,41 @@ func BenchmarkGenerateUUIDWithReader(b *testing.B) { } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +ci.yml - github.com/hashicorp/golang-lru/.github/workflows +name: build + +on: + push: + branches: + tags: + pull_request: + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: set up go 1.19 + uses: actions/setup-go@v1 + with: + go-version: 1.19 + id: go + + - name: checkout + uses: actions/checkout@v2 + + - name: build and test + run: | + go test -timeout=60s -race + go build -race + + - name: install golangci-lint + run: curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $GITHUB_WORKSPACE v1.50.1 + + - name: run golangci-lint + run: $GITHUB_WORKSPACE/golangci-lint run --out-format=github-actions + +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .gitignore - github.com/hashicorp/golang-lru # Compiled Object files, Static and Dynamic libs (Shared Objects) *.o @@ -10161,6 +10196,39 @@ _testmain.go *.test ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +.golangci.yml - github.com/hashicorp/golang-lru +linters: + enable: + - megacheck + - revive + - govet + - unconvert + - megacheck + - gas + - gocyclo + - dupl + - misspell + - unparam + - unused + - typecheck + - ineffassign + - stylecheck + - exportloopref + - gocritic + - nakedret + - gosimple + - prealloc + fast: false + disable-all: true + +issues: + exclude-rules: + - path: _test\.go + linters: + - dupl + exclude-use-default: false + +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 2q.go - github.com/hashicorp/golang-lru package lru @@ -10208,7 +10276,7 @@ func New2Q(size int) (*TwoQueueCache, error) { // New2QParams creates a new TwoQueueCache using the provided // parameter values. -func New2QParams(size int, recentRatio float64, ghostRatio float64) (*TwoQueueCache, error) { +func New2QParams(size int, recentRatio, ghostRatio float64) (*TwoQueueCache, error) { if size <= 0 { return nil, fmt.Errorf("invalid size") } @@ -10302,7 +10370,6 @@ func (c *TwoQueueCache) Add(key, value interface{}) { // Add to the recently seen list c.ensureSpace(false) c.recent.Add(key, value) - return } // ensureSpace is used to ensure we have space in the cache @@ -10391,7 +10458,6 @@ func (c *TwoQueueCache) Peek(key interface{}) (value interface{}, ok bool) { package lru import ( - "math/rand" "testing" ) @@ -10403,7 +10469,7 @@ func Benchmark2Q_Rand(b *testing.B) { trace := make([]int64, b.N*2) for i := 0; i < b.N*2; i++ { - trace[i] = rand.Int63() % 32768 + trace[i] = getRand(b) % 32768 } b.ResetTimer() @@ -10433,9 +10499,9 @@ func Benchmark2Q_Freq(b *testing.B) { trace := make([]int64, b.N*2) for i := 0; i < b.N*2; i++ { if i%2 == 0 { - trace[i] = rand.Int63() % 16384 + trace[i] = getRand(b) % 16384 } else { - trace[i] = rand.Int63() % 32768 + trace[i] = getRand(b) % 32768 } } @@ -10465,8 +10531,8 @@ func Test2Q_RandomOps(t *testing.T) { n := 200000 for i := 0; i < n; i++ { - key := rand.Int63() % 512 - r := rand.Int63() + key := getRand(t) % 512 + r := getRand(t) switch r % 3 { case 0: l.Add(key, key) @@ -10697,6 +10763,8 @@ func Test2Q_Peek(t *testing.T) { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ LICENSE - github.com/hashicorp/golang-lru +Copyright (c) 2014 HashiCorp, Inc. + Mozilla Public License, version 2.0 1. Definitions @@ -11071,7 +11139,7 @@ thread safe LRU cache. It is based on the cache in Groupcache. Documentation ============= -Full docs are available on [Godoc](http://godoc.org/github.com/hashicorp/golang-lru) +Full docs are available on [Godoc](https://pkg.go.dev/github.com/hashicorp/golang-lru) Example ======= @@ -11265,7 +11333,6 @@ func (c *ARCCache) Add(key, value interface{}) { // Add to the recently seen list c.t1.Add(key, value) - return } // replace is used to adaptively evict from either T1 or T2 @@ -11370,7 +11437,7 @@ func BenchmarkARC_Rand(b *testing.B) { trace := make([]int64, b.N*2) for i := 0; i < b.N*2; i++ { - trace[i] = rand.Int63() % 32768 + trace[i] = getRand(b) % 32768 } b.ResetTimer() @@ -11400,9 +11467,9 @@ func BenchmarkARC_Freq(b *testing.B) { trace := make([]int64, b.N*2) for i := 0; i < b.N*2; i++ { if i%2 == 0 { - trace[i] = rand.Int63() % 16384 + trace[i] = getRand(b) % 16384 } else { - trace[i] = rand.Int63() % 32768 + trace[i] = getRand(b) % 32768 } } @@ -11432,8 +11499,8 @@ func TestARC_RandomOps(t *testing.T) { n := 200000 for i := 0; i < n; i++ { - key := rand.Int63() % 512 - r := rand.Int63() + key := getRand(t) % 512 + r := getRand(t) switch r % 3 { case 0: l.Add(key, key) @@ -11768,10 +11835,17 @@ import ( "github.com/hashicorp/golang-lru/simplelru" ) +const ( + // DefaultEvictedBufferSize defines the default buffer size to store evicted key/val + DefaultEvictedBufferSize = 16 +) + // Cache is a thread-safe fixed size LRU cache. type Cache struct { - lru simplelru.LRUCache - lock sync.RWMutex + lru *simplelru.LRU + evictedKeys, evictedVals []interface{} + onEvictedCB func(k, v interface{}) + lock sync.RWMutex } // New creates an LRU of the given size. @@ -11781,30 +11855,63 @@ func New(size int) (*Cache, error) { // NewWithEvict constructs a fixed size cache with the given eviction // callback. -func NewWithEvict(size int, onEvicted func(key interface{}, value interface{})) (*Cache, error) { - lru, err := simplelru.NewLRU(size, simplelru.EvictCallback(onEvicted)) - if err != nil { - return nil, err +func NewWithEvict(size int, onEvicted func(key, value interface{})) (c *Cache, err error) { + // create a cache with default settings + c = &Cache{ + onEvictedCB: onEvicted, } - c := &Cache{ - lru: lru, + if onEvicted != nil { + c.initEvictBuffers() + onEvicted = c.onEvicted } - return c, nil + c.lru, err = simplelru.NewLRU(size, onEvicted) + return +} + +func (c *Cache) initEvictBuffers() { + c.evictedKeys = make([]interface{}, 0, DefaultEvictedBufferSize) + c.evictedVals = make([]interface{}, 0, DefaultEvictedBufferSize) +} + +// onEvicted save evicted key/val and sent in externally registered callback +// outside of critical section +func (c *Cache) onEvicted(k, v interface{}) { + c.evictedKeys = append(c.evictedKeys, k) + c.evictedVals = append(c.evictedVals, v) } // Purge is used to completely clear the cache. func (c *Cache) Purge() { + var ks, vs []interface{} c.lock.Lock() c.lru.Purge() + if c.onEvictedCB != nil && len(c.evictedKeys) > 0 { + ks, vs = c.evictedKeys, c.evictedVals + c.initEvictBuffers() + } c.lock.Unlock() + // invoke callback outside of critical section + if c.onEvictedCB != nil { + for i := 0; i < len(ks); i++ { + c.onEvictedCB(ks[i], vs[i]) + } + } } // Add adds a value to the cache. Returns true if an eviction occurred. func (c *Cache) Add(key, value interface{}) (evicted bool) { + var k, v interface{} c.lock.Lock() evicted = c.lru.Add(key, value) + if c.onEvictedCB != nil && evicted { + k, v = c.evictedKeys[0], c.evictedVals[0] + c.evictedKeys, c.evictedVals = c.evictedKeys[:0], c.evictedVals[:0] + } c.lock.Unlock() - return evicted + if c.onEvictedCB != nil && evicted { + c.onEvictedCB(k, v) + } + return } // Get looks up a key's value from the cache. @@ -11837,13 +11944,21 @@ func (c *Cache) Peek(key interface{}) (value interface{}, ok bool) { // recent-ness or deleting it for being stale, and if not, adds the value. // Returns whether found and whether an eviction occurred. func (c *Cache) ContainsOrAdd(key, value interface{}) (ok, evicted bool) { + var k, v interface{} c.lock.Lock() - defer c.lock.Unlock() - if c.lru.Contains(key) { + c.lock.Unlock() return true, false } evicted = c.lru.Add(key, value) + if c.onEvictedCB != nil && evicted { + k, v = c.evictedKeys[0], c.evictedVals[0] + c.evictedKeys, c.evictedVals = c.evictedKeys[:0], c.evictedVals[:0] + } + c.lock.Unlock() + if c.onEvictedCB != nil && evicted { + c.onEvictedCB(k, v) + } return false, evicted } @@ -11851,47 +11966,80 @@ func (c *Cache) ContainsOrAdd(key, value interface{}) (ok, evicted bool) { // recent-ness or deleting it for being stale, and if not, adds the value. // Returns whether found and whether an eviction occurred. func (c *Cache) PeekOrAdd(key, value interface{}) (previous interface{}, ok, evicted bool) { + var k, v interface{} c.lock.Lock() - defer c.lock.Unlock() - previous, ok = c.lru.Peek(key) if ok { + c.lock.Unlock() return previous, true, false } - evicted = c.lru.Add(key, value) + if c.onEvictedCB != nil && evicted { + k, v = c.evictedKeys[0], c.evictedVals[0] + c.evictedKeys, c.evictedVals = c.evictedKeys[:0], c.evictedVals[:0] + } + c.lock.Unlock() + if c.onEvictedCB != nil && evicted { + c.onEvictedCB(k, v) + } return nil, false, evicted } // Remove removes the provided key from the cache. func (c *Cache) Remove(key interface{}) (present bool) { + var k, v interface{} c.lock.Lock() present = c.lru.Remove(key) + if c.onEvictedCB != nil && present { + k, v = c.evictedKeys[0], c.evictedVals[0] + c.evictedKeys, c.evictedVals = c.evictedKeys[:0], c.evictedVals[:0] + } c.lock.Unlock() + if c.onEvictedCB != nil && present { + c.onEvictedCB(k, v) + } return } // Resize changes the cache size. func (c *Cache) Resize(size int) (evicted int) { + var ks, vs []interface{} c.lock.Lock() evicted = c.lru.Resize(size) + if c.onEvictedCB != nil && evicted > 0 { + ks, vs = c.evictedKeys, c.evictedVals + c.initEvictBuffers() + } c.lock.Unlock() + if c.onEvictedCB != nil && evicted > 0 { + for i := 0; i < len(ks); i++ { + c.onEvictedCB(ks[i], vs[i]) + } + } return evicted } // RemoveOldest removes the oldest item from the cache. -func (c *Cache) RemoveOldest() (key interface{}, value interface{}, ok bool) { +func (c *Cache) RemoveOldest() (key, value interface{}, ok bool) { + var k, v interface{} c.lock.Lock() key, value, ok = c.lru.RemoveOldest() + if c.onEvictedCB != nil && ok { + k, v = c.evictedKeys[0], c.evictedVals[0] + c.evictedKeys, c.evictedVals = c.evictedKeys[:0], c.evictedVals[:0] + } c.lock.Unlock() + if c.onEvictedCB != nil && ok { + c.onEvictedCB(k, v) + } return } // GetOldest returns the oldest entry -func (c *Cache) GetOldest() (key interface{}, value interface{}, ok bool) { - c.lock.Lock() +func (c *Cache) GetOldest() (key, value interface{}, ok bool) { + c.lock.RLock() key, value, ok = c.lru.GetOldest() - c.lock.Unlock() + c.lock.RUnlock() return } @@ -11916,7 +12064,6 @@ lru_test.go - github.com/hashicorp/golang-lru package lru import ( - "math/rand" "testing" ) @@ -11928,7 +12075,7 @@ func BenchmarkLRU_Rand(b *testing.B) { trace := make([]int64, b.N*2) for i := 0; i < b.N*2; i++ { - trace[i] = rand.Int63() % 32768 + trace[i] = getRand(b) % 32768 } b.ResetTimer() @@ -11958,9 +12105,9 @@ func BenchmarkLRU_Freq(b *testing.B) { trace := make([]int64, b.N*2) for i := 0; i < b.N*2; i++ { if i%2 == 0 { - trace[i] = rand.Int63() % 16384 + trace[i] = getRand(b) % 16384 } else { - trace[i] = rand.Int63() % 32768 + trace[i] = getRand(b) % 32768 } } @@ -12182,7 +12329,7 @@ func TestLRUResize(t *testing.T) { // Downsize l.Add(1, 1) l.Add(2, 2) - evicted := l.Resize(1); + evicted := l.Resize(1) if evicted != 1 { t.Errorf("1 element should have been evicted: %v", evicted) } @@ -12196,7 +12343,7 @@ func TestLRUResize(t *testing.T) { } // Upsize - evicted = l.Resize(2); + evicted = l.Resize(2) if evicted != 0 { t.Errorf("0 elements should have been evicted: %v", evicted) } @@ -12236,7 +12383,7 @@ type entry struct { // NewLRU constructs an LRU of the given size func NewLRU(size int, onEvict EvictCallback) (*LRU, error) { if size <= 0 { - return nil, errors.New("Must provide a positive size") + return nil, errors.New("must provide a positive size") } c := &LRU{ size: size, @@ -12320,7 +12467,7 @@ func (c *LRU) Remove(key interface{}) (present bool) { } // RemoveOldest removes the oldest item from the cache. -func (c *LRU) RemoveOldest() (key interface{}, value interface{}, ok bool) { +func (c *LRU) RemoveOldest() (key, value interface{}, ok bool) { ent := c.evictList.Back() if ent != nil { c.removeElement(ent) @@ -12331,7 +12478,7 @@ func (c *LRU) RemoveOldest() (key interface{}, value interface{}, ok bool) { } // GetOldest returns the oldest entry -func (c *LRU) GetOldest() (key interface{}, value interface{}, ok bool) { +func (c *LRU) GetOldest() (key, value interface{}, ok bool) { ent := c.evictList.Back() if ent != nil { kv := ent.Value.(*entry) @@ -12389,6 +12536,7 @@ func (c *LRU) removeElement(e *list.Element) { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ lru_interface.go - github.com/hashicorp/golang-lru/simplelru +// Package simplelru provides simple LRU implementation based on build-in container/list. package simplelru // LRUCache is the interface for simple LRU cache. @@ -12425,8 +12573,8 @@ type LRUCache interface { // Clears all cache entries. Purge() - // Resizes cache, returning number evicted - Resize(int) int + // Resizes cache, returning number evicted + Resize(int) int } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -12613,7 +12761,7 @@ func TestLRU_Resize(t *testing.T) { // Downsize l.Add(1, 1) l.Add(2, 2) - evicted := l.Resize(1); + evicted := l.Resize(1) if evicted != 1 { t.Errorf("1 element should have been evicted: %v", evicted) } @@ -12627,7 +12775,7 @@ func TestLRU_Resize(t *testing.T) { } // Upsize - evicted = l.Resize(2); + evicted = l.Resize(2) if evicted != 0 { t.Errorf("0 elements should have been evicted: %v", evicted) } @@ -12639,6 +12787,25 @@ func TestLRU_Resize(t *testing.T) { } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +testing.go - github.com/hashicorp/golang-lru +package lru + +import ( + "crypto/rand" + "math" + "math/big" + "testing" +) + +func getRand(tb testing.TB) int64 { + out, err := rand.Int(rand.Reader, big.NewInt(math.MaxInt64)) + if err != nil { + tb.Fatal(err) + } + return out.Int64() +} + +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .gitignore - github.com/hashicorp/yamux # Compiled Object files, Static and Dynamic libs (Shared Objects) *.o |