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