cache package provides a compiler-grade incremental build system using BoltDB for metadata and a content-addressed filesystem store for large artifacts.
Overview
Architecture:- BoltDB: Stores metadata (PostMeta, SearchRecord, Dependencies)
- Content Store: Content-addressed filesystem with two-tier sharding
- In-Memory LRU: Hot PostMeta data cached with 5-minute TTL
- BLAKE3 Hashing: Cryptographic content hashing
- Body Hash Tracking: Separate frontmatter and body hashes for accurate invalidation
- SSR Artifact Caching: D2 diagrams and LaTeX math pre-rendered and cached
- Zstd Compression: Fast compression for large artifacts
Opening a Cache
basePath- Cache directory (e.g.,.kosh-cache)isDev- Enable dev mode (faster, less durable)
Core Types
Manager
PostMeta
SearchRecord
SSRArtifact
Reading from Cache
GetPostByPath
GetPostByID
GetHTMLContent
GetSearchRecord
GetSSRArtifact
Writing to Cache
StorePost
BatchCommit
StoreSSRArtifact
Content-Addressed Store
Store Type
Put
< 1KB: No compression (raw)1KB - 10KB: Fast zstd compression> 10KB: Level 3 zstd compression
Get
Sharding
Content is stored with two-tier sharding:abcdef1234567890 → path ab/cd/abcdef1234567890
Cache Invalidation
Body Hash Tracking (Critical)
Posts track two hashes:ContentHash- Frontmatter hash (title, tags, date)BodyHash- Body content hash (markdown text)
ContentHash is used, changing the body without changing frontmatter would result in a silent cache hit with stale HTML.
Example invalidation logic:
SSR Input Hashes
Tracks hashes of all D2 diagrams and LaTeX expressions:In-Memory LRU Cache
Architecture
Benefits
- Faster lookups: Avoid BoltDB read for hot posts
- Automatic expiration: Stale entries cleaned up
- Thread-safe: Protected by
memCacheMu