cache

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package cache stores values in Redis, either as raw strings or as JSON-encoded Go values.

Client is the low level: it sets and gets strings under a TTL.

client := cache.NewClient("localhost:6379")

Repository is the typed layer on top. It marshals on the way in and unmarshals on the way out, so callers work with their own types:

orders := cache.NewRepository[Order](client)

if err := orders.Put(ctx, key, order, 10*time.Minute); err != nil {
	return err
}

order, err := orders.Get(ctx, key)

A key that is absent is not special-cased here: Get returns the redis.Nil error from the underlying driver, so distinguishing a miss from a real failure currently means importing github.com/redis/go-redis/v9 and comparing against it.

NewClient takes an address and nothing else - no password, database index, TLS or timeouts - so it reaches a local or sidecar Redis but not a managed instance that requires authentication.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	Set(ctx context.Context, key string, value any, ttl time.Duration) error
	Get(ctx context.Context, key string) (string, error)
}

Client is the raw string-level cache. Values are whatever go-redis can encode - strings, byte slices, numbers - and a zero ttl means the key never expires.

Get returns the redis.Nil error when the key is absent, which callers have to compare against github.com/redis/go-redis/v9 to tell a miss from a failure.

func NewClient

func NewClient(addr string) Client

NewClient connects to the Redis instance at addr ("host:port").

It takes no credentials, database index, TLS config or timeouts, so it does not reach an instance that requires authentication.

type Repository

type Repository[T any] interface {
	Get(ctx context.Context, key string) (T, error)
	Put(ctx context.Context, key string, t T, ttl time.Duration) error
}

Repository stores values of type T, marshalling to JSON on Put and unmarshalling on Get.

Get returns the zero value of T alongside any error, including the redis.Nil that signals a miss.

Example (Miss)

A miss is not distinguished from a failure by this package: Get returns the driver's redis.Nil, so telling them apart means comparing against it.

package main

import (
	"context"
	"fmt"

	"github.com/diegodesousas/go-devkit/pkg/cache"
	"github.com/pkg/errors"
	"github.com/redis/go-redis/v9"
)

type session struct {
	UserID string `json:"user_id"`
	Admin  bool   `json:"admin"`
}

func main() {
	sessions := cache.NewRepository[session](cache.NewClient("localhost:6379"))

	_, err := sessions.Get(context.Background(), "sess:absent")

	switch {
	case errors.Is(err, redis.Nil):
		fmt.Println("cache miss")
	case err != nil:
		fmt.Println("cache failure:", err)
	default:
		fmt.Println("hit")
	}
}

func NewRepository

func NewRepository[T any](client Client) Repository[T]

NewRepository returns a Repository storing values of type T as JSON on top of client.

Example

Requires a Redis instance, so this example is compiled but not run.

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/diegodesousas/go-devkit/pkg/cache"
)

type session struct {
	UserID string `json:"user_id"`
	Admin  bool   `json:"admin"`
}

func main() {
	client := cache.NewClient("localhost:6379")
	sessions := cache.NewRepository[session](client)

	ctx := context.Background()

	if err := sessions.Put(ctx, "sess:42", session{UserID: "42"}, 10*time.Minute); err != nil {
		panic(err)
	}

	s, err := sessions.Get(ctx, "sess:42")
	if err != nil {
		panic(err)
	}

	fmt.Println(s.UserID)
}

Jump to

Keyboard shortcuts

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