From aa6d62edb266a4d9879b15220477c565b3e33e14 Mon Sep 17 00:00:00 2001 From: cy Date: Tue, 28 May 2019 22:00:38 +0800 Subject: [PATCH 1/2] using the cleanupTime to checkExpiration instead of cleanupInterval --- cachetable.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/cachetable.go b/cachetable.go index 1e2d7d3..6fc9474 100644 --- a/cachetable.go +++ b/cachetable.go @@ -26,7 +26,9 @@ type CacheTable struct { // Timer responsible for triggering cleanup. cleanupTimer *time.Timer // Current timer duration. - cleanupInterval time.Duration + //cleanupInterval time.Duration + + cleanupTime time.Time // The logger used for this table. logger *log.Logger @@ -94,11 +96,14 @@ func (table *CacheTable) expirationCheck() { if table.cleanupTimer != nil { table.cleanupTimer.Stop() } + + /* if table.cleanupInterval > 0 { table.log("Expiration check triggered after", table.cleanupInterval, "for table", table.name) } else { table.log("Expiration check installed for table", table.name) } + */ // To be more accurate with timers, we would need to update 'now' on every // loop iteration. Not sure it's really efficient though. @@ -121,12 +126,13 @@ func (table *CacheTable) expirationCheck() { // Find the item chronologically closest to its end-of-lifespan. if smallestDuration == 0 || lifeSpan-now.Sub(accessedOn) < smallestDuration { smallestDuration = lifeSpan - now.Sub(accessedOn) + table.cleanupTime = accessedOn.Add(lifeSpan) } } } // Setup the interval for the next cleanup run. - table.cleanupInterval = smallestDuration + // table.cleanupInterval = smallestDuration if smallestDuration > 0 { table.cleanupTimer = time.AfterFunc(smallestDuration, func() { go table.expirationCheck() @@ -142,7 +148,7 @@ func (table *CacheTable) addInternal(item *CacheItem) { table.items[item.key] = item // Cache values so we don't keep blocking the mutex. - expDur := table.cleanupInterval + //expDur := table.cleanupInterval addedItem := table.addedItem table.Unlock() @@ -152,7 +158,7 @@ func (table *CacheTable) addInternal(item *CacheItem) { } // If we haven't set up any expiration check timer or found a more imminent item. - if item.lifeSpan > 0 && (expDur == 0 || item.lifeSpan < expDur) { + if item.lifeSpan > 0 && (table.cleanupTime.IsZero() || time.Now().Add(item.lifeSpan).Before(table.cleanupTime)) { table.expirationCheck() } } @@ -271,7 +277,8 @@ func (table *CacheTable) Flush() { table.log("Flushing table", table.name) table.items = make(map[interface{}]*CacheItem) - table.cleanupInterval = 0 + //table.cleanupInterval = 0 + table.cleanupTime = time.Time{} if table.cleanupTimer != nil { table.cleanupTimer.Stop() } From a9579948591e9b60aaa20da97e2d02d92fdbcf00 Mon Sep 17 00:00:00 2001 From: cy Date: Tue, 28 May 2019 23:58:39 +0800 Subject: [PATCH 2/2] using the cleanupTime in stead of cleanupInterval --- cachetable.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cachetable.go b/cachetable.go index 6fc9474..b1c04cd 100644 --- a/cachetable.go +++ b/cachetable.go @@ -149,6 +149,7 @@ func (table *CacheTable) addInternal(item *CacheItem) { // Cache values so we don't keep blocking the mutex. //expDur := table.cleanupInterval + cleanupTime := table.cleanupTime addedItem := table.addedItem table.Unlock() @@ -158,7 +159,7 @@ func (table *CacheTable) addInternal(item *CacheItem) { } // If we haven't set up any expiration check timer or found a more imminent item. - if item.lifeSpan > 0 && (table.cleanupTime.IsZero() || time.Now().Add(item.lifeSpan).Before(table.cleanupTime)) { + if item.lifeSpan > 0 && (cleanupTime.IsZero() || time.Now().Add(item.lifeSpan).Before(cleanupTime)) { table.expirationCheck() } }