mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +00:00
feat(cache): implement TTLCache and TimeCache (#1822)
* feat(pkg): implement ttlcache and timecache
This commit is contained in:
parent
acef4ac624
commit
c1d8a4a850
8 changed files with 742 additions and 21 deletions
41
pkg/ttlcache/domain.go
Normal file
41
pkg/ttlcache/domain.go
Normal 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)
|
Loading…
Add table
Add a link
Reference in a new issue