cache

package
v1.1.40-beta.1 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package cache is a thin, nil-safe wrapper around a shared Redis/Valkey instance. It exists for the narrow case a dataset store is the wrong tool for: deciding, among several concurrent callers racing over the same key, which one goes first — something a MergeTree-backed store cannot answer quickly, because a row it was just asked to write is not guaranteed queryable back the instant the write call returns.

Every method is a safe no-op (a permanent miss) on a nil *Client, so a deployment that has not configured Redis keeps working exactly as it did before this package existed: callers fall through to their real source of truth every time.

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
}

func New

func New(cfg Config) *Client

New returns nil when Addr is empty.

func (*Client) Claim

func (c *Client) Claim(ctx context.Context, key, value string, ttl time.Duration) (claimed bool, current string, err error)

Claim atomically sets key to value only if key does not already exist — the primitive a caller needs to pick exactly one winner among several concurrent, otherwise-identical attempts, which Get-then-Set cannot do because two callers can both Get a miss before either Sets.

claimed is true when this call is the one that set it. When it is false, current is whatever value is there now — either another concurrent caller's winning claim, or one set earlier by Set — and the caller should treat that as the answer rather than as a reason to claim again.

A nil Client always reports an unclaimed miss (claimed=false, current="", err=nil): without a real cache behind it there is no winner to pick, and the caller's ordinary fallback path is what decides instead.

func (*Client) Close

func (c *Client) Close() error

func (*Client) Get

func (c *Client) Get(ctx context.Context, key string) (string, bool)

Get returns the cached value and whether it was present. A nil Client, a connection error, and an actual miss all read the same to the caller: false — there is no real-time-vs-expired distinction a check-then-act caller needs to make, since all three mean "consult the real source of truth instead."

func (*Client) Set

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

Set stores value under key for ttl. Best-effort: a write failure here just means the next Get falls through to the real source of truth again, same as if the key had never been set.

func (*Client) Touch

func (c *Client) Touch(ctx context.Context, key string, ttl time.Duration)

Touch resets key's TTL without changing its value, so a group still receiving events keeps its fast-path entry alive instead of falling out of cache mid-burst and forcing every caller back to the real source of truth.

type Config

type Config struct {
	Addr     string
	Password string
	DB       int
}

Jump to

Keyboard shortcuts

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