From e5f66a8f4bf74efa5b260d72a425cbb6fe8728fb Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Mon, 28 Apr 2014 16:37:39 +0300 Subject: [PATCH] only delete if cache key exists --- cache2go/cache.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/cache2go/cache.go b/cache2go/cache.go index 294d21fba..41b2adace 100644 --- a/cache2go/cache.go +++ b/cache2go/cache.go @@ -51,8 +51,11 @@ func (xe *XEntry) XCache(key string, expire time.Duration, value expiringCacheEn xe.expireDuration = expire xe.timestamp = time.Now() xMux.Lock() + if _, ok := xcache[key]; !ok { + // only count if the key is not already there + count(key) + } xcache[key] = value - count(key) xMux.Unlock() go xe.expire() } @@ -108,8 +111,11 @@ func GetXCached(key string) (ece expiringCacheEntry, err error) { func Cache(key string, value interface{}) { mux.Lock() defer mux.Unlock() + if _, ok := cache[key]; !ok { + // only count if the key is not already there + count(key) + } cache[key] = timestampedValue{time.Now(), value} - count(key) } // The function to extract a value for a key that never expire