From a2d8b56f0c21c5562c23717b6137ed6254ddc6d4 Mon Sep 17 00:00:00 2001 From: Patrick Mylund Nielsen Date: Fri, 25 Nov 2016 13:56:11 -0500 Subject: [PATCH 1/4] Make Items() return a copy rather than an unsynchronized reference to the underlying items map --- cache.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/cache.go b/cache.go index 3562543..4eab3d0 100644 --- a/cache.go +++ b/cache.go @@ -998,19 +998,25 @@ func (c *cache) LoadFile(fname string) error { return fp.Close() } -// Returns the items in the cache. This may include items that have expired, -// but have not yet been cleaned up. If this is significant, the Expiration -// fields of the items should be checked. Note that explicit synchronization -// is needed to use a cache and its corresponding Items() return value at -// the same time, as the map is shared. +// Copies all unexpired items in the cache into a new map and returns it. func (c *cache) Items() map[string]Item { c.mu.RLock() defer c.mu.RUnlock() - return c.items + m := make(map[string]Item, len(c.items)) + now := time.Now().UnixNano() + for k, v := range c.items { + if v.Expiration > 0 { + if now > v.Expiration { + continue + } + } + m[k] = v + } + return m } // Returns the number of items in the cache. This may include items that have -// expired, but have not yet been cleaned up. Equivalent to len(c.Items()). +// expired, but have not yet been cleaned up. func (c *cache) ItemCount() int { c.mu.RLock() n := len(c.items) From 9e6d9117e747d8a136ae985ebcc46fbe4c293198 Mon Sep 17 00:00:00 2001 From: Patrick Mylund Nielsen Date: Fri, 25 Nov 2016 13:57:39 -0500 Subject: [PATCH 2/4] Add 'inlining of expired' note to Items() --- cache.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cache.go b/cache.go index 4eab3d0..8c440d4 100644 --- a/cache.go +++ b/cache.go @@ -1005,6 +1005,7 @@ func (c *cache) Items() map[string]Item { m := make(map[string]Item, len(c.items)) now := time.Now().UnixNano() for k, v := range c.items { + // "Inlining" of Expired if v.Expiration > 0 { if now > v.Expiration { continue From 52581776a332a467d34d727f700f046f693c576a Mon Sep 17 00:00:00 2001 From: Patrick Mylund Nielsen Date: Fri, 25 Nov 2016 14:18:09 -0500 Subject: [PATCH 3/4] LICENSE: Update copyright year --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 159e1e7..f9fe271 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2012-2015 Patrick Mylund Nielsen and the go-cache contributors +Copyright (c) 2012-2016 Patrick Mylund Nielsen and the go-cache contributors Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal From e7a9def80f35fe1b170b7b8b68871d59dea117e1 Mon Sep 17 00:00:00 2001 From: Patrick Mylund Nielsen Date: Fri, 25 Nov 2016 18:48:19 -0500 Subject: [PATCH 4/4] Add SetDefault() for setting with the default expiration --- cache.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cache.go b/cache.go index 8c440d4..70e4dad 100644 --- a/cache.go +++ b/cache.go @@ -81,6 +81,12 @@ func (c *cache) set(k string, x interface{}, d time.Duration) { } } +// Add an item to the cache, replacing any existing item, using the default +// expiration. +func (c *cache) SetDefault(k string, x interface{}) { + c.Set(k, x, DefaultExpiration) +} + // Add an item to the cache only if an item doesn't already exist for the given // key, or if the existing item has expired. Returns an error otherwise. func (c *cache) Add(k string, x interface{}, d time.Duration) error {