Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // ErrKeyNotFound indicates no value exists for the given key. ErrKeyNotFound = errors.New("key not found") // ErrKeyExpired indicates a value exists but has expired. ErrKeyExpired = errors.New("key expired") // ErrKeyExists indicates a conflicting set when the key already exists. ErrKeyExists = errors.New("key already exists") )
Functions ¶
Types ¶
type Cache ¶
type Cache interface {
// Set sets the value for the given key in the cache.
Set(ctx context.Context, key string, value []byte, opts ...Option) error
// SetOrFail is like Set, but returns ErrKeyExists if the key already exists.
SetOrFail(ctx context.Context, key string, value []byte, opts ...Option) error
// Get gets the value for the given key from the cache.
//
// If the key is not found, it returns ErrKeyNotFound.
// If the key has expired, it returns ErrKeyExpired.
// Otherwise, it returns the value and nil.
Get(ctx context.Context, key string, opts ...GetOption) ([]byte, error)
// GetAndDelete is like Get, but also deletes the key from the cache.
GetAndDelete(ctx context.Context, key string) ([]byte, error)
// Delete removes the item associated with the given key from the cache.
// If the key does not exist, it performs no action and returns nil.
// The operation is safe for concurrent use.
Delete(ctx context.Context, key string) error
// Cleanup removes all expired items from the cache.
// The operation is safe for concurrent use.
Cleanup(ctx context.Context) error
// Drain returns a map of all the non-expired items in the cache.
// The returned map is a snapshot of the cache at the time of the call.
// The cache is cleared after the call.
// The operation is safe for concurrent use.
Drain(ctx context.Context) (map[string][]byte, error)
}
type GetOption ¶ added in v1.31.0
type GetOption func(*getOptions)
func AndDefaultTTL ¶ added in v1.31.0
func AndDefaultTTL() GetOption
func AndSetValidUntil ¶ added in v1.31.0
func AndUpdateTTL ¶ added in v1.31.0
type Option ¶
type Option func(*options)
Option configures per-item cache behavior (e.g., expiry).
func WithTTL ¶
WithTTL is an Option that sets the TTL (time to live) for an item, i.e. the item will expire after the given duration from the time of insertion.
func WithValidUntil ¶
WithValidUntil is an Option that sets the valid until time for an item, i.e. the item will expire at the given time.
Click to show internal directories.
Click to hide internal directories.