cache

package
v1.6.2 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2026 License: AGPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

Package cache manages Redis connection lifecycle and provides a Redis-backed idempotency store for safe request replay.

Connections are named and cached process-wide. GetConnection returns the existing client for a name or creates one with double-checked locking; the underlying client is configured with sensible timeouts (5 s dial, 3 s read/write) and a small pool (10 connections, 5 idle). Ready pings a named connection under a 1 s timeout and is the hook the health package uses for its readiness probe. GetClient, ListConnections, CloseConnection, and CloseAll cover lookup and graceful shutdown.

IdempotencyStore is the contract for caching a response keyed by an idempotency key. RedisIdempotencyStore is the Redis implementation: it serializes an IdempotencyRecord (response body, content type, status code, creation time) as JSON under a configurable prefix, applies the caller's TTL, and treats a missing key as (nil, nil) rather than an error. Build one with NewRedisIdempotencyStore, passing the client from GetConnection and a prefix such as "idempotency".

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloseAll

func CloseAll() error

CloseAll closes all Redis connections

func CloseConnection

func CloseConnection(name string) error

CloseConnection safely closes and removes a specific connection

func GetClient

func GetClient(name string) (*redis.Client, bool)

GetClient returns a specific Redis client by name

func GetConnection

func GetConnection(name string, cfg *config.RedisConfig) (*redis.Client, error)

GetConnection returns a cached Redis connection or creates a new one

func ListConnections

func ListConnections() []string

ListConnections returns all connection names

func Ready

func Ready(name string) bool

Ready checks if a specific Redis connection is alive

Types

type IdempotencyRecord

type IdempotencyRecord struct {
	Key         string    `json:"key"`
	Response    []byte    `json:"response"`
	ContentType string    `json:"content_type"`
	StatusCode  int       `json:"status_code"`
	CreatedAt   time.Time `json:"created_at"`
}

IdempotencyRecord stores the cached response for an idempotent request

type IdempotencyStore

type IdempotencyStore interface {
	// Get retrieves a cached response by idempotency key
	// Returns nil, nil if key does not exist
	Get(ctx context.Context, key string) (*IdempotencyRecord, error)

	// Store saves a response with the given idempotency key and TTL
	Store(ctx context.Context, key string, record *IdempotencyRecord, ttl time.Duration) error

	// Exists checks if an idempotency key exists (for quick checks)
	Exists(ctx context.Context, key string) (bool, error)

	// Delete removes an idempotency record
	Delete(ctx context.Context, key string) error
}

IdempotencyStore interface for storing/retrieving idempotency records

type RedisIdempotencyStore

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

RedisIdempotencyStore implements IdempotencyStore using Redis

func NewRedisIdempotencyStore

func NewRedisIdempotencyStore(client *redis.Client, prefix string) *RedisIdempotencyStore

NewRedisIdempotencyStore creates a new Redis-backed idempotency store prefix: namespace for keys (e.g., "idempotency" -> "idempotency:key")

func (*RedisIdempotencyStore) Delete

func (s *RedisIdempotencyStore) Delete(ctx context.Context, key string) error

Delete removes an idempotency record

func (*RedisIdempotencyStore) Exists

func (s *RedisIdempotencyStore) Exists(ctx context.Context, key string) (bool, error)

Exists checks if an idempotency key exists (for quick checks without fetching data)

func (*RedisIdempotencyStore) Get

Get retrieves a cached response by idempotency key Returns nil, nil if key does not exist (not an error condition)

func (*RedisIdempotencyStore) Store

Store saves a response with the given idempotency key and TTL

Jump to

Keyboard shortcuts

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