Documentation
¶
Index ¶
- Variables
- func EncodeStorageKey[K comparable](key K, encoder KeyEncoder[K]) (string, error)
- func GetOrFetch[K comparable, V any](ctx context.Context, cache Cache[K, V], key K, ttl time.Duration, ...) (V, error)
- func GetOrFetchWithOptions[K comparable, V any](ctx context.Context, cache Cache[K, V], key K, opts GetOrFetchOptions, ...) (V, error)
- func Observe(ctx context.Context, observer Observer, event Observation)
- type BatchInvalidator
- type Cache
- type Clearable
- type GetOrFetchOptions
- type KeyEncoder
- type KeyEncoderFunc
- type Observation
- type Observer
- type ObserverFunc
- type Operation
- type PrefixInvalidatordeprecated
- type Purgeable
- type ReadErrorMode
- type SetIfAbsentCache
- type SetIfPresentCache
- type SingleflightGroup
- type TagRegistrydeprecated
- type TypedPrefixInvalidator
- type TypedTagRegistry
- type WriteErrorMode
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNilFetch is returned when a nil fetch callback is supplied. ErrNilFetch = errors.New("gocache: nil fetch function") // ErrInvalidSingleflightValue is returned when a singleflight result cannot be type asserted to V. ErrInvalidSingleflightValue = errors.New("gocache: invalid singleflight value type") )
var ( // ErrNilKeyEncoderFunc indicates a nil KeyEncoderFunc was used. ErrNilKeyEncoderFunc = errors.New("gocache: nil key encoder func") // ErrKeyEncoderRequired indicates non-string key types must provide a key encoder. ErrKeyEncoderRequired = errors.New("gocache: key encoder required for non-string key type") // ErrInvalidStringKeyType indicates a default string encoder received a non-string value. ErrInvalidStringKeyType = errors.New("gocache: key is not string-compatible") )
var ( // ErrLogicalPrefixUnsupported indicates logical-prefix invalidation was requested on a non-string key type. ErrLogicalPrefixUnsupported = errors.New("gocache: logical prefix invalidation unsupported for non-string key type") )
Functions ¶
func EncodeStorageKey ¶
func EncodeStorageKey[K comparable](key K, encoder KeyEncoder[K]) (string, error)
EncodeStorageKey encodes a key using the supplied encoder or default string path.
func GetOrFetch ¶
func GetOrFetch[K comparable, V any]( ctx context.Context, cache Cache[K, V], key K, ttl time.Duration, fetch func(context.Context) (V, error), ) (V, error)
GetOrFetch returns a cached value or fetches and stores it with the given TTL. TTL <= 0 is treated as non-expiring by compliant cache backends.
func GetOrFetchWithOptions ¶
func GetOrFetchWithOptions[K comparable, V any]( ctx context.Context, cache Cache[K, V], key K, opts GetOrFetchOptions, fetch func(context.Context) (V, error), ) (V, error)
GetOrFetchWithOptions is the configurable read-through helper.
Types ¶
type BatchInvalidator ¶
type BatchInvalidator[K comparable] interface { InvalidateKeys(ctx context.Context, keys []K) error }
BatchInvalidator is an optional capability for invalidating multiple keys at once.
type Cache ¶
type Cache[K comparable, V any] interface { Get(ctx context.Context, key K) (V, bool, error) Set(ctx context.Context, key K, value V, ttl time.Duration) error Delete(ctx context.Context, key K) error }
Cache is the minimal cross-package key/value cache contract. Implementations may be in-memory or distributed.
type GetOrFetchOptions ¶
type GetOrFetchOptions struct {
TTL time.Duration
ReadErrorMode ReadErrorMode
WriteErrorMode WriteErrorMode
Group SingleflightGroup
GroupKey string
}
GetOrFetchOptions configures read-through behavior.
type KeyEncoder ¶
type KeyEncoder[K comparable] interface { EncodeKey(K) (string, error) }
KeyEncoder encodes cache keys into deterministic storage-safe strings.
func NewStringKeyEncoder ¶
func NewStringKeyEncoder[K ~string]() KeyEncoder[K]
NewStringKeyEncoder returns a key encoder for string-typed keys.
func ResolveKeyEncoder ¶
func ResolveKeyEncoder[K comparable](encoder KeyEncoder[K]) (KeyEncoder[K], error)
ResolveKeyEncoder resolves a key encoder for type K. If encoder is nil and K is string-compatible, a default string encoder is used.
type KeyEncoderFunc ¶
type KeyEncoderFunc[K comparable] func(K) (string, error)
KeyEncoderFunc adapts a function to KeyEncoder.
func (KeyEncoderFunc[K]) EncodeKey ¶
func (f KeyEncoderFunc[K]) EncodeKey(key K) (string, error)
type Observation ¶
type Observation struct {
Backend string
Operation Operation
Key string
Err error
Latency time.Duration
// Count is the number of entries affected by an aggregate operation.
Count int
// Occupancy and Capacity report bounded backend usage without exposing keys
// or values. Unbounded or unsupported backends leave them at zero.
Occupancy int
Capacity int
}
Observation represents a backend instrumentation event.
type Observer ¶
type Observer interface {
Observe(ctx context.Context, event Observation)
}
Observer consumes cache instrumentation events.
func EnsureObserver ¶
EnsureObserver returns observer when non-nil, otherwise a no-op observer.
type ObserverFunc ¶
type ObserverFunc func(context.Context, Observation)
ObserverFunc adapts a function to Observer.
func (ObserverFunc) Observe ¶
func (f ObserverFunc) Observe(ctx context.Context, event Observation)
type Operation ¶
type Operation string
Operation identifies a cache operation for instrumentation events.
type PrefixInvalidator
deprecated
PrefixInvalidator is an optional key-prefix invalidation capability.
Deprecated: prefer TypedPrefixInvalidator[K] so prefix invalidation is keyed by the logical key type. This legacy interface historically encouraged encoded-key usage and is retained for compatibility only.
type Purgeable ¶
Purgeable is an optional capability for stores that can actively evict expired entries.
type ReadErrorMode ¶
type ReadErrorMode uint8
ReadErrorMode controls behavior when cache read operations fail. TODO: prefer string enums so they are readable and easier to understand in traces/logs
const ( // ReadErrorFail returns cache read errors to callers. ReadErrorFail ReadErrorMode = iota // ReadErrorBypass ignores cache read errors and proceeds with fetch. ReadErrorBypass )
type SetIfAbsentCache ¶
type SetIfAbsentCache[K comparable, V any] interface { SetIfAbsent(ctx context.Context, key K, value V, ttl time.Duration) (bool, error) }
SetIfAbsentCache is an optional atomic-set capability.
type SetIfPresentCache ¶
type SetIfPresentCache[K comparable, V any] interface { SetIfPresent(ctx context.Context, key K, value V, ttl time.Duration) (bool, error) }
SetIfPresentCache is an optional atomic-update capability.
SetIfPresent replaces a live entry and resets its TTL without creating a missing or expired entry. It returns true only when the replacement was applied. A non-positive TTL makes the replacement non-expiring, matching Cache.Set semantics. Implementations must leave existing logical-key and tag metadata intact and must honor context cancellation and encoding failures without partially replacing the entry.
type SingleflightGroup ¶
SingleflightGroup is compatible with golang.org/x/sync/singleflight.Group. The interface keeps go-cache decoupled from that dependency.
type TagRegistry
deprecated
type TagRegistry interface {
AddTags(ctx context.Context, key string, tags []string) error
InvalidateTags(ctx context.Context, tags []string) error
}
TagRegistry is an optional capability for tag-based invalidation.
Deprecated: prefer TypedTagRegistry[K] so tag registration can be done with typed logical keys. This legacy interface uses string keys and is retained for compatibility only.
type TypedPrefixInvalidator ¶
type TypedPrefixInvalidator[K comparable] interface { DeleteByKeyPrefix(ctx context.Context, prefix K) error }
TypedPrefixInvalidator is an optional logical key-prefix invalidation capability. For string-like key types, implementations should treat prefix as logical-key prefix (not storage-key prefix).
type TypedTagRegistry ¶
type TypedTagRegistry[K comparable] interface { AddTagsForKey(ctx context.Context, key K, tags []string) error InvalidateTags(ctx context.Context, tags []string) error }
TypedTagRegistry is an optional capability for tag-based invalidation keyed by logical typed keys.
type WriteErrorMode ¶
type WriteErrorMode uint8
WriteErrorMode controls behavior when cache write operations fail. TODO: prefer string enums so they are readable and easier to understand in traces/logs
const ( // WriteErrorFail returns cache write errors to callers. WriteErrorFail WriteErrorMode = iota // WriteErrorIgnore ignores cache write errors after successful fetch. WriteErrorIgnore )