Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions cachetable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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()
Expand All @@ -142,7 +148,8 @@ 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
cleanupTime := table.cleanupTime
addedItem := table.addedItem
table.Unlock()

Expand All @@ -152,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 && (expDur == 0 || item.lifeSpan < expDur) {
if item.lifeSpan > 0 && (cleanupTime.IsZero() || time.Now().Add(item.lifeSpan).Before(cleanupTime)) {
table.expirationCheck()
}
}
Expand Down Expand Up @@ -271,7 +278,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()
}
Expand Down