Skip to content

Commit eebd512

Browse files
authored
Merge pull request #926 from sigstore/dependabot/go_modules/github.com/hashicorp/golang-lru-1.0.2
chore(deps): Bump github.com/hashicorp/golang-lru from 0.5.4 to 1.0.2
2 parents b8a185a + 30f97d5 commit eebd512

File tree

11 files changed

+162
-52
lines changed

11 files changed

+162
-52
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ require (
2121
github.com/hashicorp/go-secure-stdlib/parseutil v0.1.7
2222
github.com/hashicorp/go-secure-stdlib/strutil v0.1.2
2323
github.com/hashicorp/go-sockaddr v1.0.2
24-
github.com/hashicorp/golang-lru v0.5.4
24+
github.com/hashicorp/golang-lru v1.0.2
2525
github.com/hashicorp/hcl v1.0.0
2626
github.com/kelseyhightower/envconfig v1.4.0
2727
github.com/letsencrypt/boulder v0.0.0-20221109233200-85aa52084eaf

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -570,8 +570,8 @@ github.com/hashicorp/go-sockaddr v1.0.2 h1:ztczhD1jLxIRjVejw8gFomI1BQZOe2WoVOu0S
570570
github.com/hashicorp/go-sockaddr v1.0.2/go.mod h1:rB4wwRAUzs07qva3c5SdrY/NEtAUjGlgmH/UkBUC97A=
571571
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
572572
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
573-
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
574-
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
573+
github.com/hashicorp/golang-lru v1.0.2 h1:dV3g9Z/unq5DpblPpw+Oqcv4dU/1omnb4Ok8iPY6p1c=
574+
github.com/hashicorp/golang-lru v1.0.2/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
575575
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
576576
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
577577
github.com/hashicorp/vault/api v1.9.2 h1:YjkZLJ7K3inKgMZ0wzCU9OHqc+UqMQyXsPXnf3Cl2as=
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
linters:
2+
enable:
3+
- megacheck
4+
- revive
5+
- govet
6+
- unconvert
7+
- megacheck
8+
- gas
9+
- gocyclo
10+
- dupl
11+
- misspell
12+
- unparam
13+
- unused
14+
- typecheck
15+
- ineffassign
16+
- stylecheck
17+
- exportloopref
18+
- gocritic
19+
- nakedret
20+
- gosimple
21+
- prealloc
22+
fast: false
23+
disable-all: true
24+
25+
issues:
26+
exclude-rules:
27+
- path: _test\.go
28+
linters:
29+
- dupl
30+
exclude-use-default: false

third_party/VENDOR-LICENSE/github.com/hashicorp/golang-lru/2q.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func New2Q(size int) (*TwoQueueCache, error) {
4444

4545
// New2QParams creates a new TwoQueueCache using the provided
4646
// parameter values.
47-
func New2QParams(size int, recentRatio float64, ghostRatio float64) (*TwoQueueCache, error) {
47+
func New2QParams(size int, recentRatio, ghostRatio float64) (*TwoQueueCache, error) {
4848
if size <= 0 {
4949
return nil, fmt.Errorf("invalid size")
5050
}
@@ -138,7 +138,6 @@ func (c *TwoQueueCache) Add(key, value interface{}) {
138138
// Add to the recently seen list
139139
c.ensureSpace(false)
140140
c.recent.Add(key, value)
141-
return
142141
}
143142

144143
// ensureSpace is used to ensure we have space in the cache

third_party/VENDOR-LICENSE/github.com/hashicorp/golang-lru/LICENSE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
Copyright (c) 2014 HashiCorp, Inc.
2+
13
Mozilla Public License, version 2.0
24

35
1. Definitions
Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,7 @@
11
golang-lru
22
==========
33

4-
This provides the `lru` package which implements a fixed-size
5-
thread safe LRU cache. It is based on the cache in Groupcache.
6-
7-
Documentation
8-
=============
9-
10-
Full docs are available on [Godoc](http://godoc.org/github.com/hashicorp/golang-lru)
11-
12-
Example
13-
=======
14-
15-
Using the LRU is very simple:
16-
17-
```go
18-
l, _ := New(128)
19-
for i := 0; i < 256; i++ {
20-
l.Add(i, nil)
21-
}
22-
if l.Len() != 128 {
23-
panic(fmt.Sprintf("bad len: %v", l.Len()))
24-
}
25-
```
4+
Please upgrade to github.com/hashicorp/golang-lru/v2 for all new code as v1 will
5+
not be updated anymore. The v2 version supports generics and is faster; old code
6+
can specify a specific tag, e.g. github.com/hashicorp/golang-lru/v1.0.2 for
7+
backwards compatibility.

third_party/VENDOR-LICENSE/github.com/hashicorp/golang-lru/arc.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ func (c *ARCCache) Add(key, value interface{}) {
173173

174174
// Add to the recently seen list
175175
c.t1.Add(key, value)
176-
return
177176
}
178177

179178
// replace is used to adaptively evict from either T1 or T2

third_party/VENDOR-LICENSE/github.com/hashicorp/golang-lru/lru.go

Lines changed: 100 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@ import (
66
"github.com/hashicorp/golang-lru/simplelru"
77
)
88

9+
const (
10+
// DefaultEvictedBufferSize defines the default buffer size to store evicted key/val
11+
DefaultEvictedBufferSize = 16
12+
)
13+
914
// Cache is a thread-safe fixed size LRU cache.
1015
type Cache struct {
11-
lru simplelru.LRUCache
12-
lock sync.RWMutex
16+
lru *simplelru.LRU
17+
evictedKeys, evictedVals []interface{}
18+
onEvictedCB func(k, v interface{})
19+
lock sync.RWMutex
1320
}
1421

1522
// New creates an LRU of the given size.
@@ -19,30 +26,63 @@ func New(size int) (*Cache, error) {
1926

2027
// NewWithEvict constructs a fixed size cache with the given eviction
2128
// callback.
22-
func NewWithEvict(size int, onEvicted func(key interface{}, value interface{})) (*Cache, error) {
23-
lru, err := simplelru.NewLRU(size, simplelru.EvictCallback(onEvicted))
24-
if err != nil {
25-
return nil, err
29+
func NewWithEvict(size int, onEvicted func(key, value interface{})) (c *Cache, err error) {
30+
// create a cache with default settings
31+
c = &Cache{
32+
onEvictedCB: onEvicted,
2633
}
27-
c := &Cache{
28-
lru: lru,
34+
if onEvicted != nil {
35+
c.initEvictBuffers()
36+
onEvicted = c.onEvicted
2937
}
30-
return c, nil
38+
c.lru, err = simplelru.NewLRU(size, onEvicted)
39+
return
40+
}
41+
42+
func (c *Cache) initEvictBuffers() {
43+
c.evictedKeys = make([]interface{}, 0, DefaultEvictedBufferSize)
44+
c.evictedVals = make([]interface{}, 0, DefaultEvictedBufferSize)
45+
}
46+
47+
// onEvicted save evicted key/val and sent in externally registered callback
48+
// outside of critical section
49+
func (c *Cache) onEvicted(k, v interface{}) {
50+
c.evictedKeys = append(c.evictedKeys, k)
51+
c.evictedVals = append(c.evictedVals, v)
3152
}
3253

3354
// Purge is used to completely clear the cache.
3455
func (c *Cache) Purge() {
56+
var ks, vs []interface{}
3557
c.lock.Lock()
3658
c.lru.Purge()
59+
if c.onEvictedCB != nil && len(c.evictedKeys) > 0 {
60+
ks, vs = c.evictedKeys, c.evictedVals
61+
c.initEvictBuffers()
62+
}
3763
c.lock.Unlock()
64+
// invoke callback outside of critical section
65+
if c.onEvictedCB != nil {
66+
for i := 0; i < len(ks); i++ {
67+
c.onEvictedCB(ks[i], vs[i])
68+
}
69+
}
3870
}
3971

4072
// Add adds a value to the cache. Returns true if an eviction occurred.
4173
func (c *Cache) Add(key, value interface{}) (evicted bool) {
74+
var k, v interface{}
4275
c.lock.Lock()
4376
evicted = c.lru.Add(key, value)
77+
if c.onEvictedCB != nil && evicted {
78+
k, v = c.evictedKeys[0], c.evictedVals[0]
79+
c.evictedKeys, c.evictedVals = c.evictedKeys[:0], c.evictedVals[:0]
80+
}
4481
c.lock.Unlock()
45-
return evicted
82+
if c.onEvictedCB != nil && evicted {
83+
c.onEvictedCB(k, v)
84+
}
85+
return
4686
}
4787

4888
// Get looks up a key's value from the cache.
@@ -75,61 +115,102 @@ func (c *Cache) Peek(key interface{}) (value interface{}, ok bool) {
75115
// recent-ness or deleting it for being stale, and if not, adds the value.
76116
// Returns whether found and whether an eviction occurred.
77117
func (c *Cache) ContainsOrAdd(key, value interface{}) (ok, evicted bool) {
118+
var k, v interface{}
78119
c.lock.Lock()
79-
defer c.lock.Unlock()
80-
81120
if c.lru.Contains(key) {
121+
c.lock.Unlock()
82122
return true, false
83123
}
84124
evicted = c.lru.Add(key, value)
125+
if c.onEvictedCB != nil && evicted {
126+
k, v = c.evictedKeys[0], c.evictedVals[0]
127+
c.evictedKeys, c.evictedVals = c.evictedKeys[:0], c.evictedVals[:0]
128+
}
129+
c.lock.Unlock()
130+
if c.onEvictedCB != nil && evicted {
131+
c.onEvictedCB(k, v)
132+
}
85133
return false, evicted
86134
}
87135

88136
// PeekOrAdd checks if a key is in the cache without updating the
89137
// recent-ness or deleting it for being stale, and if not, adds the value.
90138
// Returns whether found and whether an eviction occurred.
91139
func (c *Cache) PeekOrAdd(key, value interface{}) (previous interface{}, ok, evicted bool) {
140+
var k, v interface{}
92141
c.lock.Lock()
93-
defer c.lock.Unlock()
94-
95142
previous, ok = c.lru.Peek(key)
96143
if ok {
144+
c.lock.Unlock()
97145
return previous, true, false
98146
}
99-
100147
evicted = c.lru.Add(key, value)
148+
if c.onEvictedCB != nil && evicted {
149+
k, v = c.evictedKeys[0], c.evictedVals[0]
150+
c.evictedKeys, c.evictedVals = c.evictedKeys[:0], c.evictedVals[:0]
151+
}
152+
c.lock.Unlock()
153+
if c.onEvictedCB != nil && evicted {
154+
c.onEvictedCB(k, v)
155+
}
101156
return nil, false, evicted
102157
}
103158

104159
// Remove removes the provided key from the cache.
105160
func (c *Cache) Remove(key interface{}) (present bool) {
161+
var k, v interface{}
106162
c.lock.Lock()
107163
present = c.lru.Remove(key)
164+
if c.onEvictedCB != nil && present {
165+
k, v = c.evictedKeys[0], c.evictedVals[0]
166+
c.evictedKeys, c.evictedVals = c.evictedKeys[:0], c.evictedVals[:0]
167+
}
108168
c.lock.Unlock()
169+
if c.onEvictedCB != nil && present {
170+
c.onEvictedCB(k, v)
171+
}
109172
return
110173
}
111174

112175
// Resize changes the cache size.
113176
func (c *Cache) Resize(size int) (evicted int) {
177+
var ks, vs []interface{}
114178
c.lock.Lock()
115179
evicted = c.lru.Resize(size)
180+
if c.onEvictedCB != nil && evicted > 0 {
181+
ks, vs = c.evictedKeys, c.evictedVals
182+
c.initEvictBuffers()
183+
}
116184
c.lock.Unlock()
185+
if c.onEvictedCB != nil && evicted > 0 {
186+
for i := 0; i < len(ks); i++ {
187+
c.onEvictedCB(ks[i], vs[i])
188+
}
189+
}
117190
return evicted
118191
}
119192

120193
// RemoveOldest removes the oldest item from the cache.
121-
func (c *Cache) RemoveOldest() (key interface{}, value interface{}, ok bool) {
194+
func (c *Cache) RemoveOldest() (key, value interface{}, ok bool) {
195+
var k, v interface{}
122196
c.lock.Lock()
123197
key, value, ok = c.lru.RemoveOldest()
198+
if c.onEvictedCB != nil && ok {
199+
k, v = c.evictedKeys[0], c.evictedVals[0]
200+
c.evictedKeys, c.evictedVals = c.evictedKeys[:0], c.evictedVals[:0]
201+
}
124202
c.lock.Unlock()
203+
if c.onEvictedCB != nil && ok {
204+
c.onEvictedCB(k, v)
205+
}
125206
return
126207
}
127208

128209
// GetOldest returns the oldest entry
129-
func (c *Cache) GetOldest() (key interface{}, value interface{}, ok bool) {
130-
c.lock.Lock()
210+
func (c *Cache) GetOldest() (key, value interface{}, ok bool) {
211+
c.lock.RLock()
131212
key, value, ok = c.lru.GetOldest()
132-
c.lock.Unlock()
213+
c.lock.RUnlock()
133214
return
134215
}
135216

third_party/VENDOR-LICENSE/github.com/hashicorp/golang-lru/simplelru/lru.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type entry struct {
2525
// NewLRU constructs an LRU of the given size
2626
func NewLRU(size int, onEvict EvictCallback) (*LRU, error) {
2727
if size <= 0 {
28-
return nil, errors.New("Must provide a positive size")
28+
return nil, errors.New("must provide a positive size")
2929
}
3030
c := &LRU{
3131
size: size,
@@ -109,7 +109,7 @@ func (c *LRU) Remove(key interface{}) (present bool) {
109109
}
110110

111111
// RemoveOldest removes the oldest item from the cache.
112-
func (c *LRU) RemoveOldest() (key interface{}, value interface{}, ok bool) {
112+
func (c *LRU) RemoveOldest() (key, value interface{}, ok bool) {
113113
ent := c.evictList.Back()
114114
if ent != nil {
115115
c.removeElement(ent)
@@ -120,7 +120,7 @@ func (c *LRU) RemoveOldest() (key interface{}, value interface{}, ok bool) {
120120
}
121121

122122
// GetOldest returns the oldest entry
123-
func (c *LRU) GetOldest() (key interface{}, value interface{}, ok bool) {
123+
func (c *LRU) GetOldest() (key, value interface{}, ok bool) {
124124
ent := c.evictList.Back()
125125
if ent != nil {
126126
kv := ent.Value.(*entry)

third_party/VENDOR-LICENSE/github.com/hashicorp/golang-lru/simplelru/lru_interface.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package simplelru provides simple LRU implementation based on build-in container/list.
12
package simplelru
23

34
// LRUCache is the interface for simple LRU cache.
@@ -34,6 +35,6 @@ type LRUCache interface {
3435
// Clears all cache entries.
3536
Purge()
3637

37-
// Resizes cache, returning number evicted
38-
Resize(int) int
38+
// Resizes cache, returning number evicted
39+
Resize(int) int
3940
}

0 commit comments

Comments
 (0)