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
- Variables
- type Config
- type Invalidator
- type Store
- func (s *Store) Add(ctx context.Context, key string, val []byte, ttl time.Duration) error
- func (s *Store) Close() error
- func (s *Store) CompareAndSwap(ctx context.Context, key string, val []byte, token any, ttl time.Duration) error
- func (s *Store) Delete(ctx context.Context, key string) error
- func (s *Store) Get(ctx context.Context, key string) ([]byte, error)
- func (s *Store) GetMulti(ctx context.Context, keys []string) (map[string][]byte, error)
- func (s *Store) Gets(ctx context.Context, key string) ([]byte, any, error)
- func (s *Store) GetsMulti(ctx context.Context, keys []string) (map[string][]byte, map[string]any, error)
- func (s *Store) Incr(ctx context.Context, key string, delta uint64) (uint64, error)
- func (s *Store) Set(ctx context.Context, key string, val []byte, ttl time.Duration) error
- type StoreConfig
Constants ¶
const DefaultChannel = "cachex:invalidate"
DefaultChannel is used when Config.Channel is empty.
Variables ¶
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) Close ¶ added in v0.2.0
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) GetMulti ¶ added in v0.2.0
GetMulti fetches keys with one MGET; absent keys are omitted.
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.
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.