MCPcopy
hub / github.com/perkeep/perkeep / cleanCacheDir

Function cleanCacheDir

cmd/pk-put/kvcache.go:357–396  ·  view source on GitHub ↗

Delete all but the oldest 5 havecache/statcache dirs, unless they're newer than 30 days.

()

Source from the content-addressed store, hash-verified

355// Delete all but the oldest 5 havecache/statcache dirs, unless they're newer
356// than 30 days.
357func cleanCacheDir() {
358 dir := osutil.CacheDir()
359 f, err := os.Open(dir)
360 if err != nil {
361 return
362 }
363 defer f.Close()
364 fis, err := f.Readdir(-1)
365 if err != nil {
366 return
367 }
368 var haveCache, statCache []os.FileInfo
369 seen := make(map[string]bool)
370 for _, fi := range fis {
371 seen[fi.Name()] = true
372 }
373
374 for _, fi := range fis {
375 if strings.HasPrefix(fi.Name(), "pk-put.havecache.") {
376 haveCache = append(haveCache, fi)
377 continue
378 }
379 if strings.HasPrefix(fi.Name(), "pk-put.statcache.") {
380 statCache = append(statCache, fi)
381 continue
382 }
383 }
384 for _, list := range [][]os.FileInfo{haveCache, statCache} {
385 if len(list) <= 5 {
386 continue
387 }
388 sort.Sort(byModtime(list))
389 list = list[:len(list)-5]
390 for _, fi := range list {
391 if fi.ModTime().Before(time.Now().Add(-30 * 24 * time.Hour)) {
392 os.RemoveAll(filepath.Join(dir, fi.Name()))
393 }
394 }
395 }
396}
397
398type byModtime []os.FileInfo
399

Callers 1

NewKvHaveCacheFunction · 0.85

Calls 9

CacheDirFunction · 0.92
byModtimeTypeAlias · 0.85
HasPrefixMethod · 0.80
OpenMethod · 0.65
CloseMethod · 0.65
ReaddirMethod · 0.65
NameMethod · 0.65
ModTimeMethod · 0.45
AddMethod · 0.45

Tested by

no test coverage detected