Skip to content

Commit

Permalink
feat(cachetable): add expiresBycreatedOn options to cachetable
Browse files Browse the repository at this point in the history
"feature muesli#45"
  • Loading branch information
刘亮 committed Nov 6, 2020
1 parent a100c5a commit c209528
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions cachetable.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type CacheTable struct {

// The logger used for this table.
logger *log.Logger
// if check expires by createdOn
expiresBycreatedOn bool

// Callback method triggered when trying to load a non-existing key.
loadData func(key interface{}, args ...interface{}) *CacheItem
Expand Down Expand Up @@ -122,6 +124,13 @@ func (table *CacheTable) SetLogger(logger *log.Logger) {
table.logger = logger
}

// SetExpirebycreatedOn sets if expiration Check by the createdOn
func (table *CacheTable) SetExpirebycreatedOn(expirebycreatedon bool) {
table.Lock()
defer table.Unlock()
table.expiresBycreatedOn = expirebycreatedon
}

// Expiration check loop, triggered by a self-adjusting timer.
func (table *CacheTable) expirationCheck() {
table.Lock()
Expand All @@ -143,18 +152,25 @@ func (table *CacheTable) expirationCheck() {
item.RLock()
lifeSpan := item.lifeSpan
accessedOn := item.accessedOn
createdOn := item.createdOn
item.RUnlock()

if lifeSpan == 0 {
continue
}
if now.Sub(accessedOn) >= lifeSpan {

checkfield := accessedOn
if table.expiresBycreatedOn {
checkfield = createdOn
}

if now.Sub(checkfield) >= lifeSpan {
// Item has excessed its lifespan.
table.deleteInternal(key)
} else {
// Find the item chronologically closest to its end-of-lifespan.
if smallestDuration == 0 || lifeSpan-now.Sub(accessedOn) < smallestDuration {
smallestDuration = lifeSpan - now.Sub(accessedOn)
if smallestDuration == 0 || lifeSpan-now.Sub(checkfield) < smallestDuration {
smallestDuration = lifeSpan - now.Sub(checkfield)
}
}
}
Expand Down

0 comments on commit c209528

Please sign in to comment.