Skip to content

Commit

Permalink
store: refactor and improve store cache
Browse files Browse the repository at this point in the history
Refactor the store cache manager cleanup to make it easier to follow and
do not keep repeating the same operations multiple times.

Signed-off-by: Maciej Borzecki <[email protected]>
  • Loading branch information
bboozzoo committed Oct 4, 2024
1 parent 9a82ad9 commit 7cc600f
Showing 1 changed file with 18 additions and 26 deletions.
44 changes: 18 additions & 26 deletions store/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,54 +165,46 @@ func (cm *CacheManager) cleanup() error {
return err
}

// we need the modtime so convert to FileInfo
fil := make([]os.FileInfo, 0, len(entries))
if len(entries) <= cm.maxItems {
return nil
}

// most of the entries will have more than one hardlink, but a minority may
// be referenced only the cache and thus be a candidate for pruning
pruneCandidates := make([]os.FileInfo, 0, len(entries)/5)

for _, entry := range entries {
fi, err := entry.Info()
if err != nil {
return err
}

fil = append(fil, fi)
}

if len(fil) <= cm.maxItems {
return nil
}

numOwned := 0
for _, fi := range fil {
n, err := hardLinkCount(fi)
if err != nil {
logger.Noticef("cannot inspect cache: %s", err)
}
// Only count the file if it is not referenced elsewhere in the filesystem
// If the file is referenced in the filesystem somewhere else our copy
// is "free" so skip it.
if n <= 1 {
numOwned++
pruneCandidates = append(pruneCandidates, fi)
}
}

if numOwned <= cm.maxItems {
if len(pruneCandidates) <= cm.maxItems {
// nothing to prune
return nil
}

var lastErr error
sort.Sort(changesByMtime(fil))
sort.Sort(changesByMtime(pruneCandidates))
numOwned := len(pruneCandidates)
deleted := 0
for _, fi := range fil {
for _, fi := range pruneCandidates {
path := cm.path(fi.Name())
n, err := hardLinkCount(fi)
if err != nil {
logger.Noticef("cannot inspect cache: %s", err)
}
// If the file is referenced in the filesystem somewhere
// else our copy is "free" so skip it. If there is any
// error we cleanup the file (it is just a cache afterall).
if n > 1 {
continue
}
if err := osRemove(path); err != nil {
if !os.IsNotExist(err) {
// If there is any error we cleanup the file (it is just a cache
// afterall).
logger.Noticef("cannot cleanup cache: %s", err)
lastErr = err
}
Expand Down

0 comments on commit 7cc600f

Please sign in to comment.