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
- Variables
- func EntityJSONLDKey(entityID uuid.UUID) string
- func EntityPattern(entityID uuid.UUID) string
- func GeoImplementationKey(implID uuid.UUID) string
- func GeoPattern() string
- func GeoSchemaKey(schemaType string) string
- func GetOrSet[T any](ctx context.Context, c *Cache, key string, ttl time.Duration, ...) (T, error)
- func MenuByIDKey(menuID uuid.UUID) string
- func MenuKey(location string) string
- func MenuPattern() string
- func PublicArchiveKey(schema string, page, perPage int) string
- func PublicHomepageKey() string
- func PublicPageKey(slug string) string
- func PublicPattern() string
- func RenderedPageKey(scope, path string) string
- func SiteDataKey() string
- func SitemapKey(sub string) string
- func SitemapPattern() string
- func TenantPattern() string
- type Cache
- func (c *Cache) Delete(_ context.Context, keys ...string) error
- func (c *Cache) DeletePattern(_ context.Context, pattern string) error
- func (c *Cache) Evictions() uint64
- func (c *Cache) Generation() uint64
- func (c *Cache) Get(_ context.Context, key string) ([]byte, error)
- func (c *Cache) GetJSON(ctx context.Context, key string, dest any) error
- func (c *Cache) RenderMaxBytes() int64
- func (c *Cache) RenderStats() (pages int, bytes int64)
- func (c *Cache) Set(_ context.Context, key string, value []byte, ttl time.Duration) error
- func (c *Cache) SetIfGeneration(_ context.Context, key string, value []byte, ttl time.Duration, gen uint64) (bool, error)
- func (c *Cache) SetJSON(ctx context.Context, key string, value any, ttl time.Duration) error
- func (c *Cache) SetRenderMaxBytes(n int64)
- type CacheInvalidator
- func (ci *CacheInvalidator) InvalidateEntity(ctx context.Context, entityID uuid.UUID, slug string) error
- func (ci *CacheInvalidator) InvalidateGeoSchema(ctx context.Context) error
- func (ci *CacheInvalidator) InvalidateMenu(ctx context.Context) error
- func (ci *CacheInvalidator) InvalidateRendered(ctx context.Context) error
- func (ci *CacheInvalidator) InvalidateSitemaps(ctx context.Context) error
- func (ci *CacheInvalidator) InvalidateTenant(ctx context.Context) error
Constants ¶
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 ¶
var ErrCacheMiss = errors.New("cache miss")
ErrCacheMiss is returned by Get when the key is absent or expired.
Functions ¶
func EntityJSONLDKey ¶
EntityJSONLDKey generates a cache key for entity JSON-LD.
func EntityPattern ¶
EntityPattern returns a pattern to match all keys for a specific entity.
func GeoImplementationKey ¶
GeoImplementationKey generates a cache key for a GEO implementation.
func GeoSchemaKey ¶
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 ¶
MenuByIDKey generates a cache key for a menu by ID.
func PublicArchiveKey ¶
PublicArchiveKey generates a cache key for a public archive.
func PublicHomepageKey ¶
func PublicHomepageKey() string
PublicHomepageKey generates a cache key for the homepage.
func PublicPageKey ¶
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 ¶
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 SitemapKey ¶
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 (*Cache) DeletePattern ¶
DeletePattern removes every key matching a glob pattern where "*" matches any run of characters.
func (*Cache) Evictions ¶
Evictions counts cap evictions (batches, and values refused for being larger than the cap). It never decreases.
func (*Cache) Generation ¶
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) RenderMaxBytes ¶
RenderMaxBytes returns the current cap (0 = no limit).
func (*Cache) RenderStats ¶
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 ¶
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) SetRenderMaxBytes ¶
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".