Skip to content

Commit

Permalink
Bump hashicorp/golang-lru
Browse files Browse the repository at this point in the history
This will bump the dependencie to have the go.mod fix
helping to have proper license file

Right now the current local system golang version
was getting added to license-file everytime
during development. this bump will fix that
  • Loading branch information
piyush-garg authored and tekton-robot committed Dec 28, 2019
1 parent 337d29d commit ec3cb7a
Show file tree
Hide file tree
Showing 13 changed files with 172 additions and 13 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d // indirect
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
github.com/hako/durafmt v0.0.0-20180520121703-7b7ae1e72ead
github.com/hashicorp/golang-lru v0.5.3 // indirect
github.com/hinshun/vt10x v0.0.0-20180809195222-d55458df857c
github.com/imdario/mergo v0.3.8 // indirect
github.com/jonboulle/clockwork v0.1.1-0.20190114141812-62fb9bc030d1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ github.com/hako/durafmt v0.0.0-20180520121703-7b7ae1e72ead/go.mod h1:5Scbynm8dF1
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.3 h1:YPkqC67at8FYaadspW/6uE0COsBxS2656RLEr8Bppgk=
github.com/hashicorp/golang-lru v0.5.3/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174/go.mod h1:DqJ97dSdRW1W22yXSB90986pcOyQ7r45iio1KN2ez1A=
github.com/hinshun/vt10x v0.0.0-20180809195222-d55458df857c h1:kp3AxgXgDOmIJFR7bIwqFhwJ2qWar8tEQSE5XXhCfVk=
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module github.com/hashicorp/golang-lru

go 1.12
26 changes: 22 additions & 4 deletions third_party/VENDOR-LICENSE/github.com/hashicorp/golang-lru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,35 @@ func (c *Cache) ContainsOrAdd(key, value interface{}) (ok, evicted bool) {
}

// Remove removes the provided key from the cache.
func (c *Cache) Remove(key interface{}) {
func (c *Cache) Remove(key interface{}) (present bool) {
c.lock.Lock()
c.lru.Remove(key)
present = c.lru.Remove(key)
c.lock.Unlock()
return
}

// Resize changes the cache size.
func (c *Cache) Resize(size int) (evicted int) {
c.lock.Lock()
evicted = c.lru.Resize(size)
c.lock.Unlock()
return evicted
}

// RemoveOldest removes the oldest item from the cache.
func (c *Cache) RemoveOldest() {
func (c *Cache) RemoveOldest() (key interface{}, value interface{}, ok bool) {
c.lock.Lock()
key, value, ok = c.lru.RemoveOldest()
c.lock.Unlock()
return
}

// GetOldest returns the oldest entry
func (c *Cache) GetOldest() (key interface{}, value interface{}, ok bool) {
c.lock.Lock()
c.lru.RemoveOldest()
key, value, ok = c.lru.GetOldest()
c.lock.Unlock()
return
}

// Keys returns a slice of the keys in the cache, from oldest to newest.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,42 @@ func TestLRUPeek(t *testing.T) {
t.Errorf("should not have updated recent-ness of 1")
}
}

// test that Resize can upsize and downsize
func TestLRUResize(t *testing.T) {
onEvictCounter := 0
onEvicted := func(k interface{}, v interface{}) {
onEvictCounter++
}
l, err := NewWithEvict(2, onEvicted)
if err != nil {
t.Fatalf("err: %v", err)
}

// Downsize
l.Add(1, 1)
l.Add(2, 2)
evicted := l.Resize(1);
if evicted != 1 {
t.Errorf("1 element should have been evicted: %v", evicted)
}
if onEvictCounter != 1 {
t.Errorf("onEvicted should have been called 1 time: %v", onEvictCounter)
}

l.Add(3, 3)
if l.Contains(1) {
t.Errorf("Element 1 should have been evicted")
}

// Upsize
evicted = l.Resize(2);
if evicted != 0 {
t.Errorf("0 elements should have been evicted: %v", evicted)
}

l.Add(4, 4)
if !l.Contains(3) || !l.Contains(4) {
t.Errorf("Cache should have contained 2 elements")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ func (c *LRU) Add(key, value interface{}) (evicted bool) {
func (c *LRU) Get(key interface{}) (value interface{}, ok bool) {
if ent, ok := c.items[key]; ok {
c.evictList.MoveToFront(ent)
if ent.Value.(*entry) == nil {
return nil, false
}
return ent.Value.(*entry).value, true
}
return
Expand Down Expand Up @@ -142,6 +145,19 @@ func (c *LRU) Len() int {
return c.evictList.Len()
}

// Resize changes the cache size.
func (c *LRU) Resize(size int) (evicted int) {
diff := c.Len() - size
if diff < 0 {
diff = 0
}
for i := 0; i < diff; i++ {
c.removeOldest()
}
c.size = size
return diff
}

// removeOldest removes the oldest item from the cache.
func (c *LRU) removeOldest() {
ent := c.evictList.Back()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type LRUCache interface {
// updates the "recently used"-ness of the key. #value, isFound
Get(key interface{}) (value interface{}, ok bool)

// Check if a key exsists in cache without updating the recent-ness.
// Checks if a key exists in cache without updating the recent-ness.
Contains(key interface{}) (ok bool)

// Returns key's value without updating the "recently used"-ness of the key.
Expand All @@ -31,6 +31,9 @@ type LRUCache interface {
// Returns the number of items in the cache.
Len() int

// Clear all cache entries
// Clears all cache entries.
Purge()

// Resizes cache, returning number evicted
Resize(int) int
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,42 @@ func TestLRU_Peek(t *testing.T) {
t.Errorf("should not have updated recent-ness of 1")
}
}

// Test that Resize can upsize and downsize
func TestLRU_Resize(t *testing.T) {
onEvictCounter := 0
onEvicted := func(k interface{}, v interface{}) {
onEvictCounter++
}
l, err := NewLRU(2, onEvicted)
if err != nil {
t.Fatalf("err: %v", err)
}

// Downsize
l.Add(1, 1)
l.Add(2, 2)
evicted := l.Resize(1);
if evicted != 1 {
t.Errorf("1 element should have been evicted: %v", evicted)
}
if onEvictCounter != 1 {
t.Errorf("onEvicted should have been called 1 time: %v", onEvictCounter)
}

l.Add(3, 3)
if l.Contains(1) {
t.Errorf("Element 1 should have been evicted")
}

// Upsize
evicted = l.Resize(2);
if evicted != 0 {
t.Errorf("0 elements should have been evicted: %v", evicted)
}

l.Add(4, 4)
if !l.Contains(3) || !l.Contains(4) {
t.Errorf("Cache should have contained 2 elements")
}
}
2 changes: 2 additions & 0 deletions vendor/github.com/hashicorp/golang-lru/go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 22 additions & 4 deletions vendor/github.com/hashicorp/golang-lru/lru.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions vendor/github.com/hashicorp/golang-lru/simplelru/lru.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ github.com/gregjones/httpcache
github.com/gregjones/httpcache/diskcache
# github.com/hako/durafmt v0.0.0-20180520121703-7b7ae1e72ead
github.com/hako/durafmt
# github.com/hashicorp/golang-lru v0.5.1
# github.com/hashicorp/golang-lru v0.5.3
github.com/hashicorp/golang-lru
github.com/hashicorp/golang-lru/simplelru
# github.com/hinshun/vt10x v0.0.0-20180809195222-d55458df857c
Expand Down

0 comments on commit ec3cb7a

Please sign in to comment.