Knowledge base of ~80+ markdown files across 14 domains (00-13), Logseq graph, hardware design files (KiCAD), infrastructure configs, and talas-wiki static site. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
69 lines
1.3 KiB
Go
69 lines
1.3 KiB
Go
package wiki
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
type CachedRender struct {
|
|
HTML string
|
|
TOC []TOCEntry
|
|
WordCount int
|
|
ReadingMinutes int
|
|
CachedAt time.Time
|
|
}
|
|
|
|
type RenderCache struct {
|
|
mu sync.RWMutex
|
|
items map[string]*CachedRender
|
|
ttl time.Duration
|
|
}
|
|
|
|
func NewRenderCache(ttl time.Duration) *RenderCache {
|
|
return &RenderCache{
|
|
items: make(map[string]*CachedRender),
|
|
ttl: ttl,
|
|
}
|
|
}
|
|
|
|
func (rc *RenderCache) Get(key string, modTime time.Time) *CachedRender {
|
|
rc.mu.RLock()
|
|
defer rc.mu.RUnlock()
|
|
item, ok := rc.items[key]
|
|
if !ok {
|
|
return nil
|
|
}
|
|
// Invalidate if file was modified after cache
|
|
if modTime.After(item.CachedAt) {
|
|
return nil
|
|
}
|
|
// Invalidate if TTL expired
|
|
if time.Since(item.CachedAt) > rc.ttl {
|
|
return nil
|
|
}
|
|
return item
|
|
}
|
|
|
|
func (rc *RenderCache) Set(key string, result *RenderResult) {
|
|
rc.mu.Lock()
|
|
defer rc.mu.Unlock()
|
|
rc.items[key] = &CachedRender{
|
|
HTML: result.HTML,
|
|
TOC: result.TOC,
|
|
WordCount: result.WordCount,
|
|
ReadingMinutes: result.ReadingMinutes,
|
|
CachedAt: time.Now(),
|
|
}
|
|
}
|
|
|
|
func (rc *RenderCache) Invalidate(key string) {
|
|
rc.mu.Lock()
|
|
defer rc.mu.Unlock()
|
|
delete(rc.items, key)
|
|
}
|
|
|
|
func (rc *RenderCache) Clear() {
|
|
rc.mu.Lock()
|
|
defer rc.mu.Unlock()
|
|
rc.items = make(map[string]*CachedRender)
|
|
}
|