Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Values() method to get cache values #94

Merged
merged 3 commits into from Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) Values() []interface{} {
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) Values() []interface{} {
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) Values() []interface{} {
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 @@
return keys
}

// Values returns a slice of the values in the cache, from oldest to newest.
func (c *LRU) Values() []interface{} {

Check failure on line 133 in simplelru/lru.go

View workflow job for this annotation

GitHub Actions / build

cannot use generic type LRU[K comparable, V any] without instantiation
values := make([]interface{}, len(c.items))
i := 0
for ent := c.evictList.Back(); ent != nil; ent = ent.Prev() {
values[i] = ent.Value.(*entry).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() []interface{}

// 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