Documentation
¶
Index ¶
- Variables
- type Cache
- type GetOption
- type MemoryCache
- func (m *MemoryCache) Cleanup(_ context.Context) error
- func (m *MemoryCache) Close() error
- func (m *MemoryCache) Delete(_ context.Context, key string) error
- func (m *MemoryCache) Drain(_ context.Context) (map[string][]byte, error)
- func (m *MemoryCache) Get(_ context.Context, key string, opts ...GetOption) ([]byte, error)
- func (m *MemoryCache) GetAndDelete(ctx context.Context, key string) ([]byte, error)
- func (m *MemoryCache) Set(_ context.Context, key string, value []byte, opts ...Option) error
- func (m *MemoryCache) SetOrFail(_ context.Context, key string, value []byte, opts ...Option) error
- type Option
- type RedisCache
- func (r *RedisCache) Cleanup(_ context.Context) error
- func (r *RedisCache) Close() error
- func (r *RedisCache) Delete(ctx context.Context, key string) error
- func (r *RedisCache) Drain(ctx context.Context) (map[string][]byte, error)
- func (r *RedisCache) Get(ctx context.Context, key string, opts ...GetOption) ([]byte, error)
- func (r *RedisCache) GetAndDelete(ctx context.Context, key string) ([]byte, error)
- func (r *RedisCache) Set(ctx context.Context, key string, value []byte, opts ...Option) error
- func (r *RedisCache) SetOrFail(ctx context.Context, key string, value []byte, opts ...Option) error
- type RedisConfig
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // ErrInvalidConfig indicates an invalid configuration. ErrInvalidConfig = errors.New("invalid config") // 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 ¶
This section is empty.
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)
// Close closes the cache.
// The operation is safe for concurrent use.
Close() 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 MemoryCache ¶ added in v1.34.3
type MemoryCache struct {
// contains filtered or unexported fields
}
func NewMemory ¶
func NewMemory(ttl time.Duration) *MemoryCache
func (*MemoryCache) Cleanup ¶ added in v1.34.3
func (m *MemoryCache) Cleanup(_ context.Context) error
Cleanup implements Cache.
func (*MemoryCache) Close ¶ added in v1.34.3
func (m *MemoryCache) Close() error
func (*MemoryCache) Delete ¶ added in v1.34.3
func (m *MemoryCache) Delete(_ context.Context, key string) error
Delete implements Cache.
func (*MemoryCache) GetAndDelete ¶ added in v1.34.3
GetAndDelete implements Cache.
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.
type RedisCache ¶ added in v1.34.3
type RedisCache struct {
// contains filtered or unexported fields
}
func NewRedis ¶
func NewRedis(config RedisConfig) (*RedisCache, error)
func (*RedisCache) Cleanup ¶ added in v1.34.3
func (r *RedisCache) Cleanup(_ context.Context) error
Cleanup implements Cache.
func (*RedisCache) Close ¶ added in v1.34.3
func (r *RedisCache) Close() error
func (*RedisCache) Delete ¶ added in v1.34.3
func (r *RedisCache) Delete(ctx context.Context, key string) error
Delete implements Cache.
func (*RedisCache) GetAndDelete ¶ added in v1.34.3
GetAndDelete implements Cache.
type RedisConfig ¶ added in v1.32.0
type RedisConfig struct {
// Client is the Redis client to use.
// If nil, a client is created from the URL.
Client *redis.Client
// URL is the Redis URL to use.
// If empty, the Redis client is not created.
URL string
// Prefix is the prefix to use for all keys in the Redis cache.
Prefix string
// TTL is the time-to-live for all cache entries.
TTL time.Duration
}
RedisConfig configures the Redis cache backend.
Click to show internal directories.
Click to hide internal directories.