cachex

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: AGPL-3.0 Imports: 23 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package cachex talks to Redis-compatible caches (Redis, Valkey, KeyDB, Dragonfly) and memcached, and classifies their commands by risk. Neither has read-only transactions, so the classifier is the safety layer: without --write only commands known to be reads are sent.

Index

Constants

This section is empty.

Variables

View Source
var InfoSections = []string{"server", "clients", "memory", "persistence", "threads", "stats", "replication", "cpu",
	"commandstats", "latencystats", "sentinel", "cluster", "modules", "keyspace", "errorstats", "keysizes",
	"all", "default", "everything"}

InfoSections are the sections INFO accepts.

View Source
var MemcacheSections = []string{"items", "slabs", "settings", "sizes", "conns"}

MemcacheSections are the sections `stats` takes ("" is the general one).

Functions

func Classify

func Classify(args []string) driver.Verdict

Classify rates a raw command. Unknown commands are writes.

func ClassifyFor added in v0.3.0

func ClassifyFor(kind string, args []string) driver.Verdict

ClassifyFor rates a raw command for the conn's kind.

func ClassifyMemcache added in v0.3.0

func ClassifyMemcache(args []string) driver.Verdict

ClassifyMemcache rates a memcached command line.

func ClusterWideProblem added in v0.13.0

func ClusterWideProblem(args []string) error

ClusterWideProblem refuses a whole-key-space command other than DBSIZE for a Redis Cluster, before any approval is asked for it.

func HideSecrets added in v0.3.0

func HideSecrets(args []string, v any) any

HideSecrets hides credentials in the reply to args, for output to an AI agent: the values of password parameters in CONFIG GET (requirepass, masterauth, tls-key-file-pass, ...) and the password hashes in ACL LIST and ACL GETUSER. The output masker cannot spot these: requirepass is not a name it knows, and over RESP2 CONFIG GET returns names and values as separate items of one list.

func Info

func Info(ctx context.Context, c redis.UniversalClient, section string) (map[string]map[string]string, error)

Info returns the INFO reply as sections of key/value pairs.

func MemcacheRules added in v0.3.0

func MemcacheRules() []driver.Rule

MemcacheRules are the rules ClassifyMemcache can report.

func MemcacheWords added in v0.13.0

func MemcacheWords(args []string) ([]string, error)

MemcacheWords are the words memcached reads from a `cache run` line. memcached splits a line at spaces and ends it at a newline, so the words are re-split the same way here, before the command is rated: otherwise "get" plus an argument holding "\r\nflush_all" is rated a read and sends flush_all, " flush_all" is not seen as flush_all, and "mg" plus "key T30" hides the touch flag. A control byte cannot be in a memcached command, so a word with one is refused.

func OneReply added in v0.12.0

func OneReply(args []string) error

OneReply refuses a command that does not answer with one reply, which cache run waits for: MONITOR, SUBSCRIBE and the like, and XREAD or XREADGROUP with BLOCK.

func Open

func Open(ctx context.Context, t driver.Target, password string, dial DialFunc, timeout time.Duration) (redis.UniversalClient, error)

Open connects to a redis conn and checks it answers: one server, a Redis Cluster (any node leads to the others) or the master that the Sentinels name.

func Quote

func Quote(s string) string

Quote shows s on one line: as it is when that is unambiguous, else in double quotes with \n, \t, \" and \xNN escapes.

func ReplyText

func ReplyText(v any) string

ReplyText renders a reply from Run like redis-cli does.

func Rules

func Rules() []driver.Rule

Rules lists the rules Classify can report.

func Run

func Run(ctx context.Context, c redis.UniversalClient, args []string) (any, error)

Run sends a raw command and converts the reply to JSON-safe values.

func SectionsFor added in v0.3.0

func SectionsFor(kind string) []string

SectionsFor are the info sections the kind takes.

func SplitCommand added in v0.12.0

func SplitCommand(args []string) ([]string, error)

SplitCommand reads a command given as one argument ("GET foo") the way redis-cli reads a line: words split at spaces, "..." with \" \\ \n \r \t \xHH escapes, and '...' with \' only. Several arguments are kept.

Types

type DialFunc

type DialFunc func(ctx context.Context, network, addr string) (net.Conn, error)

DialFunc opens a TCP connection, possibly through an SSH host.

type KeyInfo added in v0.14.0

type KeyInfo struct {
	Key  string  `json:"key"`
	Type *string `json:"type"`        // the store's own type name; null = unknown
	TTL  *int64  `json:"ttl_seconds"` // -1 = no expiry; null = unknown
}

KeyInfo is a listed key with its type and time to live, never its value.

func Keys

func Keys(ctx context.Context, c redis.UniversalClient, pattern string, limit int) ([]KeyInfo, bool, error)

Keys lists keys matching pattern with SCAN (never KEYS), up to limit, with each key's type and time to live: TYPE and PTTL for the whole page go in one pipeline, so one round trip (one per master in a Cluster).

type Store added in v0.3.0

type Store interface {
	Get(ctx context.Context, key string, limit int64, maxBytes int) (*Value, error)
	Keys(ctx context.Context, pattern string, limit int) ([]KeyInfo, bool, error)
	Info(ctx context.Context, section string) (map[string]map[string]string, error)
	Set(ctx context.Context, key, value string, ttl time.Duration) error
	Exists(ctx context.Context, key string) (bool, error)
	Del(ctx context.Context, keys ...string) (int64, error)
	Run(ctx context.Context, args []string) (any, error)
	Close() error
}

Store is what the cache commands need from a cache server: Redis (and Valkey) or memcached.

func OpenStore added in v0.3.0

func OpenStore(ctx context.Context, t driver.Target, password string, dial DialFunc, timeout time.Duration) (Store, error)

OpenStore connects to a cache conn of either kind.

type Summary

type Summary struct {
	Version        string           `json:"version"`
	Mode           string           `json:"mode,omitempty"`
	Role           string           `json:"role"`
	UptimeSeconds  int64            `json:"uptime_seconds"`
	UsedMemory     int64            `json:"used_memory_bytes"`
	MaxMemory      int64            `json:"maxmemory_bytes"` // 0 = no limit
	EvictionPolicy string           `json:"eviction_policy,omitempty"`
	Clients        int64            `json:"connected_clients"`
	Keys           map[string]int64 `json:"keys"`     // per database: db0 -> count
	Expires        map[string]int64 `json:"expires"`  // keys with a time to live, per database
	Hits           int64            `json:"hits"`     // keyspace hits since start
	Misses         int64            `json:"misses"`   // keyspace misses since start
	HitRate        *float64         `json:"hit_rate"` // hits / (hits + misses); null before any read
}

Summary is the INFO reply boiled down to what people check first.

func Summarize

func Summarize(info map[string]map[string]string) Summary

Summarize picks the summary out of parsed INFO sections.

func SummarizeFor added in v0.3.0

func SummarizeFor(kind string, info map[string]map[string]string) Summary

SummarizeFor is Summarize for the kind.

type Value

type Value struct {
	Key       string `json:"key"`
	Type      string `json:"type"`
	TTL       *int64 `json:"ttl_seconds"` // null = no expiry
	Size      int64  `json:"size"`        // bytes for strings, items for collections
	Value     any    `json:"value"`
	Truncated bool   `json:"truncated,omitempty"`
}

Value is a key with its type, time to live and (part of) its content.

func Get

func Get(ctx context.Context, c redis.UniversalClient, key string, limit int64, maxBytes int) (*Value, error)

Get reads a key of any type. Collections return at most limit items.

func (*Value) Text

func (v *Value) Text() string

Text shows a key for people: what it is, when it expires, and its content one item per line.

Jump to

Keyboard shortcuts

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