Merge branch 'fix/io-migration-cache' into new

# Conflicts:
#	pkg/cache/cache.go
This commit is contained in:
Snider 2026-02-08 21:28:25 +00:00
commit 44e74128c4

49
pkg/cache/cache.go vendored
View file

@ -3,8 +3,6 @@ package cache
import (
"encoding/json"
"errors"
"io/fs"
"os"
"path/filepath"
"time"
@ -17,7 +15,6 @@ const DefaultTTL = 1 * time.Hour
// Cache represents a file-based cache.
type Cache struct {
medium io.Medium
baseDir string
ttl time.Duration
}
@ -30,13 +27,8 @@ type Entry struct {
}
// New creates a new cache instance.
// If baseDir is empty, uses .core/cache in current directory.
// If m is nil, uses io.Local.
func New(m io.Medium, baseDir string, ttl time.Duration) (*Cache, error) {
if m == nil {
m = io.Local
}
// If baseDir is empty, uses .core/cache in current directory
func New(baseDir string, ttl time.Duration) (*Cache, error) {
if baseDir == "" {
// Use .core/cache in current working directory
cwd, err := os.Getwd()
@ -50,21 +42,12 @@ func New(m io.Medium, baseDir string, ttl time.Duration) (*Cache, error) {
ttl = DefaultTTL
}
// Convert to absolute path for consistency
absBaseDir, err := filepath.Abs(baseDir)
if err != nil {
return nil, err
}
// Ensure cache directory exists
if err := m.EnsureDir(absBaseDir); err != nil {
if err := io.Local.EnsureDir(baseDir); err != nil {
return nil, err
}
baseDir = absBaseDir
return &Cache{
medium: m,
baseDir: baseDir,
ttl: ttl,
}, nil
@ -79,17 +62,16 @@ func (c *Cache) Path(key string) string {
func (c *Cache) Get(key string, dest interface{}) (bool, error) {
path := c.Path(key)
content, err := c.medium.Read(path)
dataStr, err := io.Local.Read(path)
if err != nil {
if errors.Is(err, fs.ErrNotExist) || os.IsNotExist(err) {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
data := []byte(content)
var entry Entry
if err := json.Unmarshal(data, &entry); err != nil {
if err := json.Unmarshal([]byte(dataStr), &entry); err != nil {
// Invalid cache file, treat as miss
return false, nil
}
@ -111,6 +93,11 @@ func (c *Cache) Get(key string, dest interface{}) (bool, error) {
func (c *Cache) Set(key string, data interface{}) error {
path := c.Path(key)
// Ensure parent directory exists
if err := io.Local.EnsureDir(filepath.Dir(path)); err != nil {
return err
}
// Marshal the data
dataBytes, err := json.Marshal(data)
if err != nil {
@ -128,15 +115,14 @@ func (c *Cache) Set(key string, data interface{}) error {
return err
}
// medium.Write creates parent directories automatically
return c.medium.Write(path, string(entryBytes))
return io.Local.Write(path, string(entryBytes))
}
// Delete removes an item from the cache.
func (c *Cache) Delete(key string) error {
path := c.Path(key)
err := c.medium.Delete(path)
if errors.Is(err, fs.ErrNotExist) || os.IsNotExist(err) {
err := io.Local.Delete(path)
if os.IsNotExist(err) {
return nil
}
return err
@ -144,21 +130,20 @@ func (c *Cache) Delete(key string) error {
// Clear removes all cached items.
func (c *Cache) Clear() error {
return c.medium.DeleteAll(c.baseDir)
return io.Local.DeleteAll(c.baseDir)
}
// Age returns how old a cached item is, or -1 if not cached.
func (c *Cache) Age(key string) time.Duration {
path := c.Path(key)
content, err := c.medium.Read(path)
dataStr, err := io.Local.Read(path)
if err != nil {
return -1
}
data := []byte(content)
var entry Entry
if err := json.Unmarshal(data, &entry); err != nil {
if err := json.Unmarshal([]byte(dataStr), &entry); err != nil {
return -1
}