Skip to content

Commit

Permalink
add Values() method to get cache values (#94)
Browse files Browse the repository at this point in the history
* add Values() method to get cache values

* Update against generics

---------

Co-authored-by: Jeff Mitchell <jeffrey.mitchell@gmail.com>
  • Loading branch information
DoubleDi and jefferai committed Jun 6, 2023
1 parent b74c7fa commit bd154e2
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 0 deletions.
10 changes: 10 additions & 0 deletions 2q.go
Expand Up @@ -181,6 +181,16 @@ func (c *TwoQueueCache[K, V]) Keys() []K {
return append(k1, k2...)
}

// Values returns a slice of the values in the cache.
// The frequently used values are first in the returned slice.
func (c *TwoQueueCache[K, V]) Values() []V {
c.lock.RLock()
defer c.lock.RUnlock()
v1 := c.frequent.Values()
v2 := c.recent.Values()
return append(v1, v2...)
}

// Remove removes the provided key from the cache.
func (c *TwoQueueCache[K, V]) Remove(key K) {
c.lock.Lock()
Expand Down
5 changes: 5 additions & 0 deletions 2q_test.go
Expand Up @@ -236,6 +236,11 @@ func Test2Q(t *testing.T) {
t.Fatalf("bad key: %v", k)
}
}
for i, v := range l.Values() {
if v != i+128 {
t.Fatalf("bad key: %v", v)
}
}
for i := 0; i < 128; i++ {
if _, ok := l.Get(i); ok {
t.Fatalf("should be evicted")
Expand Down
9 changes: 9 additions & 0 deletions arc.go
Expand Up @@ -211,6 +211,15 @@ func (c *ARCCache[K, V]) Keys() []K {
return append(k1, k2...)
}

// Values returns all the cached values
func (c *ARCCache[K, V]) Values() []V {
c.lock.RLock()
defer c.lock.RUnlock()
v1 := c.t1.Values()
v2 := c.t2.Values()
return append(v1, v2...)
}

// Remove is used to purge a key from the cache
func (c *ARCCache[K, V]) Remove(key K) {
c.lock.Lock()
Expand Down
5 changes: 5 additions & 0 deletions arc_test.go
Expand Up @@ -308,6 +308,11 @@ func TestARC(t *testing.T) {
t.Fatalf("bad key: %v", k)
}
}
for i, v := range l.Values() {
if v != i+128 {
t.Fatalf("bad value: %v", v)
}
}
for i := 0; i < 128; i++ {
if _, ok := l.Get(i); ok {
t.Fatalf("should be evicted")
Expand Down
8 changes: 8 additions & 0 deletions lru.go
Expand Up @@ -233,6 +233,14 @@ func (c *Cache[K, V]) Keys() []K {
return keys
}

// Values returns a slice of the values in the cache, from oldest to newest.
func (c *Cache[K, V]) Values() []V {
c.lock.RLock()
values := c.lru.Values()
c.lock.RUnlock()
return values
}

// Len returns the number of items in the cache.
func (c *Cache[K, V]) Len() int {
c.lock.RLock()
Expand Down
5 changes: 5 additions & 0 deletions lru_test.go
Expand Up @@ -95,6 +95,11 @@ func TestLRU(t *testing.T) {
t.Fatalf("bad key: %v", k)
}
}
for i, v := range l.Values() {
if v != i+128 {
t.Fatalf("bad value: %v", v)
}
}
for i := 0; i < 128; i++ {
if _, ok := l.Get(i); ok {
t.Fatalf("should be evicted")
Expand Down
11 changes: 11 additions & 0 deletions simplelru/lru.go
Expand Up @@ -129,6 +129,17 @@ func (c *LRU[K, V]) Keys() []K {
return keys
}

// Values returns a slice of the values in the cache, from oldest to newest.
func (c *LRU[K, V]) Values() []V {
values := make([]V, len(c.items))
i := 0
for ent := c.evictList.back(); ent != nil; ent = ent.prevEntry() {
values[i] = ent.value
i++
}
return values
}

// Len returns the number of items in the cache.
func (c *LRU[K, V]) Len() int {
return c.evictList.length()
Expand Down
3 changes: 3 additions & 0 deletions simplelru/lru_interface.go
Expand Up @@ -32,6 +32,9 @@ type LRUCache[K comparable, V any] interface {
// Returns a slice of the keys in the cache, from oldest to newest.
Keys() []K

// Values returns a slice of the values in the cache, from oldest to newest.
Values() []V

// Returns the number of items in the cache.
Len() int

Expand Down
5 changes: 5 additions & 0 deletions simplelru/lru_test.go
Expand Up @@ -34,6 +34,11 @@ func TestLRU(t *testing.T) {
t.Fatalf("bad key: %v", k)
}
}
for i, v := range l.Values() {
if v != i+128 {
t.Fatalf("bad value: %v", v)
}
}
for i := 0; i < 128; i++ {
if _, ok := l.Get(i); ok {
t.Fatalf("should be evicted")
Expand Down

0 comments on commit bd154e2

Please sign in to comment.