From fa4f30e5dbfd010cd9919ced93e84d0b74609615 Mon Sep 17 00:00:00 2001 From: Ralph Caraveo III Date: Tue, 16 Oct 2018 20:57:12 -0700 Subject: [PATCH 1/2] simplies logic to just forward to other methods, makes stats consistent where some weren't being fully cleared --- cache.go | 13 ++++--------- segment.go | 19 +++++++------------ 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/cache.go b/cache.go index 04049af..8397b0f 100644 --- a/cache.go +++ b/cache.go @@ -12,7 +12,7 @@ const ( // segmentCount represents the number of segments within a freecache instance. segmentCount = 256 // segmentAndOpVal is bitwise AND applied to the hashVal to find the segment id. - segmentAndOpVal = 255 + segmentAndOpVal = segmentCount - 1 minBufSize = 512 * 1024 ) @@ -57,11 +57,7 @@ func (cache *Cache) Set(key, value []byte, expireSeconds int) (err error) { // Get returns the value or not found error. func (cache *Cache) Get(key []byte) (value []byte, err error) { - hashVal := hashFunc(key) - segID := hashVal & segmentAndOpVal - cache.locks[segID].Lock() - value, _, err = cache.segments[segID].get(key, hashVal) - cache.locks[segID].Unlock() + value, _, err = cache.GetWithExpiration(key) return } @@ -104,9 +100,8 @@ func (cache *Cache) SetInt(key int64, value []byte, expireSeconds int) (err erro // GetInt returns the value for an integer within the cache or a not found error. func (cache *Cache) GetInt(key int64) (value []byte, err error) { - var bKey [8]byte - binary.LittleEndian.PutUint64(bKey[:], uint64(key)) - return cache.Get(bKey[:]) + value, _, err = cache.GetIntWithExpiration(key) + return } // GetIntWithExpiration returns the value and expiration or a not found error. diff --git a/segment.go b/segment.go index a511222..eba9b78 100644 --- a/segment.go +++ b/segment.go @@ -369,11 +369,14 @@ func (seg *segment) lookupByOff(slot []entryPtr, hash16 uint16, offset int64) (i } func (seg *segment) resetStatistics() { - atomic.StoreInt64(&seg.totalEvacuate, 0) - atomic.StoreInt64(&seg.totalExpired, 0) - atomic.StoreInt64(&seg.overwrites, 0) + atomic.StoreInt64(&seg.entryCount, 0) atomic.StoreInt64(&seg.hitCount, 0) atomic.StoreInt64(&seg.missCount, 0) + atomic.StoreInt64(&seg.overwrites, 0) + atomic.StoreInt64(&seg.totalCount, 0) + atomic.StoreInt64(&seg.totalTime, 0) + atomic.StoreInt64(&seg.totalEvacuate, 0) + atomic.StoreInt64(&seg.totalExpired, 0) } func (seg *segment) clear() { @@ -385,13 +388,5 @@ func (seg *segment) clear() { for i := 0; i < len(seg.slotLens); i++ { seg.slotLens[i] = 0 } - - atomic.StoreInt64(&seg.hitCount, 0) - atomic.StoreInt64(&seg.missCount, 0) - atomic.StoreInt64(&seg.entryCount, 0) - atomic.StoreInt64(&seg.totalCount, 0) - atomic.StoreInt64(&seg.totalTime, 0) - atomic.StoreInt64(&seg.totalEvacuate, 0) - atomic.StoreInt64(&seg.totalExpired, 0) - atomic.StoreInt64(&seg.overwrites, 0) + seg.resetStatistics() } From 7b05643c0ea2cb244b511ad58e6643f6051ad832 Mon Sep 17 00:00:00 2001 From: Ralph Caraveo III Date: Sun, 21 Oct 2018 10:16:24 -0700 Subject: [PATCH 2/2] Move totalCount and totalTime out of resetStatistics --- segment.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/segment.go b/segment.go index eba9b78..0546722 100644 --- a/segment.go +++ b/segment.go @@ -373,8 +373,6 @@ func (seg *segment) resetStatistics() { atomic.StoreInt64(&seg.hitCount, 0) atomic.StoreInt64(&seg.missCount, 0) atomic.StoreInt64(&seg.overwrites, 0) - atomic.StoreInt64(&seg.totalCount, 0) - atomic.StoreInt64(&seg.totalTime, 0) atomic.StoreInt64(&seg.totalEvacuate, 0) atomic.StoreInt64(&seg.totalExpired, 0) } @@ -388,5 +386,10 @@ func (seg *segment) clear() { for i := 0; i < len(seg.slotLens); i++ { seg.slotLens[i] = 0 } + + // These fields are not stats. + atomic.StoreInt64(&seg.totalCount, 0) + atomic.StoreInt64(&seg.totalTime, 0) + seg.resetStatistics() }