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
- func Bypass(ctx context.Context) context.Context
- func IsBypass(ctx context.Context) bool
- func Key(kind Kind, parts ...string) string
- type Cache
- func (c *Cache) Clear() (int64, error)
- func (c *Cache) Close() error
- func (c *Cache) Delete(kind Kind, key string) error
- func (c *Cache) Enabled() bool
- func (c *Cache) Get(kind Kind, key string) ([]byte, bool)
- func (c *Cache) Put(kind Kind, key string, body []byte) error
- func (c *Cache) Stats() Stats
- func (c *Cache) Sweep() (int64, error)
- func (c *Cache) TTL(kind Kind) time.Duration
- type Kind
- type Options
- type Stats
Constants ¶
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.
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 ¶
Bypass marks ctx so reads are skipped while writes still happen: "give me a live answer, and refresh what you keep for next time".
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 ¶
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) Close ¶
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) Get ¶
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 ¶
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.
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.
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.