Documentation
¶
Index ¶
- func GetEx[T any](ctx context.Context, c ExpirableCache[T], key string, builder FetchCached[T], ...) (T, bool, error)
- func GetJson[T any](ctx context.Context, c ExpirableByteCache, key string) (T, bool, error)
- func GetJsonEx[T any](ctx context.Context, c ExpirableByteCache, key string, builder FetchCached[T], ...) (T, bool, error)
- func GetObject[T any, D codec.Decoder](ctx context.Context, c ExpirableByteCache, d D, key string) (T, bool, error)
- func GetObjectEx[T any, D codec.Decoder, E codec.Encoder](ctx context.Context, c ExpirableByteCache, d D, e E, key string, ...) (T, bool, error)
- func IsZero[T any](t T) bool
- func Set[T any](ctx context.Context, c ExpirableCache[T], key string, value T, ...) error
- func SetJson[T any](ctx context.Context, c ExpirableByteCache, key string, value T, ...) error
- func SetObject[T any, E codec.Encoder](ctx context.Context, c ExpirableByteCache, e E, key string, value T, ...) error
- type Bulk
- type ByteCache
- type Cache
- type Core
- type Evictor
- type ExpirableByteCache
- type ExpirableCache
- type FetchCached
- type Option
- type TTL
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetEx ¶
func GetEx[T any](ctx context.Context, c ExpirableCache[T], key string, builder FetchCached[T], options ...Option) (T, bool, error)
GetEx retrieves an object from the cache using the provided key. And returns the object, a boolean indicating if it was found, and an error if any occurred. If the object is not found, it uses the builder function to create the object. When the builder returns an error, the cache will not be set. and found will be false.
func GetJson ¶
GetJson retrieves and decodes a JSON-encoded object from a byte cache. This is a convenience function that uses JSON unmarshalling for deserialization.
func GetJsonEx ¶
func GetJsonEx[T any](ctx context.Context, c ExpirableByteCache, key string, builder FetchCached[T], options ...Option) (T, bool, error)
GetJsonEx retrieves a JSON object from the cache using the provided key. Similar to GetObjectEx, but specifically for JSON data with automatic encoding/decoding. If the object is not found in cache, it uses the builder function to create it and caches the result as JSON.
func GetObject ¶
func GetObject[T any, D codec.Decoder](ctx context.Context, c ExpirableByteCache, d D, key string) (T, bool, error)
GetObject retrieves and decodes a typed object from a byte cache using the provided decoder. Returns the decoded object, whether it was found, and any error that occurred during retrieval or decoding.
func GetObjectEx ¶
func GetObjectEx[T any, D codec.Decoder, E codec.Encoder](ctx context.Context, c ExpirableByteCache, d D, e E, key string, builder FetchCached[T], options ...Option) (T, bool, error)
GetObjectEx retrieves an object from the cache using the provided key. Similar to GetEx, but for byte cache with encoding/decoding support. If the object is not found in cache, it uses the builder function to create it and caches the result.
func Set ¶
func Set[T any](ctx context.Context, c ExpirableCache[T], key string, value T, options ...Option) error
Set stores a value in the cache with optional configuration such as TTL. It applies the provided options to determine cache behavior like expiration and dynamic TTL calculation.
func SetJson ¶
func SetJson[T any](ctx context.Context, c ExpirableByteCache, key string, value T, options ...Option) error
SetJson stores a typed object in a byte cache by encoding it as JSON. This is a convenience function that uses JSON marshaling for serialization.
func SetObject ¶
func SetObject[T any, E codec.Encoder](ctx context.Context, c ExpirableByteCache, e E, key string, value T, options ...Option) error
SetObject stores a typed object in a byte cache by encoding it with the provided encoder. This is useful for storing structured data that needs to be serialized before caching.
Types ¶
type Bulk ¶
type Bulk[S any] interface { // MultiSet stores multiple key-value pairs in the cache without expiration. MultiSet(ctx context.Context, valMap map[string]S) error // MultiGet retrieves multiple values from the cache by their keys, returning a map of found key-value pairs. MultiGet(ctx context.Context, keys []string) (map[string]S, error) // MultiDel removes multiple keys from the cache. MultiDel(ctx context.Context, keys []string) error }
Bulk provides methods for batch operations on multiple key-value pairs.
type ByteCache ¶
ByteCache is a specialized cache for storing byte slices, commonly used for serialized data or raw content.
type Cache ¶
Cache defines a generic caching interface that provides CRUD operations for storing and retrieving typed values. The interface supports both simple operations and batch operations with optional TTL (Time To Live) functionality. S represents the type of values that can be stored in the cache.
type Core ¶
type Core[S any] interface { // Set stores a key-value pair in the cache without expiration. Set(ctx context.Context, key string, val S) error // Get retrieves a value from the cache by key, returning the value, whether it was found, and any error. Get(ctx context.Context, key string) (S, bool, error) // Del removes a single key from the cache. Del(ctx context.Context, key string) error // Exists checks whether a key exists in the cache. Exists(ctx context.Context, key string) (bool, error) }
Core provides basic methods for single key-value operations in the cache.
type Evictor ¶
type Evictor interface {
// DelAll removes all keys from the cache.
DelAll(ctx context.Context) error
}
Evictor provides a method to clear all entries from the cache.
type ExpirableByteCache ¶
type ExpirableByteCache = ExpirableCache[[]byte]
ExpirableByteCache is a specialized expirable cache for storing byte slices with TTL support.
type ExpirableCache ¶
ExpirableCache combines core cache operations with TTL functionality, allowing for expirable cache entries.
type FetchCached ¶
FetchCached is a function type that defines a builder for fetching cached objects. It should return the object of type T and an error if any occurs during the fetching process. If the error is nil, it indicates that the object was successfully fetched or built. So a cache can be set. If the object is not found or cannot be built, it should return a zero value of type T and an error. Then the cache will not be set.
type Option ¶
type Option func(o *options)
Option defines a functional option for configuring cache behavior.
func WithDynamicTTL ¶
WithDynamicTTL allows setting a dynamic TTL based on the value type T. The calculator function should return a boolean indicating whether the TTL is set and the duration for which the value should be cached.
func WithExpiration ¶
WithExpiration sets a fixed expiration duration for cache entries.
func WithNeverExpire ¶
func WithNeverExpire() Option
WithNeverExpire configures the cache to never expire entries automatically.
func WithSingleflight ¶
func WithSingleflight(single *singleflight.Group) Option
WithSingleflight enables singleflight to prevent duplicate concurrent cache loads for the same key. This helps reduce redundant work when multiple goroutines request the same uncached data simultaneously.
type TTL ¶
type TTL[S any] interface { // SetWithTTL stores a key-value pair in the cache with a specified expiration duration. SetWithTTL(ctx context.Context, key string, val S, expiration time.Duration) error // MultiSetWithTTL stores multiple key-value pairs in the cache with a specified expiration duration. MultiSetWithTTL(ctx context.Context, valMap map[string]S, expiration time.Duration) error }
TTL provides methods to set cache entries with a specified Time To Live (TTL).