45 lines
975 B
Go
45 lines
975 B
Go
package cache
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"software-station/internal/models"
|
|
)
|
|
|
|
const cacheDir = ".cache"
|
|
|
|
func init() {
|
|
if err := os.MkdirAll(cacheDir, 0750); err != nil {
|
|
log.Printf("Warning: failed to create cache directory: %v", err)
|
|
}
|
|
}
|
|
|
|
func GetCachePath(owner, repo string) string {
|
|
return filepath.Join(cacheDir, filepath.Clean(fmt.Sprintf("%s_%s.json", owner, repo)))
|
|
}
|
|
|
|
func SaveToCache(owner, repo string, software models.Software) error {
|
|
path := GetCachePath(owner, repo)
|
|
data, err := json.MarshalIndent(software, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return os.WriteFile(path, data, 0600)
|
|
}
|
|
|
|
func GetFromCache(owner, repo string) (*models.Software, error) {
|
|
path := GetCachePath(owner, repo)
|
|
data, err := os.ReadFile(path) // #nosec G304
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var software models.Software
|
|
if err := json.Unmarshal(data, &software); err != nil {
|
|
return nil, err
|
|
}
|
|
return &software, nil
|
|
}
|