feat(cache): implement TTLCache and TimeCache (#1822)

* feat(pkg): implement ttlcache and timecache
This commit is contained in:
Kyle Sanderson 2024-12-18 09:15:06 +13:00 committed by GitHub
parent acef4ac624
commit c1d8a4a850
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 742 additions and 21 deletions

41
pkg/ttlcache/domain.go Normal file
View file

@ -0,0 +1,41 @@
package ttlcache
import (
"sync"
"time"
"github.com/autobrr/autobrr/pkg/timecache"
)
const NoTTL time.Duration = 0
const DefaultTTL time.Duration = time.Nanosecond * 1
type Cache[K comparable, V any] struct {
tc timecache.Cache
l sync.RWMutex
o Options[K, V]
ch chan time.Time
m map[K]Item[V]
}
type Item[V any] struct {
t time.Time
d time.Duration
v V
}
type Options[K comparable, V any] struct {
defaultTTL time.Duration
defaultResolution time.Duration
deallocationFunc DeallocationFunc[K, V]
noUpdateTime bool
}
type DeallocationReason int
const (
ReasonTimedOut = DeallocationReason(iota)
ReasonDeleted = DeallocationReason(iota)
)
type DeallocationFunc[K comparable, V any] func(key K, value V, reason DeallocationReason)