cachexredis

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package cachexredis is a cachex.Invalidator over Redis pub/sub.

Delivery is at-most-once and best effort. go-redis PubSub reconnects and resubscribes automatically, but messages published while a subscriber is disconnected are lost; the cache's L1 TTL still bounds staleness for those. Duplicate or reordered messages are harmless because an invalidation only drops L1 copies.

Index

Constants

View Source
const DefaultChannel = "cachex:invalidate"

DefaultChannel is used when Config.Channel is empty.

Variables

View Source
var ErrMalformedValue = errors.New("cachexredis: value has no version header")

ErrMalformedValue reports a value that wasn't written by a Store (no version header).

Functions

This section is empty.

Types

type Config

type Config struct {
	Client  redis.UniversalClient // required
	Channel string                // default "cachex:invalidate"
	OnError func(error)           // optional: decode/subscription errors
}

Config configures an Invalidator.

type Invalidator

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

Invalidator publishes and receives cachex invalidations on a Redis channel.

func New

func New(c Config) (*Invalidator, error)

New validates c and returns an Invalidator with a random origin id.

func (*Invalidator) Publish

func (i *Invalidator) Publish(ctx context.Context, msg cachex.Invalidation) error

Publish PUBLISHes msg to the channel. Bound it with ctx; no retry is done here.

func (*Invalidator) Subscribe

func (i *Invalidator) Subscribe(ctx context.Context, fn func(cachex.Invalidation)) error

Subscribe SUBSCRIBEs and waits for the confirmation before returning, so messages published after Subscribe returns are delivered. fn is called from one goroutine per Subscribe call until ctx is done, then the PubSub is closed. Multiple calls are allowed.

type Store added in v0.2.0

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

Store is a cachex.Store (L2) over Redis. It also implements cachex.MultiGetter, cachex.CASStore and GetsMulti.

Each value is stored as an 8-byte random version followed by the payload. Every write picks a new version, and the version is the compare-and-swap token, so a CAS fails after any intervening write, even one that stored identical bytes. Values written to the same keys by other clients lack that header and are reported as errors, never served.

Redis keys and values are binary-safe, so keys are used as-is (no hashing, no length limit beyond Redis's own).

func NewStore added in v0.2.0

func NewStore(c StoreConfig) (*Store, error)

NewStore builds a Store. It does not contact Redis.

func (*Store) Add added in v0.2.0

func (s *Store) Add(ctx context.Context, key string, val []byte, ttl time.Duration) error

Add stores only if key is absent, else cachex.ErrNotStored.

func (*Store) Close added in v0.2.0

func (s *Store) Close() error

Close marks the Store closed; with CloseClient it also closes the client. Idempotent.

func (*Store) CompareAndSwap added in v0.2.0

func (s *Store) CompareAndSwap(ctx context.Context, key string, val []byte, token any, ttl time.Duration) error

CompareAndSwap stores val only if key still has the version Gets returned. It returns cachex.ErrNotStored when the key changed and cachex.ErrMiss when it is gone.

func (*Store) Delete added in v0.2.0

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

Delete removes key; a missing key is not an error.

func (*Store) Get added in v0.2.0

func (s *Store) Get(ctx context.Context, key string) ([]byte, error)

Get returns cachex.ErrMiss when absent.

func (*Store) GetMulti added in v0.2.0

func (s *Store) GetMulti(ctx context.Context, keys []string) (map[string][]byte, error)

GetMulti fetches keys with one MGET; absent keys are omitted.

func (*Store) Gets added in v0.2.0

func (s *Store) Gets(ctx context.Context, key string) ([]byte, any, error)

Gets returns the value and its version as the CAS token.

func (*Store) GetsMulti added in v0.2.0

func (s *Store) GetsMulti(ctx context.Context, keys []string) (map[string][]byte, map[string]any, error)

GetsMulti is Gets for many keys in one MGET. Absent keys are missing from both maps.

func (*Store) Incr added in v0.2.0

func (s *Store) Incr(ctx context.Context, key string, delta uint64) (uint64, error)

Incr adds delta to a decimal counter, keeping its TTL; cachex.ErrMiss when absent. Counters are limited to 2^53 (Lua numbers are doubles).

func (*Store) Set added in v0.2.0

func (s *Store) Set(ctx context.Context, key string, val []byte, ttl time.Duration) error

Set stores val; ttl <= 0 means no expiry.

type StoreConfig added in v0.2.0

type StoreConfig struct {
	Client      redis.UniversalClient // required; a Cluster client works (every op touches one key)
	KeyPrefix   string                // prepended to every key
	CloseClient bool                  // Close also closes Client
}

StoreConfig configures a Store.

Jump to

Keyboard shortcuts

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