Skip to content

Commit caee8fb

Browse files
Include number of evicted entries when explicitly evicting
Update the implementation of `ghasum cache` to output the number of entries that were removed when evicting. This gives some indication of how much work was done. This may help users understand if future ghasum operations will take more time because of cache misses as well as how much disk space might have been saved. This information is not outputted when other commands evict (by default) in the background. This is because I believe it would be too noisy and not as helpful in that context (e.g. the first benefit listed above does not really apply). This is, however, open for debate, and I can see a verbose mode or similar where this info would probably be included in the output.
1 parent 0acc62b commit caee8fb

File tree

7 files changed

+29
-11
lines changed

7 files changed

+29
-11
lines changed

cmd/ghasum/cache.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,16 @@ func cmdCache(argv []string) error {
4646
return errors.Join(errUnexpected, err)
4747
}
4848

49-
msg := "Ok"
49+
var msg string
5050
command := args[0]
5151
switch command {
5252
case "clear":
5353
err = c.Clear()
54+
msg = "Ok"
5455
case "evict":
55-
err = c.Evict()
56+
var cnt uint
57+
cnt, err = c.Evict()
58+
msg = fmt.Sprintf("Ok (%d evicted)", cnt)
5659
case "path":
5760
msg = c.Path()
5861
default:

cmd/ghasum/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func cmdInit(argv []string) error {
5454
}
5555

5656
if !*flagNoEvict {
57-
if err = c.Evict(); err != nil {
57+
if _, err = c.Evict(); err != nil {
5858
return errors.Join(errCache, err)
5959
}
6060
}

cmd/ghasum/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func cmdList(argv []string) error {
5555
}
5656

5757
if !*flagNoEvict {
58-
if err = c.Evict(); err != nil {
58+
if _, err = c.Evict(); err != nil {
5959
return errors.Join(errCache, err)
6060
}
6161
}

cmd/ghasum/update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func cmdUpdate(argv []string) error {
5656
}
5757

5858
if !*flagNoEvict {
59-
if err = c.Evict(); err != nil {
59+
if _, err = c.Evict(); err != nil {
6060
return errors.Join(errCache, err)
6161
}
6262
}

cmd/ghasum/verify.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func cmdVerify(argv []string) error {
7777
}
7878

7979
if !*flagNoEvict {
80-
if err = c.Evict(); err != nil {
80+
if _, err = c.Evict(); err != nil {
8181
return errors.Join(errCache, err)
8282
}
8383
}

internal/cache/cache.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ func (c *Cache) Clear() error {
5151
}
5252

5353
// Evict removes old entries from the cache.
54-
func (c *Cache) Evict() error {
54+
func (c *Cache) Evict() (uint, error) {
5555
deadline := time.Now().AddDate(0, 0, -5)
56+
57+
var count uint
5658
walk := func(path string, entry fs.DirEntry, _ error) error {
5759
depth := strings.Count(path, string(os.PathSeparator))
5860
if depth < 2 {
@@ -65,6 +67,7 @@ func (c *Cache) Evict() error {
6567
}
6668

6769
if info.ModTime().Before(deadline) {
70+
count += 1
6871
_ = os.RemoveAll(filepath.Join(c.path, path))
6972
return fs.SkipDir
7073
}
@@ -74,16 +77,16 @@ func (c *Cache) Evict() error {
7477

7578
fsys, err := os.OpenRoot(c.path)
7679
if errors.Is(err, fs.ErrNotExist) {
77-
return nil
80+
return 0, nil
7881
} else if err != nil {
79-
return fmt.Errorf("could not open cache directory: %v", err)
82+
return 0, fmt.Errorf("could not open cache directory: %v", err)
8083
}
8184

8285
if err := fs.WalkDir(fsys.FS(), ".", walk); err != nil {
83-
return fmt.Errorf("cache eviction failed: %v", err)
86+
return 0, fmt.Errorf("cache eviction failed: %v", err)
8487
}
8588

86-
return nil
89+
return count, nil
8790
}
8891

8992
// Init sets up the cache (if necessary).

testdata/cache/success.txtar

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ stdout 'Ok'
1010
! stderr .
1111
! exists .does-not-exist/
1212

13+
# Evict - cache directory exists
14+
exec ghasum cache -cache .cache/ evict
15+
stdout 'Ok \(0 evicted\)'
16+
! stderr .
17+
! exists .cache/
18+
19+
# Evict - cache directory does not exist
20+
exec ghasum cache -cache .does-not-exist/ evict
21+
stdout 'Ok \(0 evicted\)'
22+
! stderr .
23+
! exists .does-not-exist/
24+
1325
# Path - no path specified
1426
exec ghasum cache path
1527
! stdout 'Ok'

0 commit comments

Comments
 (0)