Documentation
¶
Index ¶
- Variables
- type CacheClient
- type CacheManager
- func (c *CacheManager) Decr(ctx context.Context, key string) (int64, error)
- func (c *CacheManager) Delete(ctx context.Context, keys ...string) error
- func (c *CacheManager) Expire(ctx context.Context, key string, expiration time.Duration) error
- func (c *CacheManager) Get(ctx context.Context, key string, dest any) error
- func (c *CacheManager) GetTTL(ctx context.Context, key string) (time.Duration, error)
- func (c *CacheManager) Incr(ctx context.Context, key string) (int64, error)
- func (c *CacheManager) MGet(ctx context.Context, destMap map[string]any, keys ...string) (missedKeys []string, err error)
- func (c *CacheManager) Set(ctx context.Context, key string, value any, expiration time.Duration) error
Constants ¶
This section is empty.
Variables ¶
var ErrKeyNotFound = errors.New("key not found in cache")
ErrKeyNotFound is a custom error returned when a key is not found in the cache.
Functions ¶
This section is empty.
Types ¶
type CacheClient ¶
type CacheClient interface {
// Set stores a value associated with a given key and an expiration duration.
// The 'value' here is expected to be a byte slice, allowing the CacheManager
// to handle marshalling of various data types into a storable format.
Set(ctx context.Context, key string, value []byte, expiration time.Duration) error
// Get retrieves the value associated with a key.
// It returns the value as a byte slice, If the key is not found, it returns ErrKeyNotFound.
Get(ctx context.Context, key string) ([]byte, error)
MGet(ctx context.Context, keys ...string) ([]interface{}, error)
// Delete removes one or more keys from the cache.
Delete(ctx context.Context, keys ...string) error
// GetTTL retrieves the remaining time-to-live (TTL) for a key.
// If the key is not found, it returns ErrKeyNotFound.
// If the key exists nut has no TTL (it's persistent), it returns 0.
GetTTL(ctx context.Context, key string) (time.Duration, error)
// Incr atomically increments the integer value of a key by one.
Incr(ctx context.Context, key string) (int64, error)
// Decr atomically decrements the integer value of a key by one.
Decr(ctx context.Context, key string) (int64, error)
// Expire sets a timeout on a key.
Expire(ctx context.Context, key string, expiration time.Duration) error
}
CacheClient is a generic interface for basic cache operations. This interface is completely abstracted from implementation details (Redis, memcached ...).
type CacheManager ¶
type CacheManager struct {
// contains filtered or unexported fields
}
CacheManager creates a new instance of CacheManager.
func New ¶
func New(client CacheClient, logger *slog.Logger) *CacheManager
New creates a new instance of CacheManager. It accepts an implementation of CacheClient interface.
func (*CacheManager) Decr ¶
Decr atomically decrements the integer value of a key by one. This is a pass-through method and does not involve JSON marshalling.
func (*CacheManager) Delete ¶
func (c *CacheManager) Delete(ctx context.Context, keys ...string) error
Delete removes one or more keys from cache.
func (*CacheManager) Get ¶
Get retrieves a value from the cache, unmarshal it from JSON into the 'dest' pointers. If the key is not found, it returns ErrKeyFound.
func (*CacheManager) GetTTL ¶
GetTTL retrieves the remaining time-to-live for a key. It returns 0 if the exists but has no expirations (persistent).
func (*CacheManager) Incr ¶
Incr atomically increments the integer value of a key by one. This is a pass-through method and does not involve JSON marshalling.
func (*CacheManager) MGet ¶
func (c *CacheManager) MGet(ctx context.Context, destMap map[string]any, keys ...string) (missedKeys []string, err error)
MGet retrieves multiple values from the cache for the given keys. It populates the `destMap` for keys are found(cache hint). The keys of destMap must be the cache keys, and values must be pointers to the destination structs It returns a slice of keys that were not found in the cache(cache misses), which can then be fetched from the primary data source.