cache

package
v1.3.12 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package cache provides a generic, type-safe caching layer using Redis.

This package uses generics to provide compile-time type safety for cached values. Cache entries are stored as JSON in Redis with TTL support. Keys can be encoded using custom KeyEncoder implementations for different types (strings, integers, UUIDs).

Basic usage:

type User struct {
    ID   int
    Name string
}

cache := cache.New[string, User](
    redisClient,
    "users",           // hash key
    24 * time.Hour,    // TTL
    cache.NewStringKeyEncoder(),
)

// Set a value
user := User{ID: 123, Name: "Alice"}
if err := cache.Set(ctx, "user:123", &user); err != nil {
    return err
}

// Get a value
retrieved, err := cache.Get(ctx, "user:123")
if err != nil {
    return err
}

Custom KeyEncoder implementations can be provided for non-standard key types:

cache := cache.New[CustomKeyType, MyValue](
    redisClient,
    "data",
    time.Hour,
    MyCustomKeyEncoder{},
)

The cache layer automatically handles JSON marshaling/unmarshaling and provides operations for Set, Get, Delete, and Invalidate (clear all cached values for a hash).

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrKeyNotFound     = errors.New("cache: key not found")
	ErrCacheMarshal    = errors.New("cache: failed to marshal value")
	ErrCacheUnmarshal  = errors.New("cache: failed to unmarshal value")
	ErrCacheGet        = errors.New("cache: failed to get")
	ErrCacheSet        = errors.New("cache: failed to set")
	ErrCacheTTL        = errors.New("cache: failed to set TTL")
	ErrCacheDelete     = errors.New("cache: failed to delete")
	ErrCacheInvalidate = errors.New("cache: failed to invalidate")
)
View Source
var ErrCacheInvalidKeyType = errors.New("cache: invalid key type")

Functions

This section is empty.

Types

type Cache

type Cache[K any, V any] struct {
	// contains filtered or unexported fields
}

func New

func New[K any, V any](
	client redis.UniversalClient,
	hashKey string,
	ttl time.Duration,
	keyEncoder KeyEncoder,
) *Cache[K, V]

func (*Cache[K, V]) Delete

func (c *Cache[K, V]) Delete(ctx context.Context, key K) error

func (*Cache[K, V]) Get

func (c *Cache[K, V]) Get(ctx context.Context, key K) (*V, error)

func (*Cache[K, V]) Invalidate

func (c *Cache[K, V]) Invalidate(ctx context.Context) error

func (*Cache[K, V]) Set

func (c *Cache[K, V]) Set(ctx context.Context, key K, value *V) error

type IntKeyEncoder

type IntKeyEncoder struct{}

func NewIntKeyEncoder

func NewIntKeyEncoder() *IntKeyEncoder

func (*IntKeyEncoder) Encode

func (e *IntKeyEncoder) Encode(key any) (string, error)

type KeyEncoder

type KeyEncoder interface {
	Encode(key any) (string, error)
}

type StringKeyEncoder

type StringKeyEncoder struct{}

func NewStringKeyEncoder

func NewStringKeyEncoder() *StringKeyEncoder

func (*StringKeyEncoder) Encode

func (e *StringKeyEncoder) Encode(key any) (string, error)

type UUIDKeyEncoder

type UUIDKeyEncoder struct{}

func NewUUIDKeyEncoder

func NewUUIDKeyEncoder() *UUIDKeyEncoder

func (*UUIDKeyEncoder) Encode

func (e *UUIDKeyEncoder) Encode(key any) (string, error)

Jump to

Keyboard shortcuts

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