cache

package
v1.21.21 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package cache defines clicky's generic cache-browser contract: a read/admin API over a key-value cache keyspace (valkey/redis or anything shaped like it) that groups keys into a prefix tree, serves per-key detail, search, whole-keyspace stats, and key/prefix deletion.

The package mirrors the metrics split: the interface plus the HTTP handler live here in the root module, while backend implementations live in their own modules (github.com/flanksource/clicky/valkey) so the root module never pulls in client dependencies.

Index

Constants

This section is empty.

Variables

View Source
var ErrKeyNotFound = errors.New("cache: key not found")

ErrKeyNotFound is returned by Browser.Key when the key does not exist. The HTTP handler maps it to a 404.

Functions

func Handler

func Handler(b Browser, prefix string) http.Handler

Handler returns the cache-browser endpoints as a standalone http.Handler for callers that compose their own mux.

func RegisterRoutes

func RegisterRoutes(mux *http.ServeMux, b Browser, prefix string)

RegisterRoutes mounts the cache-browser endpoints on mux under prefix:

GET    {prefix}/cache/tree?prefix=&max=
GET    {prefix}/cache/key?key=
GET    {prefix}/cache/search?q=&limit=
GET    {prefix}/cache/stats
DELETE {prefix}/cache/key?key=
DELETE {prefix}/cache/prefix?prefix=

Keys travel in query parameters, not path segments, because cache keys routinely contain ":" and "/". prefix is the leading path segment shared with the rest of the API (e.g. "/api/v1"); pass it without a trailing slash.

Types

type Browser

type Browser interface {
	Tree(ctx context.Context, req TreeRequest) (TreeResponse, error)
	Key(ctx context.Context, key string) (KeyDetail, error)
	Search(ctx context.Context, req SearchRequest) (SearchResponse, error)
	Stats(ctx context.Context) (Stats, error)
	DeleteKey(ctx context.Context, key string) (DeleteResponse, error)
	DeletePrefix(ctx context.Context, prefix string) (DeleteResponse, error)
}

Browser is the backend contract the HTTP handler serves. All keys and prefixes are logical: implementations strip any physical namespace prefix before returning and re-apply it on lookups.

type DeleteResponse

type DeleteResponse struct {
	Deleted int64 `json:"deleted"`
}

DeleteResponse reports how many keys a delete removed.

type KeyDetail

type KeyDetail struct {
	Key        string `json:"key"`
	Type       string `json:"type"`
	TTLSeconds int64  `json:"ttlSeconds"`
	// Bytes is MEMORY USAGE when supported, otherwise omitted.
	Bytes int64 `json:"bytes,omitempty"`
	// Length is the full value length: STRLEN, HLEN, LLEN, SCARD or ZCARD.
	Length  int64             `json:"length"`
	Value   string            `json:"value,omitempty"`
	Fields  map[string]string `json:"fields,omitempty"`
	Items   []string          `json:"items,omitempty"`
	Members []ScoredMember    `json:"members,omitempty"`
	// Truncated is set when the returned value was capped (string bytes or
	// collection items); Length still reports the full size.
	Truncated bool `json:"truncated,omitempty"`
}

KeyDetail is the full detail for a single key. Exactly one of Value, Fields, Items, Members is populated according to Type.

type ScoredMember

type ScoredMember struct {
	Member string  `json:"member"`
	Score  float64 `json:"score"`
}

ScoredMember is one zset member with its score.

type SearchRequest

type SearchRequest struct {
	Query string
	// Limit caps the number of returned keys. Zero uses the backend default.
	Limit int
}

SearchRequest is a substring search over the whole keyspace.

type SearchResponse

type SearchResponse struct {
	Keys      []TreeNode `json:"keys"`
	Truncated bool       `json:"truncated,omitempty"`
}

SearchResponse lists matching keys as leaf nodes.

type Stats

type Stats struct {
	Keys int64 `json:"keys"`
	// KeysTruncated is set when Keys was counted by a capped scan (namespaced
	// keyspaces) and is therefore a lower bound.
	KeysTruncated    bool   `json:"keysTruncated,omitempty"`
	UsedMemoryBytes  int64  `json:"usedMemoryBytes,omitempty"`
	MaxMemoryBytes   int64  `json:"maxMemoryBytes,omitempty"`
	Hits             int64  `json:"hits,omitempty"`
	Misses           int64  `json:"misses,omitempty"`
	EvictedKeys      int64  `json:"evictedKeys,omitempty"`
	ExpiredKeys      int64  `json:"expiredKeys,omitempty"`
	ConnectedClients int64  `json:"connectedClients,omitempty"`
	Version          string `json:"version,omitempty"`
	UptimeSeconds    int64  `json:"uptimeSeconds,omitempty"`
	InfoError        string `json:"infoError,omitempty"`
}

Stats is a whole-keyspace overview. Keys is always populated; the server-info fields are populated only when the backend's INFO command succeeded — when it failed or is unsupported, InfoError carries the reason so the caller can render the gap honestly instead of showing zeros.

type TreeNode

type TreeNode struct {
	// Name is the display segment: the next path segment for groups, the
	// remaining key tail for leaves.
	Name string `json:"name"`
	// Prefix is set on group nodes only: the full logical prefix including
	// the trailing separator, ready to feed back into TreeRequest.Prefix.
	Prefix string `json:"prefix,omitempty"`
	// Key is set on leaf nodes only: the full logical key.
	Key string `json:"key,omitempty"`
	// Keys is the total number of keys under this node (1 for a leaf).
	Keys int `json:"keys"`
	// Children is the number of distinct child segments under a group node.
	Children int `json:"children,omitempty"`
	// Type is the value type of a leaf: string|hash|list|set|zset|stream.
	Type string `json:"type,omitempty"`
	// TTLSeconds is the leaf's remaining TTL; -1 means no expiry.
	TTLSeconds int64 `json:"ttlSeconds,omitempty"`
	// Bytes is the MEMORY USAGE of the node when the server supports it: the
	// leaf's own size, or for a group the aggregated size of every key
	// beneath it.
	Bytes int64 `json:"bytes,omitempty"`
}

TreeNode is one entry in a tree level: either a group of keys sharing the next path segment, or a single leaf key.

type TreeRequest

type TreeRequest struct {
	// Prefix is the logical prefix to expand, e.g. "tx:". Empty expands the
	// root. It must include the trailing separator when pointing at a group.
	Prefix string
	// MaxChildren caps the number of nodes returned for the level. Zero uses
	// the backend default.
	MaxChildren int
}

TreeRequest asks for one level of the key tree under Prefix. Keys are split on the backend's separator (":" for valkey); each distinct next segment becomes either a group node (more segments follow) or a leaf (an actual key).

type TreeResponse

type TreeResponse struct {
	Prefix string     `json:"prefix"`
	Nodes  []TreeNode `json:"nodes"`
	// Keys is the total number of keys scanned under Prefix, including keys
	// rolled up into group nodes.
	Keys int `json:"keys"`
	// Truncated is set when the node list or the underlying scan hit a cap;
	// counts are then lower bounds.
	Truncated bool `json:"truncated,omitempty"`
	// BytesSupported reports whether per-key byte sizes were available
	// (MEMORY USAGE); when false every Bytes field is absent, not zero.
	BytesSupported bool `json:"bytesSupported"`
}

TreeResponse is one expanded tree level.

Jump to

Keyboard shortcuts

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