Documentation
¶
Index ¶
- func GetEx[T any](ctx context.Context, c Cache[T], key string, builder FetchCached[T], ...) (T, bool, error)
- func GetJson[T any](ctx context.Context, c ByteCache, key string, options ...Option) (T, bool, error)
- func GetJsonEx[T any](ctx context.Context, c ByteCache, key string, builder FetchCached[T], ...) (T, bool, error)
- func GetObject[T any, D codec.Decoder](ctx context.Context, c ByteCache, d D, key string) (T, bool, error)
- func GetObjectEx[T any, D codec.Decoder, E codec.Encoder](ctx context.Context, c ByteCache, d D, e E, key string, builder FetchCached[T], ...) (T, bool, error)
- func IsZero[T any](t T) bool
- func Set[T any](ctx context.Context, c Cache[T], key string, value T, options ...Option) error
- func SetJson[T any](ctx context.Context, c ByteCache, key string, value T, options ...Option) error
- func SetObject[T any, E codec.Encoder](ctx context.Context, c ByteCache, e E, key string, value T, options ...Option) error
- type ByteCache
- type Cache
- type FetchCached
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetEx ¶
func GetEx[T any](ctx context.Context, c Cache[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 GetJsonEx ¶
func GetJsonEx[T any](ctx context.Context, c ByteCache, 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.
func GetObjectEx ¶
func GetObjectEx[T any, D codec.Decoder, E codec.Encoder](ctx context.Context, c ByteCache, 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. Something like GetEx, but for ByteCache.
Types ¶
type Cache ¶
type Cache[S any] interface { Set(ctx context.Context, key string, val S) error SetWithTTL(ctx context.Context, key string, val S, expiration time.Duration) error MultiSet(ctx context.Context, valMap map[string]S) error MultiSetWithTTL(ctx context.Context, valMap map[string]S, expiration time.Duration) error Get(ctx context.Context, key string) (S, bool, error) MultiGet(ctx context.Context, keys []string) (map[string]S, error) Del(ctx context.Context, key string) error MultiDel(ctx context.Context, keys []string) error DelAll(ctx context.Context) error Exists(ctx context.Context, key string) (bool, error) io.Closer }
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 error is nil, it indicates that the object was successfully fetched or built. So 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)
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 ¶
func WithNeverExpire ¶
func WithNeverExpire() Option
func WithSingleflight ¶
func WithSingleflight(single *singleflight.Group) Option