feat(*): initial commit
build / Go-Build (push) Successful in 1m1s

This commit is contained in:
2026-06-24 00:16:21 +02:00
parent 3cf9e6f266
commit 85e4df52a5
20 changed files with 1125 additions and 3 deletions
+45
View File
@@ -0,0 +1,45 @@
package internal
import (
"sync"
"time"
)
type CacheItem[T any] struct {
item T
imutex sync.Mutex
expiry time.Time
TTLSeconds uint
FetchFunc func() (T, error)
}
func (c *CacheItem[T]) Get() (T, error) {
c.imutex.Lock()
defer c.imutex.Unlock()
if !c.expiry.After(time.Now()) {
c.expiry = time.Now().Add(time.Duration(c.TTLSeconds) * time.Second)
item, err := c.FetchFunc()
if err != nil {
return item, err
}
c.item = item
}
return c.item, nil
}
func (c *CacheItem[T]) Invalidate() {
c.imutex.Lock()
defer c.imutex.Unlock()
c.expiry = time.Now()
}
func NewCache[T any](fetchfunc func() (T, error), ttl uint) CacheItem[T] {
return CacheItem[T]{
TTLSeconds: ttl,
FetchFunc: fetchfunc,
}
}