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 ¶
- Variables
- func Classify(args []string) driver.Verdict
- func ClassifyFor(kind string, args []string) driver.Verdict
- func ClassifyMemcache(args []string) driver.Verdict
- func ClusterWideProblem(args []string) error
- func HideSecrets(args []string, v any) any
- func Info(ctx context.Context, c redis.UniversalClient, section string) (map[string]map[string]string, error)
- func MemcacheRules() []driver.Rule
- func MemcacheWords(args []string) ([]string, error)
- func OneReply(args []string) error
- func Open(ctx context.Context, t driver.Target, password string, dial DialFunc, ...) (redis.UniversalClient, error)
- func Quote(s string) string
- func ReplyText(v any) string
- func Rules() []driver.Rule
- func Run(ctx context.Context, c redis.UniversalClient, args []string) (any, error)
- func SectionsFor(kind string) []string
- func SplitCommand(args []string) ([]string, error)
- type DialFunc
- type KeyInfo
- type Store
- type Summary
- type Value
Constants ¶
This section is empty.
Variables ¶
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.
var MemcacheSections = []string{"items", "slabs", "settings", "sizes", "conns"}
MemcacheSections are the sections `stats` takes ("" is the general one).
Functions ¶
func ClassifyFor ¶ added in v0.3.0
ClassifyFor rates a raw command for the conn's kind.
func ClassifyMemcache ¶ added in v0.3.0
ClassifyMemcache rates a memcached command line.
func ClusterWideProblem ¶ added in v0.13.0
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
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
MemcacheRules are the rules ClassifyMemcache can report.
func MemcacheWords ¶ added in v0.13.0
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
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 ¶
Quote shows s on one line: as it is when that is unambiguous, else in double quotes with \n, \t, \" and \xNN escapes.
func SectionsFor ¶ added in v0.3.0
SectionsFor are the info sections the kind takes.
func SplitCommand ¶ added in v0.12.0
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 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.
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.
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.