Documentation
¶
Overview ¶
Package lastcache implements an in-memory cache with stale-if-error and stale-while-revalidate strategies, plus per-key single-flight so a burst of concurrent requests for the same key triggers at most one fetch.
stale-if-error (Get / GetStale) When a fetch fails and a previous value is still around, the cache serves that stale value for up to Config.StaleTTL instead of returning the error. stale-while-revalidate (GetAsync) An expired value is returned immediately while a single background goroutine refreshes it.
Index ¶
- type Cache
- func (c *Cache[K, V]) Delete(key K)
- func (c *Cache[K, V]) Get(ctx context.Context, key K, fetch Fetch[K, V]) (V, error)
- func (c *Cache[K, V]) GetAsync(ctx context.Context, key K, fetch Fetch[K, V]) Result[V]
- func (c *Cache[K, V]) GetStale(ctx context.Context, key K, fetch Fetch[K, V]) (Result[V], error)
- func (c *Cache[K, V]) Range(f func(key K, value V, ttl time.Duration) bool)
- func (c *Cache[K, V]) Set(key K, value V)
- func (c *Cache[K, V]) TTL(key K) time.Duration
- type Config
- type Fetch
- type Result
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[K comparable, V any] struct { // contains filtered or unexported fields }
Cache is a generic, concurrency-safe cache. Use New to construct one; it must not be copied after first use.
func New ¶
func New[K comparable, V any](config Config) *Cache[K, V]
New returns a new Cache. A zero Config is valid.
func (*Cache[K, V]) Get ¶
Get returns the value for key, fetching it if missing or expired. On a fetch error it transparently serves a stale value when one is available and Config.StaleTTL > 0; otherwise it returns the error. Concurrent calls for the same key share a single fetch.
func (*Cache[K, V]) GetAsync ¶
GetAsync returns the current value immediately. If it is expired, the stale value is returned (Result.Stale == true) and a single background goroutine refreshes the key. On a cold miss the fetch runs synchronously; if it fails, Result.Err is set. Background refresh errors are reported via Config.OnError.
func (*Cache[K, V]) GetStale ¶
GetStale is like Get but reports whether the value was served stale via the returned Result. The error is non-nil only when nothing could be served.
func (*Cache[K, V]) Range ¶
Range calls f for each key with its value and remaining TTL. Iteration stops if f returns false. It follows sync.Map.Range semantics (no consistent snapshot).
type Config ¶
type Config struct {
// TTL is how long a fetched value stays fresh. Values <= 0 use defaultTTL.
TTL time.Duration
// StaleTTL is how long a stale value may be served after expiry when a
// refresh fails (stale-if-error). 0 disables serving stale: every expired
// Get then re-runs the fetch until it succeeds.
StaleTTL time.Duration
// MaxConcurrentRefresh bounds the number of background refreshes running at
// once across all keys (GetAsync). Values <= 0 use 1.
MaxConcurrentRefresh int
// OnError, if set, is called with the underlying error when a background
// refresh (GetAsync) fails. Foreground errors are returned to the caller.
OnError func(key any, err error)
// Context is the base context used for background refreshes, which outlive
// the request that triggered them. Defaults to context.Background().
Context context.Context
}
Config configures a Cache. The zero value is valid and uses defaults.
type Fetch ¶
type Fetch[K comparable, V any] func(ctx context.Context, key K) (V, error)
Fetch retrieves the value for a key. It is called by the cache on a miss or when a value has expired.
type Result ¶
type Result[V any] struct { // Value is the cached or freshly fetched value. It is the zero value of V // when Err is set and nothing could be served. Value V // Stale is true when Value is an expired value served because a refresh // failed (GetStale) or is still in progress (GetAsync). Stale bool // Err holds the underlying fetch error when a stale value was served, or // the fetch error on a cold miss. It is nil for fresh values. Err error }
Result carries a value plus whether it was served stale.