cache

package
v0.14.2 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package cache is a tiny per-profile JSON file store for Jira metadata (labels, epics, projects, …) that's cheap to look up — used by the `jira cache <resource>` commands and (eventually) by Cobra shell completion functions.

The store is intentionally dumb: one JSON file per resource, atomic write, read-time freshness check. No locking — concurrent writers would race, but in practice each profile is driven by one user shell.

Index

Constants

View Source
const DefaultTTL = 1 * time.Hour

DefaultTTL is the freshness window before `Read` reports stale=true. Callers can still use the cached value; this just signals "consider refreshing" to commands and completion functions.

View Source
const IssueKeysResource = "issuekeys"

IssueKeysResource names the per-profile MRU list of recently used issue keys. Unlike the fetched metadata resources it is never primed from Jira: commands write it as a side effect of touching keys, and shell completion reads it, so freshness windows do not apply — the newest entry is by definition current.

View Source
const SchemaVersion = 1

SchemaVersion is the on-disk cache-entry shape version. Any change to a cached resource's shape bumps this constant; every entry stamped with an older version then fails the read-time check below and is refetched, so a CLI upgrade can never mis-parse a stale shape. This is what lets the per-resource TTLs run long. Version 1 added status_category to cached statuses (version 0 was the originally-unversioned shape; entries written before the field existed decode to Schema=0 and are refetched).

Variables

This section is empty.

Functions

func Clear

func Clear(profile, resource string) (bool, error)

Clear removes the cache file for (profile, resource). Returns ok=true when a file existed and was removed; ok=false silently when none.

func ClearProfile

func ClearProfile(profile string) (int, error)

ClearProfile wipes every cache file under a profile (the parent directory). Returns the number of files removed.

func CountProfile added in v0.10.0

func CountProfile(profile string) (int, error)

CountProfile reports how many cache files a ClearProfile would remove, without removing anything. The dry-run half of a whole-profile `cache clear`.

func Exists added in v0.10.0

func Exists(profile, resource string) (bool, error)

Exists reports whether a cache file is present for (profile, resource) — what a Clear would remove — without touching it. The dry-run half of `cache clear <resource>`. Like every xos boolean probe it treats a regular file where a directory should be (ENOTDIR mid-path) as absent, so on a hand-corrupted cache tree the preview says "nothing to remove" where the live Clear surfaces the error.

func IssueKeys added in v0.10.15

func IssueKeys(profile string) []string

IssueKeys returns the profile's recently used issue keys, newest first. Any miss — no cache, unreadable file, wrong shape — returns nil so completion and rendering degrade to nothing rather than erroring.

func Key

func Key(profile, siteURL, configPath string) string

Key returns the stable namespace component for one config/site/profile identity. The human profile name stays visible in command output; this key is only for cache storage so profiles with the same name in different Jira sites or config files do not share metadata.

func Path

func Path(profile, resource string) (string, error)

Path returns the on-disk location for a (profile, resource) pair. The directory is created lazily; callers should treat the returned path purely as input to Read/Write.

func RecordIssueKeys added in v0.10.15

func RecordIssueKeys(profile string, keys []string) error

RecordIssueKeys merges keys into the profile's most-recent-first list: incoming keys (already normalized by the caller's parse) move to or enter the front in the order given, duplicates collapse onto their newest position, and the tail truncates at the cap. Recording is a side effect of real work — callers ignore the error by design, so a broken cache can never fail a command.

Types

type Entry

type Entry struct {
	Profile   string          `json:"profile"`
	Resource  string          `json:"resource"`
	Schema    int             `json:"schema"`
	FetchedAt time.Time       `json:"fetched_at"`
	Data      json.RawMessage `json:"data"`
}

Entry wraps a cached value with its fetch timestamp + source profile. Stored verbatim on disk so consumers can introspect age and provenance.

func Read

func Read(profile, resource string, ttl time.Duration) (entry Entry, ok, stale bool, err error)

Read returns the cached entry. ok=false when no cache file exists. stale=true when the entry is older than ttl (or DefaultTTL when ttl<=0) — the value is still returned so callers can use stale data as a fallback while triggering a refresh.

func ReadCachedOrEmpty added in v0.4.1

func ReadCachedOrEmpty(profile, resource string) (Entry, bool, error)

ReadCachedOrEmpty returns the cached entry for (profile, resource) regardless of age, or ok=false when no usable entry exists — absent, unreadable, or written by an incompatible schema. It is the NeverBlock read: consumers that must serve cached-or-empty and never trigger a network fetch (shell completion, JQL field reference, board-scope resolution) call this, so cache age is irrelevant to them and a long resource TTL never changes what they see. The read error is still returned, so a caller that distinguishes "absent" from "broken" can; completion-style callers may treat any (!ok || err != nil) as empty.

func Write

func Write(profile, resource string, data json.RawMessage) (Entry, error)

Write atomically stores `data` (already JSON-encoded) under the (profile, resource) key. xos.AtomicWrite's temp-file-then-rename avoids leaving a half-written file when the process is killed mid-write.

Jump to

Keyboard shortcuts

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