core/generate/cache_context.go
core/generate/cache_context.goBrowse 1970 files
439 tokens
1,559 bytes
Token encoding: o200k_base
Snapshot 21a254f
← Back to SKILL.md
1package generate2 3import (4 "strings"5 6 "github.com/railwayapp/railpack/core/plan"7)8 9const (10 APT_CACHE_KEY = "apt"11 MISE_CACHE_KEY = "mise"12)13 14type CacheContext struct {15 Caches map[string]*plan.Cache16}17 18func NewCacheContext() *CacheContext {19 return &CacheContext{20 Caches: make(map[string]*plan.Cache),21 }22}23 24func (c *CacheContext) AddCache(name string, directory string) string {25 return c.AddCacheWithType(name, directory, plan.CacheTypeShared)26}27 28func (c *CacheContext) AddCacheWithType(name string, directory string, cacheType string) string {29 sanitizedName := sanitizeCacheName(name)30 c.Caches[sanitizedName] = plan.NewCache(directory)31 c.Caches[sanitizedName].Type = cacheType32 return sanitizedName33}34 35func (c *CacheContext) SetCache(name string, cache *plan.Cache) {36 c.Caches[name] = cache37}38 39func (c *CacheContext) GetCache(name string) *plan.Cache {40 return c.Caches[name]41}42 43func (c *CacheContext) GetAptCaches() []string {44 if _, ok := c.Caches[APT_CACHE_KEY]; !ok {45 aptCache := plan.NewCache("/var/cache/apt")46 aptCache.Type = plan.CacheTypeLocked47 c.Caches[APT_CACHE_KEY] = aptCache48 }49 50 aptListsKey := "apt-lists"51 if _, ok := c.Caches[aptListsKey]; !ok {52 aptListsCache := plan.NewCache("/var/lib/apt/lists")53 aptListsCache.Type = plan.CacheTypeLocked54 c.Caches[aptListsKey] = aptListsCache55 }56 57 return []string{APT_CACHE_KEY, aptListsKey}58}59 60func sanitizeCacheName(name string) string {61 if len(name) > 0 && name[0] == '/' {62 name = name[1:]63 }64 name = strings.TrimRight(name, "/")65 return strings.ReplaceAll(name, "/", "-")66}67