Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"os"
"runtime"
"sort"
"sync"
"time"
)
Expand Down Expand Up @@ -1052,6 +1053,20 @@ func (c *cache) Items() map[string]Item {
return m
}

// Keys returns a sorted slice of all the keys in the cache.
func (c *cache) Keys() []string {
c.mu.RLock()
defer c.mu.RUnlock()
keys := make([]string, len(c.items))
var i int
for k := range c.items {
keys[i] = k
i++
}
sort.Strings(keys)
return keys
}

// Returns the number of items in the cache. This may include items that have
// expired, but have not yet been cleaned up.
func (c *cache) ItemCount() int {
Expand Down
18 changes: 18 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ func TestCache(t *testing.T) {
}
}

func TestCacheKeys(t *testing.T) {
tc := New(DefaultExpiration, 0)

tc.Set("a", 1, DefaultExpiration)
tc.Set("b", 2, DefaultExpiration)
tc.Set("c", 3, DefaultExpiration)

keys := tc.Keys()

if len(keys) != 3 {
t.Error("invalid number of cache keys received")
}

if keys[0] != "a" || keys[1] != "b" || keys[2] != "c" {
t.Error("invalid cache keys received")
}
}

func TestCacheTimes(t *testing.T) {
var found bool

Expand Down