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

webcache

package
v0.58.1 Latest Latest
Warning

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

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

Documentation

Overview

Package webcache keeps public web content — search results, fetched pages and deep-research answers — so a repeated question costs no network call and no LLM round trip.

It is deliberately two-level: an in-process LRU in front of a SQLite table in the global config database. The memory level makes a repeat within one turn free; the disk level is what makes the same query fast on the NEXT turn, after a restart or from a different session.

Only successful, non-empty results are ever stored — that decision belongs to the caller (see internal/search/cache.go for the backend decorator, and the deep-search tool for answers), because only the caller knows what a failure looks like for its own kind.

Index

Constants

View Source
const (
	DefaultSearchTTL = time.Hour
	DefaultPageTTL   = 24 * time.Hour
	DefaultAnswerTTL = 6 * time.Hour
)

Default TTLs per kind. Search rankings move fastest and are the cheapest to redo; a page's body text is stable for a day; an answer is the expensive artefact and is worth a working session.

View Source
const (
	DefaultL1Entries = 512
	DefaultL1Bytes   = 64 << 20
	DefaultL2Rows    = 5000
	DefaultL2Bytes   = 256 << 20
)

Default capacities. Both levels are bounded — an unbounded cache in a long-lived server is a leak, which is exactly what the backend-local caches this replaces had.

Variables

This section is empty.

Functions

func Bypass

func Bypass(ctx context.Context) context.Context

Bypass marks ctx so reads are skipped while writes still happen: "give me a live answer, and refresh what you keep for next time".

func IsBypass

func IsBypass(ctx context.Context) bool

IsBypass reports whether ctx was marked by Bypass.

func Key

func Key(kind Kind, parts ...string) string

Key hashes a kind and its canonical parts into the opaque key stored in the table. Callers never store the raw query or URL — the digest is all that is persisted, so the cache cannot leak what someone searched for.

Types

type Cache

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

Cache is a memory LRU in front of the web_cache table. Every method tolerates a nil receiver, so a caller can carry "caching is off" as a nil pointer instead of branching at each site.

func Open

func Open(db *sql.DB, opts Options) (*Cache, error)

Open returns a cache over db. The web_cache table is created by migration 074, which runs when the database is opened — Open does not create it.

func (*Cache) Clear

func (c *Cache) Clear() (int64, error)

Clear empties both levels and reports how many disk rows it removed.

func (*Cache) Close

func (c *Cache) Close() error

Close releases nothing — the cache does not own the database handle it was opened over — but exists so callers can defer it beside the server's other shutdown work.

func (*Cache) Delete

func (c *Cache) Delete(kind Kind, key string) error

Delete removes one entry from both levels.

func (*Cache) Enabled

func (c *Cache) Enabled() bool

Enabled reports whether reads and writes are live.

func (*Cache) Get

func (c *Cache) Get(kind Kind, key string) ([]byte, bool)

Get returns the stored body for kind/key. The slice is a copy, so a caller may keep or mutate it without corrupting the cache.

func (*Cache) Put

func (c *Cache) Put(kind Kind, key string, body []byte) error

Put stores body under kind/key with that kind's TTL. An empty body is never stored; a write failure is returned but leaves the memory level populated.

func (*Cache) Stats

func (c *Cache) Stats() Stats

Stats reads the disk level and the in-process counters.

func (*Cache) Sweep

func (c *Cache) Sweep() (int64, error)

Sweep drops expired rows, the memory entries that went with them, and trims the table back inside its budget. It is cheap enough to run on a ticker.

func (*Cache) TTL

func (c *Cache) TTL(kind Kind) time.Duration

TTL returns the lifetime configured for kind.

type Kind

type Kind string

Kind separates the three payload shapes that share the table. It is part of the row key, so the kinds can never collide.

const (
	// KindSearch is a provider's ranked result list for one query.
	KindSearch Kind = "search"
	// KindPage is the extracted text of one URL.
	KindPage Kind = "page"
	// KindAnswer is a synthesised deep-research answer.
	KindAnswer Kind = "answer"
)

type Options

type Options struct {
	Enabled   bool
	TTL       map[Kind]time.Duration
	L1Entries int
	L1Bytes   int
	L2Rows    int64
	L2Bytes   int64
	Now       func() time.Time
}

Options configures a Cache. Zero values take the defaults above; a nil Now takes time.Now. TTL may be partial — a kind missing from the map falls back to its default.

type Stats

type Stats struct {
	Entries   int64
	Bytes     int64
	Hits      int64
	Misses    int64
	Evictions int64
}

Stats reports what the cache holds and how it is doing. Entries and Bytes count the disk level (the durable one); Hits and Misses count every Get.

Jump to

Keyboard shortcuts

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