James Tombleson
11892b9a7b
* starting the ffxiv reader * working on getting the standard interface for sources based on the work for ffxiv * got more of ffxiv working and updated tests * Author and Description can be extracted and validated with tests * added uuid package * ffxiv core logic is working and testes updated to reflect it. * Updated the scheduler with the current sources and moved them from main * updated reddit to allow modern go to talk to the endpoint with a debug flag * gave the func a better name * cleaned up main * Moved cache to its own package and updated tests" * moved config to its own package and added basic tests * updated imports * minor update" * interface update and cache model update * updated the scheduler for basic services. No DB calls yet * updated db calls * bypassed the reddit test as its flaky in github
46 lines
1.2 KiB
Go
46 lines
1.2 KiB
Go
package cache
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/jtom38/newsbot/collector/domain/model"
|
|
)
|
|
|
|
// 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) []*model.CacheItem {
|
|
var temp []*model.CacheItem
|
|
for i, item := range cacheStorage {
|
|
if i != index { temp = append(temp, item )}
|
|
}
|
|
return temp
|
|
}
|