This commit is contained in:
Aleksandrs Antonovs 2019-11-03 12:43:41 +01:00 committed by GitHub
commit 00834db9a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 0 deletions

View File

@ -6,6 +6,7 @@ import (
"io"
"os"
"runtime"
"sort"
"sync"
"time"
)
@ -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 {

View File

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