GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — DockerImageSave/cache.go at master · jadolg/DockerImageSave Expand file tree/ Copy pathcache.goMore file actions 120 lines (102 loc) · 3.1 KB / Copy pathcache.goFile metadata and controls120 lines (102 loc) · 3.1 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 package main import ( "context" "fmt" "os" "path/filepath" "strings" "time" log "github.com/sirupsen/logrus" ) const ( cleanupInterval = 1 * time.Hour ) // CacheManager handles the storage and cleanup of cached Docker images type CacheManager struct { dir string maxCacheAge time.Duration } // NewCacheManager creates a new CacheManager instance func NewCacheManager(dir string, maxCacheAge time.Duration) (*CacheManager, error) { if dir == "" { tmpDir, err := os.MkdirTemp("", "docker-image-cache-*") if err != nil { return nil, fmt.Errorf("failed to create temporary cache directory: %w", err) } dir = tmpDir } else if err := os.MkdirAll(dir, 0755); err != nil { return nil, fmt.Errorf("failed to create cache directory: %w", err) } return &CacheManager{dir: dir, maxCacheAge: maxCacheAge}, nil } // StartCleanup starts a background goroutine that periodically removes old files func (c *CacheManager) StartCleanup(ctx context.Context) { ticker := time.NewTicker(cleanupInterval) defer ticker.Stop() // Run initial cleanup c.PerformCleanup() for { select { case <-ticker.C: c.PerformCleanup() case <-ctx.Done(): log.Info("Stopping cache cleanup background task") return } } } // PerformCleanup removes files from the cache directory that are older than maxCacheAge func (c *CacheManager) PerformCleanup() { files, err := os.ReadDir(c.dir) if err != nil { log.WithError(err).Error("Failed to read cache directory during cleanup") return } now := time.Now() for _, file := range files { if file.IsDir() { continue } info, err := file.Info() if err != nil { log.WithField("file", file.Name()).WithError(err).Warn("Failed to get info for file during cleanup") continue } mtime := info.ModTime() if now.Sub(mtime) > c.maxCacheAge { path := filepath.Join(c.dir, file.Name()) log.WithFields(log.Fields{ "file": file.Name(), "age": now.Sub(mtime), }).Info("Removing old cached file") if err := os.Remove(path); err != nil { log.WithField("file", file.Name()).WithError(err).Error("Failed to remove old cached file") } } } } // GetCachePath returns the full path for a cached image func (c *CacheManager) GetCachePath(imageName string, platform Platform) string { return filepath.Join(c.dir, c.GetCacheFilename(imageName, platform)) } // GetCacheFilename generates a safe filename for caching func (c *CacheManager) GetCacheFilename(imageName string, platform Platform) string { return imageFilename(ParseImageReference(imageName), platform) } // imageFilename builds the platform-qualified tar filename for an image reference. func imageFilename(ref ImageReference, platform Platform) string { parts := []string{ sanitizeFilenameComponent(ref.Repository), sanitizeFilenameComponent(ref.Tag), sanitizeFilenameComponent(platform.OS), sanitizeFilenameComponent(platform.Architecture), } if platform.Variant != "" { parts = append(parts, sanitizeFilenameComponent(platform.Variant)) } return strings.Join(parts, "_") + ".tar.gz" } // Dir returns the cache directory path func (c *CacheManager) Dir() string { return c.dir } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |