James Tombleson
ff4075383a
* added a route to delete subscriptions based on the ID given * added a new route to find a record based on the name and source * added a route to query Discord Web Hooks by Server and Channel names * tested the endpoints and they seem good to test more * updated some routes for subscriptions and formatted files * removed debug file * fixing some panic calls * swag
48 lines
1.2 KiB
Go
48 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
|
|
}
|