mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 00:39: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
74
pkg/timecache/timecache.go
Normal file
74
pkg/timecache/timecache.go
Normal file
|
@ -0,0 +1,74 @@
|
|||
package timecache
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Cache struct {
|
||||
m sync.RWMutex
|
||||
t time.Time
|
||||
o Options
|
||||
}
|
||||
|
||||
type Options struct {
|
||||
round time.Duration
|
||||
}
|
||||
|
||||
func New(o Options) *Cache {
|
||||
c := Cache{
|
||||
o: o,
|
||||
}
|
||||
|
||||
return &c
|
||||
}
|
||||
|
||||
func (t *Cache) Now() time.Time {
|
||||
t.m.RLock()
|
||||
if !t.t.IsZero() {
|
||||
defer t.m.RUnlock()
|
||||
return t.t
|
||||
}
|
||||
|
||||
t.m.RUnlock()
|
||||
return t.update()
|
||||
}
|
||||
|
||||
func (t *Cache) update() time.Time {
|
||||
t.m.Lock()
|
||||
defer t.m.Unlock()
|
||||
if !t.t.IsZero() {
|
||||
return t.t
|
||||
}
|
||||
|
||||
var d time.Duration
|
||||
if t.o.round > time.Nanosecond {
|
||||
d = t.o.round
|
||||
} else {
|
||||
d = time.Second * 1
|
||||
}
|
||||
|
||||
t.t = time.Now().Round(d)
|
||||
|
||||
go func(duration time.Duration) {
|
||||
if t.o.round > time.Nanosecond {
|
||||
duration = t.o.round / 2
|
||||
}
|
||||
|
||||
time.Sleep(duration)
|
||||
t.reset()
|
||||
}(d)
|
||||
|
||||
return t.t
|
||||
}
|
||||
|
||||
func (t *Cache) reset() {
|
||||
t.m.Lock()
|
||||
defer t.m.Unlock()
|
||||
t.t = time.Time{}
|
||||
}
|
||||
|
||||
func (o Options) Round(d time.Duration) Options {
|
||||
o.round = d
|
||||
return o
|
||||
}
|
49
pkg/timecache/timecache_test.go
Normal file
49
pkg/timecache/timecache_test.go
Normal file
|
@ -0,0 +1,49 @@
|
|||
package timecache
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestTime(t *testing.T) {
|
||||
t.Parallel()
|
||||
tc := (&Cache{}).Now()
|
||||
if tc.IsZero() {
|
||||
t.Fatalf("time is zero")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRounding(t *testing.T) {
|
||||
t.Parallel()
|
||||
ti := New(Options{}.Round(time.Minute * 5)).Now()
|
||||
|
||||
if ti.Minute()%5 != 0 {
|
||||
t.Fatalf("time is not a 5 multiple")
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolution(t *testing.T) {
|
||||
t.Parallel()
|
||||
const magicNumber = 3
|
||||
const rounds = 700
|
||||
ti := New(Options{}.Round(time.Millisecond * magicNumber))
|
||||
|
||||
unique := 0
|
||||
old := ti.Now().UnixMilli()
|
||||
for i := 0; i < rounds; i++ {
|
||||
new := ti.Now().UnixMilli()
|
||||
if new > old {
|
||||
unique++
|
||||
old = new
|
||||
}
|
||||
|
||||
if div := new % magicNumber; div != 0 {
|
||||
t.Fatalf("not a multiple of %d: %d", magicNumber, div)
|
||||
}
|
||||
time.Sleep(time.Millisecond * 1)
|
||||
}
|
||||
|
||||
if unique < rounds/magicNumber-1 {
|
||||
t.Fatalf("not enough resolution rounds %d", unique)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue