2026-01-28 15:19:03 +00:00
|
|
|
// Package cache provides a file-based cache for GitHub API responses.
|
|
|
|
|
package cache
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"time"
|
2026-02-02 04:32:20 +00:00
|
|
|
|
refactor: strip to pure package library (#3)
- Fix remaining 187 pkg/ files referencing core/cli → core/go
- Move SDK library code from internal/cmd/sdk/ → pkg/sdk/ (new package)
- Create pkg/rag/helpers.go with convenience functions from internal/cmd/rag/
- Fix pkg/mcp/tools_rag.go to use pkg/rag instead of internal/cmd/rag
- Fix pkg/build/buildcmd/cmd_sdk.go and pkg/release/sdk.go to use pkg/sdk
- Remove all non-library content: main.go, internal/, cmd/, docker/,
scripts/, tasks/, tools/, .core/, .forgejo/, .woodpecker/, Taskfile.yml
- Run go mod tidy to trim unused dependencies
core/go is now a pure Go package suite (library only).
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: Claude <developers@lethean.io>
Reviewed-on: https://forge.lthn.ai/core/go/pulls/3
2026-02-16 14:23:45 +00:00
|
|
|
"forge.lthn.ai/core/go/pkg/io"
|
2026-01-28 15:19:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// DefaultTTL is the default cache expiry time.
|
|
|
|
|
const DefaultTTL = 1 * time.Hour
|
|
|
|
|
|
|
|
|
|
// Cache represents a file-based cache.
|
|
|
|
|
type Cache struct {
|
|
|
|
|
baseDir string
|
|
|
|
|
ttl time.Duration
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Entry represents a cached item with metadata.
|
|
|
|
|
type Entry struct {
|
|
|
|
|
Data json.RawMessage `json:"data"`
|
|
|
|
|
CachedAt time.Time `json:"cached_at"`
|
|
|
|
|
ExpiresAt time.Time `json:"expires_at"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// New creates a new cache instance.
|
|
|
|
|
// 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()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
baseDir = filepath.Join(cwd, ".core", "cache")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ttl == 0 {
|
|
|
|
|
ttl = DefaultTTL
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ensure cache directory exists
|
2026-02-02 04:32:20 +00:00
|
|
|
if err := io.Local.EnsureDir(baseDir); err != nil {
|
2026-01-28 15:19:03 +00:00
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &Cache{
|
|
|
|
|
baseDir: baseDir,
|
|
|
|
|
ttl: ttl,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Path returns the full path for a cache key.
|
|
|
|
|
func (c *Cache) Path(key string) string {
|
|
|
|
|
return filepath.Join(c.baseDir, key+".json")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get retrieves a cached item if it exists and hasn't expired.
|
|
|
|
|
func (c *Cache) Get(key string, dest interface{}) (bool, error) {
|
|
|
|
|
path := c.Path(key)
|
|
|
|
|
|
2026-02-02 04:32:20 +00:00
|
|
|
dataStr, err := io.Local.Read(path)
|
2026-01-28 15:19:03 +00:00
|
|
|
if err != nil {
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var entry Entry
|
2026-02-02 04:32:20 +00:00
|
|
|
if err := json.Unmarshal([]byte(dataStr), &entry); err != nil {
|
2026-01-28 15:19:03 +00:00
|
|
|
// Invalid cache file, treat as miss
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check expiry
|
|
|
|
|
if time.Now().After(entry.ExpiresAt) {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Unmarshal the actual data
|
|
|
|
|
if err := json.Unmarshal(entry.Data, dest); err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set stores an item in the cache.
|
|
|
|
|
func (c *Cache) Set(key string, data interface{}) error {
|
|
|
|
|
path := c.Path(key)
|
|
|
|
|
|
|
|
|
|
// Ensure parent directory exists
|
2026-02-02 04:32:20 +00:00
|
|
|
if err := io.Local.EnsureDir(filepath.Dir(path)); err != nil {
|
2026-01-28 15:19:03 +00:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Marshal the data
|
|
|
|
|
dataBytes, err := json.Marshal(data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
entry := Entry{
|
|
|
|
|
Data: dataBytes,
|
|
|
|
|
CachedAt: time.Now(),
|
|
|
|
|
ExpiresAt: time.Now().Add(c.ttl),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
entryBytes, err := json.MarshalIndent(entry, "", " ")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-02 04:32:20 +00:00
|
|
|
return io.Local.Write(path, string(entryBytes))
|
2026-01-28 15:19:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete removes an item from the cache.
|
|
|
|
|
func (c *Cache) Delete(key string) error {
|
|
|
|
|
path := c.Path(key)
|
2026-02-02 04:32:20 +00:00
|
|
|
err := io.Local.Delete(path)
|
2026-01-28 15:19:03 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Clear removes all cached items.
|
|
|
|
|
func (c *Cache) Clear() error {
|
2026-02-02 04:32:20 +00:00
|
|
|
return io.Local.DeleteAll(c.baseDir)
|
2026-01-28 15:19:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
|
2026-02-02 04:32:20 +00:00
|
|
|
dataStr, err := io.Local.Read(path)
|
2026-01-28 15:19:03 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var entry Entry
|
2026-02-02 04:32:20 +00:00
|
|
|
if err := json.Unmarshal([]byte(dataStr), &entry); err != nil {
|
2026-01-28 15:19:03 +00:00
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return time.Since(entry.CachedAt)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GitHub-specific cache keys
|
|
|
|
|
|
|
|
|
|
// GitHubReposKey returns the cache key for an org's repo list.
|
|
|
|
|
func GitHubReposKey(org string) string {
|
|
|
|
|
return filepath.Join("github", org, "repos")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GitHubRepoKey returns the cache key for a specific repo's metadata.
|
|
|
|
|
func GitHubRepoKey(org, repo string) string {
|
|
|
|
|
return filepath.Join("github", org, repo, "meta")
|
|
|
|
|
}
|