attachcache

package
v0.24.3 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: Apache-2.0 Imports: 13 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 attachcache stores attachment bytes on local disk.

Attachment bytes were proxied from Jira on every view, which contradicts the premise of the tool: everything else answers from local disk, and an issue with three screenshots re-downloaded them on every open. Bytes for a given attachment id are immutable in Jira, so they cache indefinitely and the only bound needed is total size.

A cached attachment also survives having no credential at all, which is what lets the bundled demo snapshot show real images offline.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Key added in v0.13.0

func Key(site, profile, issue, id string) string

Key is the on-disk identity for an attachment. Site (and issue) are mixed in so a profile that changes sites cannot serve the previous site's bytes, and a request for the wrong issue cannot read a cached id.

An empty site keeps the legacy id-only form so snapshot imports for `gadak demo` / export-static (no site) stay reachable. Existing id-only files on a real site miss after this change (the safe invalidation).

func Tag added in v0.22.0

func Tag(key string) string

path derives a filename from the cache key by hashing it. The key is attachcache.Key(site, profile, issue, id). Hashing keeps a hostile key from escaping the directory. Tag is an opaque, stable identity for a cache key — the same hash the filename uses. A caller that needs a validator wants this, not the key itself: the key is `<site>\x1f<profile>\x1f<issue>\x1f<id>`, and putting that in an ETag published the Atlassian hostname and the workspace name in an HTTP response header, which a paired serve hands to anything on the tailnet that can reach it (GDK-1621).

func TooLarge

func TooLarge(err error) bool

TooLarge reports whether err means the entry was skipped for its size.

Types

type Cache

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

Cache is a content-addressed store under one directory. Safe for concurrent use; a miss for the same id collapses into a single fetch.

func New

func New(dir string, maxBytes, maxEntry int64) (*Cache, error)

New opens (and creates) a cache directory. maxBytes <= 0 means defaultMaxBytes. New opens the cache. maxBytes bounds the directory, maxEntry the largest single file it will keep; either 0 takes the package default.

func (*Cache) Dir

func (c *Cache) Dir() string

Dir is the directory the cache owns.

func (*Cache) Fill

func (c *Cache) Fill(id string, fetch func() (io.ReadCloser, Meta, error)) error

Fill is FillOrStream for callers with nowhere to stream to (the warm path): an entry too large to keep is reported as TooLarge and its body discarded.

func (*Cache) FillOrStream added in v0.22.0

func (c *Cache) FillOrStream(id string, fetch func() (io.ReadCloser, Meta, error)) (io.ReadCloser, Meta, error)

FillOrStream is the single-flight write path, and it never asks a caller to fetch the same bytes twice (GDK-1616). fetch runs at most once per call — at most once per id when ten renders miss at the same moment — and the three outcomes are distinct:

  • (nil, meta, nil): the bytes are on disk. Serve them with Get.
  • (rc, meta, nil): too large to keep. rc is the whole object, exactly as the origin sent it, including any bytes this call had already staged. The caller streams it and closes it.
  • (nil, _, err): the fetch or the write failed.

The returned body is why a request can no longer fetch twice: the size verdict arrives with the bytes attached, so there is nothing left to go back to the origin for.

fetch must return the body, its content type, and its length (0 if unknown).

func (*Cache) Get

func (c *Cache) Get(id string) (io.ReadSeekCloser, Meta, error)

Get returns an open reader for a cached attachment. The caller closes it. A miss returns os.ErrNotExist.

func (*Cache) Has

func (c *Cache) Has(id string) bool

Has reports whether an attachment is cached, without opening it.

func (*Cache) ImportFile

func (c *Cache) ImportFile(key, path, contentType, filename string) error

ImportFile seeds an entry from a local file under the given cache key. Callers that know site/profile/issue must pass Key(...); do not pass a raw id when the site is set — that is the snapshot-import miss that D9 closed.

func (*Cache) ImportManifest added in v0.13.0

func (c *Cache) ImportManifest(dir, site, profile string, issueForID func(id string) (issue string, ok bool)) (ImportStats, error)

ImportManifest seeds the cache from a fixture directory's manifest.json. Each file is stored under Key(site, profile, issue, id). issueForID must return the issue key that owns id; ids it rejects are skipped — they are never written under the raw id (that would reopen the D9 cross-site hole).

func (*Cache) MaxEntry added in v0.22.0

func (c *Cache) MaxEntry() int64

MaxEntry is the largest object this cache will keep, in bytes. A caller with a recorded size can ask before fetching whether an object could ever be cached — which is how a ranged request decides between filling the cache and streaming past it (GDK-1616).

func (*Cache) MissReason added in v0.13.0

func (c *Cache) MissReason(key, id string) string

MissReason explains a cache miss so a log can tell a key-scope mismatch (legacy id-only file still on disk) from a true absence. It never serves the legacy entry.

func (*Cache) Stats

func (c *Cache) Stats() (files int, bytes int64)

Stats reports the cache footprint, for the settings panel and `gadak status`.

type ImportStats added in v0.13.0

type ImportStats struct {
	Seeded     int
	SkippedIDs []string
}

ImportStats is what a snapshot import reports: how many files landed, and which manifest ids were skipped (not in the mirror).

type Meta

type Meta struct {
	ContentType string `json:"content_type"`
	Size        int64  `json:"size"`
	Filename    string `json:"filename,omitempty"`
}

Meta is the sidecar recorded next to each cached file. Content-Type has to survive a restart, and guessing it back from bytes is worse than storing it.

Jump to

Keyboard shortcuts

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