All checks were successful
Cadoles/bouncer/pipeline/head This commit looks good
64 lines
1.1 KiB
Go
64 lines
1.1 KiB
Go
package syncx
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/cache"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type RefreshFunc[K comparable, V any] func(ctx context.Context, key K) (V, error)
|
|
|
|
type CachedResource[K comparable, V any] struct {
|
|
cache cache.Cache[K, V]
|
|
lock sync.RWMutex
|
|
refresh RefreshFunc[K, V]
|
|
}
|
|
|
|
func (r *CachedResource[K, V]) Clear() {
|
|
r.cache.Clear()
|
|
}
|
|
|
|
func (r *CachedResource[K, V]) Get(ctx context.Context, key K) (V, bool, error) {
|
|
value, exists := r.cache.Get(key)
|
|
if exists {
|
|
return value, false, nil
|
|
}
|
|
|
|
locked := r.lock.TryLock()
|
|
if !locked {
|
|
r.lock.RLock()
|
|
|
|
value, exists := r.cache.Get(key)
|
|
if exists {
|
|
r.lock.RUnlock()
|
|
return value, false, nil
|
|
}
|
|
|
|
r.lock.RUnlock()
|
|
}
|
|
|
|
if !locked {
|
|
r.lock.Lock()
|
|
}
|
|
|
|
defer r.lock.Unlock()
|
|
|
|
value, err := r.refresh(ctx, key)
|
|
if err != nil {
|
|
return *new(V), false, errors.WithStack(err)
|
|
}
|
|
|
|
r.cache.Set(key, value)
|
|
|
|
return value, true, nil
|
|
}
|
|
|
|
func NewCachedResource[K comparable, V any](cache cache.Cache[K, V], refresh RefreshFunc[K, V]) *CachedResource[K, V] {
|
|
return &CachedResource[K, V]{
|
|
cache: cache,
|
|
refresh: refresh,
|
|
}
|
|
}
|