cache clean implemented
This commit is contained in:
+22
-12
@@ -3,14 +3,18 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
type rawConfig struct {
|
||||
Config
|
||||
EnvFile string `toml:"env_file"`
|
||||
EnvFile string `toml:"env_file"`
|
||||
MaxCacheSize string `toml:"max_cache_size"`
|
||||
MaxCacheAgeStr string `toml:"max_cache_age"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
@@ -19,19 +23,10 @@ type Config struct {
|
||||
MirroredRepos []string `toml:"mirrored_repos"`
|
||||
Port string `toml:"port"`
|
||||
Token string
|
||||
MaxCacheSize int64
|
||||
MaxCacheAge time.Duration
|
||||
}
|
||||
|
||||
/* Function kept for reference for future logic
|
||||
func NewConfig() *Config {
|
||||
return &Config{
|
||||
CacheRoot: "/home/ewpt3ch/dev/pacman-cache-server/tmprepo",
|
||||
MirrorURLs: "https://us.mirrors.cicku.me/archlinux/",
|
||||
Port: "8090",
|
||||
Auth: AuthConfig{Token: "FakeToken"},
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
func ReadConfig(path string) (*Config, error) {
|
||||
|
||||
var rawcfg rawConfig
|
||||
@@ -47,6 +42,21 @@ func ReadConfig(path string) (*Config, error) {
|
||||
return nil, fmt.Errorf("error getting token: %v", err)
|
||||
}
|
||||
|
||||
if rawcfg.MaxCacheSize == "" {
|
||||
return nil, fmt.Errorf("max_cache_size must be set")
|
||||
}
|
||||
sizeBytes, err := strconv.ParseInt(strings.TrimSuffix(rawcfg.MaxCacheSize, "GB"), 10, 64)
|
||||
sizeBytes = sizeBytes * 1024 * 1024 * 1024
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid cache size string")
|
||||
}
|
||||
cfg.MaxCacheSize = sizeBytes
|
||||
|
||||
cfg.MaxCacheAge, err = time.ParseDuration(rawcfg.MaxCacheAgeStr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error getting max_cache_age: %v", err)
|
||||
}
|
||||
|
||||
if err = cfg.validate(); err != nil {
|
||||
return nil, fmt.Errorf("invalid config: %w", err)
|
||||
}
|
||||
|
||||
@@ -49,6 +49,8 @@ func defaultCfgMap() map[string]string {
|
||||
"mirrored_repos": `["core", "extra"]`,
|
||||
"port": `"8090"`,
|
||||
"PKGSTASH_TOKEN": "testtoken",
|
||||
"max_cache_size": `"1GB"`,
|
||||
"max_cache_age": `"1h"`,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/ewpt3ch/pkgstash/internal/cache"
|
||||
"github.com/ewpt3ch/pkgstash/internal/repomaint"
|
||||
@@ -39,8 +40,10 @@ func newTestServer(t *testing.T, mirrorHandler http.HandlerFunc) (*httptest.Serv
|
||||
|
||||
croot := t.TempDir()
|
||||
mrepos := []string{"core"}
|
||||
maxCacheSize := int64(1 * 1024 * 1024 * 1024)
|
||||
maxCacheAge := time.Duration(time.Hour)
|
||||
|
||||
c, err := cache.NewCache(croot, []string{mirror.URL + "/"}, mrepos)
|
||||
c, err := cache.NewCache(croot, []string{mirror.URL + "/"}, mrepos, maxCacheSize, maxCacheAge)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create cache: %v", err)
|
||||
}
|
||||
|
||||
+1
-1
@@ -53,7 +53,7 @@ func main() {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
c, err := cache.NewCache(cfg.CacheRoot, cfg.MirrorURLs, cfg.MirroredRepos)
|
||||
c, err := cache.NewCache(cfg.CacheRoot, cfg.MirrorURLs, cfg.MirroredRepos, cfg.MaxCacheSize, cfg.MaxCacheAge)
|
||||
if err != nil {
|
||||
slog.Error("failed to create cache", "err", err)
|
||||
os.Exit(1)
|
||||
|
||||
Reference in New Issue
Block a user