Adding Cache

This commit is contained in:
2026-03-03 01:16:04 -05:00
parent f0641262bd
commit cb77df0eb6
7 changed files with 160 additions and 19 deletions

52
internal/api/cache.go Normal file
View File

@@ -0,0 +1,52 @@
package api
import (
"crypto/sha256"
"encoding/hex"
"os"
"path/filepath"
)
type FileCache struct {
dir string
}
func NewFileCache() *FileCache {
var cacheDir string
userCacheDir, err := os.UserCacheDir()
if err == nil {
cacheDir = filepath.Join(userCacheDir, "box-box", "openf1")
} else {
// Fallback to a local .cache directory
cacheDir = ".cache/box-box/openf1"
}
// Ensure the cache directory exists
_ = os.MkdirAll(cacheDir, 0755)
return &FileCache{
dir: cacheDir,
}
}
func (c *FileCache) getCachePath(key string) string {
hash := sha256.Sum256([]byte(key))
filename := hex.EncodeToString(hash[:]) + ".json"
return filepath.Join(c.dir, filename)
}
// Get retrieves data from the cache. Returns nil, false if not found.
func (c *FileCache) Get(key string) ([]byte, bool) {
path := c.getCachePath(key)
data, err := os.ReadFile(path)
if err != nil {
return nil, false
}
return data, true
}
// Set saves data to the cache.
func (c *FileCache) Set(key string, data []byte) error {
path := c.getCachePath(key)
return os.WriteFile(path, data, 0644)
}