Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add O(1) ItemCount method to retrieve items currently in the cache ma…

…p (whether expired and not cleaned up or not)
  • Loading branch information...
commit c9f5754801381ed147c0593a3723e634e9be7aab 1 parent d5d03c2
@pmylund authored
Showing with 20 additions and 0 deletions.
  1. +10 −0 cache.go
  2. +10 −0 cache_test.go
View
10 cache.go
@@ -49,6 +49,7 @@ type unexportedInterface interface {
DecrementFloat64(string, float64) (float64, error)
Delete(string)
DeleteExpired()
+ ItemCount() int
Flush()
Save(io.Writer) error
SaveFile(string) error
@@ -933,6 +934,15 @@ func (c *cache) LoadFile(fname string) error {
return fp.Close()
}
+// 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 {
+ c.Lock()
+ n := len(c.items)
+ c.Unlock()
+ return n
+}
+
// Delete all items from the cache.
func (c *cache) Flush() {
c.Lock()
View
10 cache_test.go
@@ -1122,6 +1122,16 @@ func TestDelete(t *testing.T) {
}
}
+func TestItemCount(t *testing.T) {
+ tc := New(0, 0)
+ tc.Set("foo", "1", 0)
+ tc.Set("bar", "2", 0)
+ tc.Set("baz", "3", 0)
+ if n := tc.ItemCount(); n != 3 {
+ t.Errorf("Item count is not 3: %d", n)
+ }
+}
+
func TestFlush(t *testing.T) {
tc := New(0, 0)
tc.Set("foo", "bar", 0)
Please sign in to comment.
Something went wrong with that request. Please try again.