newsbot-api/services/cache/monitor.go
James Tombleson ada453e08a
Features/delete source and first dto (#36)
* updated db, added dto for ListSources, and added delete source

* updated from model > models

* updated to models

* sources now sends back a standard message

* updated subscription routes to have beter logid and swagger details

* moved the dto objects back to modles given they are not bound to the database

* cleaned up how we return the error

* cleaned up swag and updated models to take from the base apistatusmodel. less human errors this way

* cleaned up swag and updated models

* swag updated

* updated queue to return a router and also renamed it as it will hold all queue info later on

* removed config tag

* added subscription details route

* article routes have been moved to support dto

* updated discordwebhooks to use dto

* updated discordwebhookqueue to return details on the items via dto

* removed the example routes

* updated sources to use dto

* subscriptions moved to dto

* generated swag
2023-01-22 10:12:55 -08:00

48 lines
1.2 KiB
Go

package cache
import (
"time"
"github.com/jtom38/newsbot/collector/domain/models"
)
// 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) []*models.CacheItem {
var temp []*models.CacheItem
for i, item := range cacheStorage {
if i != index {
temp = append(temp, item)
}
}
return temp
}