cache

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEmptyConfig       = errors.New("empty config")
	ErrAddrMissing       = errors.New("missing required field 'Addr'")
	ErrCredentialMissing = errors.New("missing Redis credentials in environment")
	ErrConnectionFail    = errors.New("failed to connect to Redis")
)

Error variables to describe common failure modes.

View Source
var RedisCl *redis.Client

RedisCl global variable for the Redis client, can be overridden in tests

View Source
var RedisClientFunc = func(cfg *Config) RedisClient {
	return redis.NewClient(&redis.Options{
		Addr:     cfg.Addr,
		Password: os.Getenv("REDIS_PASSWORD"),
		DB:       0,
	})
}

RedisClientFunc is a function variable that creates a Redis client based on the provided configuration. It can be overridden for testing purposes.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	Client RedisClient
}

Cache wraps a Redis client to provide basic caching operations.

func New

func New(ctx context.Context, cfg *Config) (*Cache, func() error, error)

New initializes and returns a Cache instance along with a close function to release resources.

func (*Cache) Clear

func (c *Cache) Clear(ctx context.Context) error

Clear removes all keys in the currently selected Redis database.

func (*Cache) Delete

func (c *Cache) Delete(ctx context.Context, key string) error

Delete removes the specified key from Redis.

func (*Cache) Get

func (c *Cache) Get(ctx context.Context, key string) (string, error)

Get retrieves the value for the specified key from Redis.

func (*Cache) Set

func (c *Cache) Set(ctx context.Context, key, value string, ttl time.Duration) error

Set stores the given key-value pair in Redis with the specified TTL (time to live).

type Config

type Config struct {
	Addr string
}

Config holds the configuration required to connect to Redis.

type RedisClient

type RedisClient interface {
	Get(ctx context.Context, key string) *redis.StringCmd
	Set(ctx context.Context, key string, value interface{}, ttl time.Duration) *redis.StatusCmd
	Del(ctx context.Context, keys ...string) *redis.IntCmd
	FlushDB(ctx context.Context) *redis.StatusCmd
	Ping(ctx context.Context) *redis.StatusCmd
	Close() error
}

RedisClient is an interface for Redis operations that allows mocking

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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