41 lines
804 B
Go
41 lines
804 B
Go
// cache.go
|
|
package main
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"encoding/hex"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
)
|
|
|
|
var (
|
|
cacheDir = "cache"
|
|
cacheMutex sync.Mutex
|
|
TestMode bool
|
|
)
|
|
|
|
// InitCache ensures the cache directory exists
|
|
func InitCache() {
|
|
if _, err := os.Stat(cacheDir); os.IsNotExist(err) {
|
|
os.Mkdir(cacheDir, 0755)
|
|
}
|
|
}
|
|
|
|
// ClearCache wipes the cache directory (used for Test Mode)
|
|
func ClearCache() {
|
|
os.RemoveAll(cacheDir)
|
|
InitCache()
|
|
}
|
|
|
|
// GetCacheFilename generates the hashed filename preserving the extension
|
|
func GetCacheFilename(key string) string {
|
|
hash := md5.Sum([]byte(key))
|
|
ext := filepath.Ext(key)
|
|
// Default to .html if no extension (e.g. for processed markdown)
|
|
if ext == "" || ext == ".md" {
|
|
ext = ".html"
|
|
}
|
|
return filepath.Join(cacheDir, hex.EncodeToString(hash[:])+ext)
|
|
}
|