cache

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package cache provides a generic key-value cache interface.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Get retrieves a value by key. Returns the value, whether it was found, and any error.
	Get(ctx context.Context, key string) ([]byte, bool, error)

	// Set stores a value by key.
	Set(ctx context.Context, key string, value []byte) error

	// GetMulti retrieves multiple values by keys. Returns a map of found key-value pairs.
	GetMulti(ctx context.Context, keys []string) (map[string][]byte, error)

	// SetMulti stores multiple key-value pairs.
	SetMulti(ctx context.Context, entries map[string][]byte) error

	// Close releases resources held by the cache.
	Close() error
}

Cache is a generic key-value cache.

type InMemoryCache

type InMemoryCache struct {
	// contains filtered or unexported fields
}

InMemoryCache is a thread-safe in-memory cache backed by a map.

func NewInMemory

func NewInMemory(ttl time.Duration) *InMemoryCache

NewInMemory creates a new in-memory cache. A zero TTL means entries never expire.

func (*InMemoryCache) Close

func (c *InMemoryCache) Close() error

Close is a no-op for the in-memory cache.

func (*InMemoryCache) Get

func (c *InMemoryCache) Get(_ context.Context, key string) ([]byte, bool, error)

Get retrieves a value by key, skipping expired entries.

func (*InMemoryCache) GetMulti

func (c *InMemoryCache) GetMulti(_ context.Context, keys []string) (map[string][]byte, error)

GetMulti retrieves multiple values by keys, skipping expired entries.

func (*InMemoryCache) Set

func (c *InMemoryCache) Set(_ context.Context, key string, value []byte) error

Set stores a value by key.

func (*InMemoryCache) SetMulti

func (c *InMemoryCache) SetMulti(_ context.Context, entries map[string][]byte) error

SetMulti stores multiple key-value pairs.

type RedisCache

type RedisCache struct {
	// contains filtered or unexported fields
}

RedisCache is a cache backed by Redis.

func NewRedis

func NewRedis(redisURL, prefix string, ttl time.Duration) (*RedisCache, error)

NewRedis creates a new Redis-backed cache. The prefix is prepended to all keys (e.g., "panda:embed:"). A zero TTL means entries never expire.

func (*RedisCache) Close

func (c *RedisCache) Close() error

Close closes the Redis client connection.

func (*RedisCache) Get

func (c *RedisCache) Get(ctx context.Context, key string) ([]byte, bool, error)

Get retrieves a value by key.

func (*RedisCache) GetMulti

func (c *RedisCache) GetMulti(ctx context.Context, keys []string) (map[string][]byte, error)

GetMulti retrieves multiple values by keys.

func (*RedisCache) Set

func (c *RedisCache) Set(ctx context.Context, key string, value []byte) error

Set stores a value by key.

func (*RedisCache) SetMulti

func (c *RedisCache) SetMulti(ctx context.Context, entries map[string][]byte) error

SetMulti stores multiple key-value pairs.

Jump to

Keyboard shortcuts

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