From 0dcaa59db854bb8f91445a3b1d09e6b0c341729c Mon Sep 17 00:00:00 2001 From: youjianglong Date: Wed, 28 Mar 2018 15:22:17 +0800 Subject: [PATCH] Add Iterate --- cache.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/cache.go b/cache.go index db88d2f..72ccf93 100644 --- a/cache.go +++ b/cache.go @@ -165,6 +165,37 @@ func (c *cache) GetWithExpiration(k string) (interface{}, time.Time, bool) { return item.Object, time.Time{}, true } +// Iterate every item by item handle items from cache,and if the handle returns to false, +// it will be interrupted and return false. +func (c *cache) Iterate(f func(key string, item Item) bool) bool { + now := time.Now().UnixNano() + c.mu.RLock() + keys := make([]string, len(c.items)) + i := 0 + for k, v := range c.items { + // "Inlining" of Expired + if v.Expiration > 0 && now > v.Expiration { + continue + } + keys[i] = k + i++ + } + c.mu.RUnlock() + keys = keys[:i] + for _, key := range keys { + c.mu.RLock() + item, ok := c.items[key] + c.mu.RUnlock() + if !ok { + continue + } + if !f(key, item) { + return false + } + } + return true +} + func (c *cache) get(k string) (interface{}, bool) { item, found := c.items[k] if !found {