cachemanager

package
v0.0.0-...-0f16109 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 18, 2026 License: AGPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

func (c *CacheManager) Decr(ctx context.Context, key string) (int64, error)

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) Expire

func (c *CacheManager) Expire(ctx context.Context, key string, expiration time.Duration) error

Expire sets a timeout on a key. This is a pass-through method.

func (*CacheManager) Get

func (c *CacheManager) Get(ctx context.Context, key string, dest any) error

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

func (c *CacheManager) GetTTL(ctx context.Context, key string) (time.Duration, error)

GetTTL retrieves the remaining time-to-live for a key. It returns 0 if the exists but has no expirations (persistent).

func (*CacheManager) Incr

func (c *CacheManager) Incr(ctx context.Context, key string) (int64, error)

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.

func (*CacheManager) Set

func (c *CacheManager) Set(ctx context.Context, key string, value any, expiration time.Duration) error

Set marshals the provided value to JSON and then stores it in the cache. It handles the conversion of any Go type to a byte slice for storage.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL