cache

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package cache is the in-process cache for rendered HTML, sitemaps and site data. Entries live in memory with a TTL and are lost on restart.

Index

Constants

View Source
const (
	// TTLPublicPage is the TTL for public page data (5 minutes).
	TTLPublicPage = 5 * time.Minute

	// TTLMenuData is the TTL for menu data (1 hour).
	TTLMenuData = 1 * time.Hour

	// TTLGeoSchema is the TTL for GEO schema data (1 hour).
	TTLGeoSchema = 1 * time.Hour

	// TTLSiteData is the TTL for site metadata (1 hour).
	TTLSiteData = 1 * time.Hour

	// TTLSitemap is the TTL for sitemap XML data (1 hour).
	TTLSitemap = 1 * time.Hour

	// TTLDefault is the default TTL for cached items.
	TTLDefault = 5 * time.Minute
)

TTL constants for different cache types.

Variables

View Source
var ErrCacheMiss = errors.New("cache miss")

ErrCacheMiss is returned by Get when the key is absent or expired.

Functions

func EntityJSONLDKey

func EntityJSONLDKey(entityID uuid.UUID) string

EntityJSONLDKey generates a cache key for entity JSON-LD.

func EntityPattern

func EntityPattern(entityID uuid.UUID) string

EntityPattern returns a pattern to match all keys for a specific entity.

func GeoImplementationKey

func GeoImplementationKey(implID uuid.UUID) string

GeoImplementationKey generates a cache key for a GEO implementation.

func GeoPattern

func GeoPattern() string

GeoPattern returns a pattern to match all GEO keys.

func GeoSchemaKey

func GeoSchemaKey(schemaType string) string

GeoSchemaKey generates a cache key for a GEO schema type.

func GetOrSet

func GetOrSet[T any](ctx context.Context, c *Cache, key string, ttl time.Duration, compute func() (T, error)) (T, error)

GetOrSet retrieves a value from cache, or computes and stores it if not found. This is a convenience method for the cache-aside pattern.

func MenuByIDKey(menuID uuid.UUID) string

MenuByIDKey generates a cache key for a menu by ID.

func MenuKey(location string) string

MenuKey generates a cache key for a menu.

func MenuPattern() string

MenuPattern returns a pattern to match all menu keys.

func PublicArchiveKey

func PublicArchiveKey(schema string, page, perPage int) string

PublicArchiveKey generates a cache key for a public archive.

func PublicHomepageKey

func PublicHomepageKey() string

PublicHomepageKey generates a cache key for the homepage.

func PublicPageKey

func PublicPageKey(slug string) string

PublicPageKey generates a cache key for a public page.

func PublicPattern

func PublicPattern() string

PublicPattern returns a pattern to match all public page keys.

func RenderedPageKey

func RenderedPageKey(scope, path string) string

RenderedPageKey generates the cache key for a server-rendered HTML page. scope is the engine's content hash, so a theme change never serves stale markup (feeds pass their base URL); path identifies the page.

func SiteDataKey

func SiteDataKey() string

SiteDataKey generates a cache key for site metadata.

func SitemapKey

func SitemapKey(sub string) string

SitemapKey generates a cache key for a sitemap sub-page. The sub parameter identifies the sub-sitemap (e.g. "index", "blog").

func SitemapPattern

func SitemapPattern() string

SitemapPattern returns a pattern to match all sitemap keys.

func TenantPattern

func TenantPattern() string

TenantPattern returns a pattern to match every cache key. Retained as a name for historical clarity; post-Path-B it has no tenant dimension.

Types

type Cache

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

Cache is a concurrency-safe in-memory TTL cache.

func New

func New(logger *slog.Logger) *Cache

New creates an empty cache.

func (*Cache) Delete

func (c *Cache) Delete(_ context.Context, keys ...string) error

Delete removes values from the cache.

func (*Cache) DeletePattern

func (c *Cache) DeletePattern(_ context.Context, pattern string) error

DeletePattern removes every key matching a glob pattern where "*" matches any run of characters.

func (*Cache) Evictions

func (c *Cache) Evictions() uint64

Evictions counts cap evictions (batches, and values refused for being larger than the cap). It never decreases.

func (*Cache) Generation

func (c *Cache) Generation() uint64

Generation returns a counter that every Delete and DeletePattern advances. Read it before computing a value from the database and hand it to SetIfGeneration.

func (*Cache) Get

func (c *Cache) Get(_ context.Context, key string) ([]byte, error)

Get retrieves a value; expired entries count as misses.

func (*Cache) GetJSON

func (c *Cache) GetJSON(ctx context.Context, key string, dest any) error

GetJSON retrieves and unmarshals a JSON value from the cache.

func (*Cache) RenderMaxBytes

func (c *Cache) RenderMaxBytes() int64

RenderMaxBytes returns the current cap (0 = no limit).

func (*Cache) RenderStats

func (c *Cache) RenderStats() (pages int, bytes int64)

RenderStats returns the number of cached "render:" entries and the sum of their value sizes. Rendered pages and feeds share the prefix (feeds.go uses RenderedPageKey too), so "pages" includes feeds; sitemaps and JSON keys are not counted.

func (*Cache) Set

func (c *Cache) Set(_ context.Context, key string, value []byte, ttl time.Duration) error

Set stores a value with the given TTL. Expired entries are swept every sweepEvery writes, and when the cache is full arbitrary entries are evicted to make room (it is a cache; an evicted entry is just a miss).

func (*Cache) SetIfGeneration

func (c *Cache) SetIfGeneration(_ context.Context, key string, value []byte, ttl time.Duration, gen uint64) (bool, error)

SetIfGeneration is Set, skipped (false) when a delete ran since gen was read. A render that started before a purge may hold data from before the change; storing it would keep the stale page for the whole TTL.

func (*Cache) SetJSON

func (c *Cache) SetJSON(ctx context.Context, key string, value any, ttl time.Duration) error

SetJSON marshals and stores a JSON value in the cache.

func (*Cache) SetRenderMaxBytes

func (c *Cache) SetRenderMaxBytes(n int64)

SetRenderMaxBytes caps the bytes held by "render:" entries; 0 means no limit. A lower cap takes effect on the next render write.

type CacheInvalidator

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

CacheInvalidator provides methods for invalidating cache entries.

func NewCacheInvalidator

func NewCacheInvalidator(cache *Cache, logger *slog.Logger) *CacheInvalidator

NewCacheInvalidator creates a new cache invalidator.

func (*CacheInvalidator) InvalidateEntity

func (ci *CacheInvalidator) InvalidateEntity(ctx context.Context, entityID uuid.UUID, slug string) error

InvalidateEntity invalidates all cache entries related to an entity.

func (*CacheInvalidator) InvalidateGeoSchema

func (ci *CacheInvalidator) InvalidateGeoSchema(ctx context.Context) error

InvalidateGeoSchema invalidates all GEO schema cache entries.

func (*CacheInvalidator) InvalidateMenu

func (ci *CacheInvalidator) InvalidateMenu(ctx context.Context) error

InvalidateMenu invalidates all menu-related cache entries.

func (*CacheInvalidator) InvalidateRendered

func (ci *CacheInvalidator) InvalidateRendered(ctx context.Context) error

InvalidateRendered drops every cached rendered page. Pages embed navigation and sibling data, so any content, menu or site-config change invalidates the whole set (coarse on purpose).

func (*CacheInvalidator) InvalidateSitemaps

func (ci *CacheInvalidator) InvalidateSitemaps(ctx context.Context) error

InvalidateSitemaps drops every cached sitemap (index and children).

func (*CacheInvalidator) InvalidateTenant

func (ci *CacheInvalidator) InvalidateTenant(ctx context.Context) error

InvalidateTenant invalidates every cache entry. The name is retained from the multi-tenant era for clarity at call sites — post-Path-B it is functionally equivalent to "clear the whole cache".

Jump to

Keyboard shortcuts

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