cache

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package cache provides a Redis/Valkey-backed cache client with OpenTelemetry instrumentation, built on go-redis v9.

It exposes typed methods (SetJSON, GetJSON) for struct values and GetBytes for raw byte access. OpenTelemetry tracing and metrics are enabled by default on every client.

Basic usage:

c, err := cache.New(ctx, "redis://localhost:6379/0")
if err != nil { ... }
defer c.Close()

err = c.SetJSON(ctx, "mykey", myStruct, 15*time.Minute)
found, err := c.GetJSON(ctx, "mykey", &myStruct)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is a thin wrapper around a Redis client that supports JSON serialisation.

Zero value is not usable — use New() to construct.

func New

func New(_ context.Context, url string, opts ...Option) (*Client, error)

New builds a Client from a Redis URL and applies the given options.

The URL must be in redis:// or rediss:// format as accepted by redis.ParseURL (e.g. "redis://localhost:6379/0"). OpenTelemetry tracing and metrics are automatically enabled on every client.

Callers MUST call Close() when the client is no longer needed.

func (*Client) Close

func (c *Client) Close() error

Close releases underlying Redis connection pool resources.

func (*Client) GetBytes

func (c *Client) GetBytes(ctx context.Context, key string) ([]byte, error)

GetBytes retrieves the raw byte slice stored at key. It returns (nil, nil) when the key does not exist.

func (*Client) GetJSON

func (c *Client) GetJSON(ctx context.Context, key string, dest any) (bool, error)

GetJSON retrieves the value at key and unmarshals it into dest. It returns (true, nil) on a cache hit, (false, nil) when the key does not exist, and (false, err) on any other error.

func (*Client) Ping

func (c *Client) Ping(ctx context.Context) error

Ping checks connectivity to the Redis server.

func (*Client) SetJSON

func (c *Client) SetJSON(ctx context.Context, key string, val any, ttl time.Duration) error

SetJSON marshals val to JSON and stores it at key with the given TTL. A zero or negative TTL means no expiration.

type Option

type Option func(*Client)

Option is a functional option that configures a Client.

Jump to

Keyboard shortcuts

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