httpcache

package
v0.5.1 Latest Latest
Warning

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

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

Documentation

Overview

Package httpcache caches provider responses on disk, so a screen you have already seen is drawn from the last answer while a fresh one is fetched.

It is an http.RoundTripper rather than a decorator around forge.Forge, which means it covers both providers and every endpoint without any call site knowing it exists. The alternative — a caching Forge — would need a hand-written key for each of forty methods, and would silently miss the next one added.

Nothing derived from a credential is ever written. Only response bodies and their validators are stored; the request's Authorization and token headers are neither stored nor part of a cache key. Authentication stays where gu has always kept it: in memory, delegated to the CLIs.

Index

Constants

This section is empty.

Variables

View Source
var ErrOffline = errors.New("not in the cache, and this lookup may not use the network")

ErrOffline is returned for a request that is not in the cache while the context forbids reaching the network.

Functions

func Clear

func Clear(dir string) error

Clear empties the cache directory. It is what `:cache clear` runs, and what anyone reaching for a fresh start expects to be able to do.

func Dir

func Dir(fallback string) string

Dir is where the cache lives, given gu's config directory as a fallback for systems with no user cache directory of their own.

func IsOffline

func IsOffline(ctx context.Context) bool

IsOffline reports whether ctx forbids the network.

func Offline

func Offline(ctx context.Context) context.Context

Offline returns a context whose requests are served from the cache or fail at once. It is how a screen draws itself before fetching anything: the same load function, run against the disk.

Types

type Transport

type Transport struct {
	// Base is the round-tripper doing the real work; nil means
	// http.DefaultTransport.
	Base http.RoundTripper
	// Dir is where entries live. An empty Dir disables the cache entirely,
	// which is what a configuration that switched it off resolves to.
	Dir string
	// Scope separates one account's entries from another's. Two profiles
	// routinely share a host and see different things there, so the profile
	// label is part of every key — it is a name, never a token.
	Scope string
	// MaxAge bounds how old an entry may be and still be served without asking
	// the provider. Beyond it the entry is still a fine revalidation base.
	MaxAge time.Duration
}

Transport caches GET responses and revalidates them with If-None-Match.

func (*Transport) Purge

func (t *Transport) Purge()

Purge drops every entry, which is what a write makes necessary: gu cannot know which listings a comment or a merge has just invalidated, and a stale list after a change you made yourself is the one staleness nobody forgives.

func (*Transport) RoundTrip

func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip serves req from the cache where it can, and records what it could not.

Jump to

Keyboard shortcuts

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