newsbot-api/internal/services/cache/monitor.go

48 lines
1.2 KiB
Go

package cache
import (
"time"
"git.jamestombleson.com/jtom38/newsbot-api/internal/domain"
)
// When a record becomes tainted, it needs to be renewed or it will be dropped from the cache.
// If a record is tainted and used again, the taint will be removed and a new Expires value will be set.
// If its not renewed, it will be dropped.
type CacheAgeMonitor struct{}
func NewCacheAgeMonitor() CacheAgeMonitor {
return CacheAgeMonitor{}
}
// This is an automated job that will review all the objects for age and taint them if needed.
func (cam CacheAgeMonitor) CheckExpiredEntries() {
now := time.Now()
for index, item := range cacheStorage {
if now.After(item.Expires) {
// the timer expired, and its not tainted, taint it
if !item.IsTainted {
item.IsTainted = true
item.Expires = now.Add(1 * time.Hour)
}
// if its tainted and the timer didnt get renewed, delete
if item.IsTainted {
cacheStorage = cam.removeEntry(index)
}
}
}
}
// This creates a new slice and skips over the item that needs to be dropped
func (cam CacheAgeMonitor) removeEntry(index int) []*domain.CacheItem {
var temp []*domain.CacheItem
for i, item := range cacheStorage {
if i != index {
temp = append(temp, item)
}
}
return temp
}