quintodrome/utils/cache/simple_cache.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

130 lines
2.9 KiB
Go
Raw Normal View History

package cache
import (
2024-06-21 13:57:43 -08:00
"errors"
"fmt"
2024-06-24 13:30:13 -08:00
"sync/atomic"
"time"
2024-06-21 13:57:43 -08:00
"github.com/jellydator/ttlcache/v3"
2024-06-24 13:30:13 -08:00
. "github.com/navidrome/navidrome/utils/gg"
)
2024-06-21 13:57:43 -08:00
type SimpleCache[K comparable, V any] interface {
Add(key K, value V) error
AddWithTTL(key K, value V, ttl time.Duration) error
Get(key K) (V, error)
GetWithLoader(key K, loader func(key K) (V, time.Duration, error)) (V, error)
Keys() []K
2024-06-24 13:30:13 -08:00
Values() []V
}
type Options struct {
2024-06-21 13:57:43 -08:00
SizeLimit uint64
DefaultTTL time.Duration
}
2024-06-21 13:57:43 -08:00
func NewSimpleCache[K comparable, V any](options ...Options) SimpleCache[K, V] {
opts := []ttlcache.Option[K, V]{
ttlcache.WithDisableTouchOnHit[K, V](),
}
if len(options) > 0 {
2024-06-21 13:57:43 -08:00
o := options[0]
if o.SizeLimit > 0 {
opts = append(opts, ttlcache.WithCapacity[K, V](o.SizeLimit))
}
if o.DefaultTTL > 0 {
opts = append(opts, ttlcache.WithTTL[K, V](o.DefaultTTL))
}
}
2024-06-21 13:57:43 -08:00
c := ttlcache.New[K, V](opts...)
return &simpleCache[K, V]{
data: c,
}
}
2024-06-24 13:30:13 -08:00
const evictionTimeout = 1 * time.Hour
2024-06-21 13:57:43 -08:00
type simpleCache[K comparable, V any] struct {
2024-06-24 13:30:13 -08:00
data *ttlcache.Cache[K, V]
evictionDeadline atomic.Pointer[time.Time]
}
2024-06-21 13:57:43 -08:00
func (c *simpleCache[K, V]) Add(key K, value V) error {
2024-06-24 13:30:13 -08:00
c.evictExpired()
2024-06-21 13:57:43 -08:00
return c.AddWithTTL(key, value, ttlcache.DefaultTTL)
}
2024-06-21 13:57:43 -08:00
func (c *simpleCache[K, V]) AddWithTTL(key K, value V, ttl time.Duration) error {
2024-06-24 13:30:13 -08:00
c.evictExpired()
2024-06-21 13:57:43 -08:00
item := c.data.Set(key, value, ttl)
if item == nil {
return errors.New("failed to add item")
}
return nil
}
2024-06-21 13:57:43 -08:00
func (c *simpleCache[K, V]) Get(key K) (V, error) {
item := c.data.Get(key)
if item == nil {
var zero V
2024-06-21 13:57:43 -08:00
return zero, errors.New("item not found")
}
2024-06-21 13:57:43 -08:00
return item.Value(), nil
}
2024-06-21 13:57:43 -08:00
func (c *simpleCache[K, V]) GetWithLoader(key K, loader func(key K) (V, time.Duration, error)) (V, error) {
var err error
2024-06-21 13:57:43 -08:00
loaderWrapper := ttlcache.LoaderFunc[K, V](
func(t *ttlcache.Cache[K, V], key K) *ttlcache.Item[K, V] {
2024-06-24 13:30:13 -08:00
c.evictExpired()
var value V
var ttl time.Duration
value, ttl, err = loader(key)
2024-06-21 13:57:43 -08:00
if err != nil {
return nil
}
return t.Set(key, value, ttl)
},
)
item := c.data.Get(key, ttlcache.WithLoader[K, V](loaderWrapper))
if item == nil {
var zero V
if err != nil {
return zero, fmt.Errorf("cache error: loader returned %w", err)
}
2024-06-21 13:57:43 -08:00
return zero, errors.New("item not found")
}
2024-06-21 13:57:43 -08:00
return item.Value(), nil
}
2024-06-24 13:30:13 -08:00
func (c *simpleCache[K, V]) evictExpired() {
if c.evictionDeadline.Load() == nil || c.evictionDeadline.Load().Before(time.Now()) {
c.data.DeleteExpired()
c.evictionDeadline.Store(P(time.Now().Add(evictionTimeout)))
}
}
2024-06-21 13:57:43 -08:00
func (c *simpleCache[K, V]) Keys() []K {
res := make([]K, 0, c.data.Len())
2024-06-24 13:30:13 -08:00
c.data.Range(func(item *ttlcache.Item[K, V]) bool {
if !item.IsExpired() {
res = append(res, item.Key())
}
return true
})
return res
}
func (c *simpleCache[K, V]) Values() []V {
res := make([]V, 0, c.data.Len())
2024-06-24 13:30:13 -08:00
c.data.Range(func(item *ttlcache.Item[K, V]) bool {
if !item.IsExpired() {
res = append(res, item.Value())
}
return true
})
return res
}