Skip to content

Commit

Permalink
added GetAll function
Browse files Browse the repository at this point in the history
  • Loading branch information
John Farley committed Apr 24, 2020
1 parent 4a6e12a commit a3441f4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
16 changes: 16 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,22 @@ func (c *cache) get(k string) (interface{}, bool) {
return item.Object, true
}

// GetAll returns all keys in the cache or empty map.
func (c *cache) GetAll() map[string]interface{} {
c.mu.RLock()
defer c.mu.RUnlock()
var items map[string]interface{}

if c.ItemCount() > 0 {
items = make(map[string]interface{}, len(c.items))
for k, v := range c.items {
items[k] = v.Object
}
}

return items
}

// Delete an item from the cache. Does nothing if the key is not in the cache.
func (c *cache) Delete(k string) {
c.mu.Lock()
Expand Down
5 changes: 5 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ func TestCache(t *testing.T) {
} else if c2 := x.(float64); c2+1.2 != 4.7 {
t.Error("c2 (which should be 3.5) plus 1.2 does not equal 4.7; value:", c2)
}

z := tc.GetAll()
if len(z) != 3 {
t.Errorf("expected to receive a, b, c, received: %+v\n", z)
}
}

func TestStorePointerToStruct(t *testing.T) {
Expand Down

0 comments on commit a3441f4

Please sign in to comment.