Replace timer-based SMA with a timer-less implementation

This commit is contained in:
Ingo Oppermann
2024-10-23 11:08:13 +02:00
parent 2dda47b81f
commit df30a6b8e3
17 changed files with 543 additions and 371 deletions

View File

@@ -4,38 +4,30 @@ import (
"errors"
"sync"
"time"
timesrc "github.com/datarhei/core/v16/time"
)
type TimeSource interface {
Now() time.Time
}
type StdTimeSource struct{}
func (s *StdTimeSource) Now() time.Time {
return time.Now()
}
type CacheEntry[T any] struct {
value T
validUntil time.Time
}
type Cache[T any] struct {
ts TimeSource
ts timesrc.Source
lock sync.Mutex
entries map[string]CacheEntry[T]
lastPurge time.Time
}
func NewCache[T any](ts TimeSource) *Cache[T] {
func NewCache[T any](ts timesrc.Source) *Cache[T] {
c := &Cache[T]{
ts: ts,
entries: map[string]CacheEntry[T]{},
}
if c.ts == nil {
c.ts = &StdTimeSource{}
c.ts = &timesrc.StdSource{}
}
c.lastPurge = c.ts.Now()