Help us shape the future of Go! Take the 2026 Go Developer Survey

clicache

package
v1.3.104 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package clicache pkg/clicache/clicache.go c4-vis-cli `skywire cli` commands use to memoize deployment-service fetches (SD `/api/services`, TPD `/all-transports`, UT `/uptimes`, etc.) for the CLI's --cfa minutes-old window.

Replaces the older per-URL JSON-file cache in `/tmp/<service>/...`, which had two problems:

  • umask silently downgraded the intended 0o777/0o666 modes, so a CLI run as one user couldn't write into a dir created by a CLI run as another user (typically root via the visor service).
  • Each cached endpoint was its own file on disk; no way to scope or evict in one operation.

The bbolt approach: one DB file at $XDG_CACHE_HOME/skywire/cli-fetch.db (default ~/.cache/skywire/cli-fetch.db). System-wide installs that want a shared cache across users can set SKYWIRE_CLI_CACHE_DB to /var/cache/skywire/cli-fetch.db (or similar) and ensure the file is 0o666 with a 0o777 parent — both are set explicitly by Open.

Entries are keyed by URL inside a per-service-host bucket so a future cache-clear can target a single service. Values are framed as JSON `{"body": "...", "fetched_at": "..."}` so a `bbolt dump` or `strings(1)` produces something legible.

Index

Constants

View Source
const DefaultFilename = "cli-fetch.db"

DefaultFilename is the bbolt file's basename under the cache dir.

Variables

This section is empty.

Functions

func DefaultPath

func DefaultPath() (string, error)

DefaultPath returns the path the CLI should use for its cache db, resolved from (in order): $SKYWIRE_CLI_CACHE_DB, $XDG_CACHE_HOME/skywire/<DefaultFilename>, $HOME/.cache/skywire/<DefaultFilename>.

Types

type Cache

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

Cache is a thin wrapper around a bbolt.DB scoped to the CLI's service-fetch use case. The zero value is unusable — use Open.

func Open

func Open(path string) (*Cache, error)

Open creates or opens the cache DB at path. The parent directory is created if missing. Both the directory and the DB file are explicitly chmodded to 0o777 / 0o666 so a root-owned DB remains writable by other users — this is what the older per-URL file cache failed to do (umask downgrade).

The wide permissions are deliberate: when the visor runs as root (e.g. via the systemd service) and the operator runs the CLI as their own user, both must be able to write the cache or the CLI degenerates back into per-user files. The cache contents are public deployment-service responses, so wide read perms aren't a disclosure risk. gosec G301/G302 warnings are silenced for the same reason.

A 2s open timeout matches the rest of the codebase's bbolt usage and prevents the CLI from hanging if another process holds the lock.

func (*Cache) Close

func (c *Cache) Close() error

Close releases the underlying bbolt DB. Safe to call on a nil receiver so callers can `defer c.Close()` without nil-checks.

func (*Cache) Fresh

func (c *Cache) Fresh(rawURL string, maxAge time.Duration) (Entry, bool)

Fresh is a convenience wrapper that returns the entry only if it's been fetched within maxAge. Equivalent to Get + age check.

func (*Cache) Get

func (c *Cache) Get(rawURL string) (Entry, bool)

Get returns the cached entry for rawURL, or ok=false on miss. Returns ok=false on any decode error — caller treats it as a fresh-fetch trigger rather than propagating a partial value.

func (*Cache) Path

func (c *Cache) Path() string

Path returns the on-disk location of the DB.

func (*Cache) Put

func (c *Cache) Put(rawURL string, body []byte) error

Put stores body under rawURL with the current time as the fetched_at timestamp.

type Entry

type Entry struct {
	Body      []byte    `json:"body"`
	FetchedAt time.Time `json:"fetched_at"`
}

Entry is the value stored under each URL key.

Jump to

Keyboard shortcuts

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