}
// Trim removes old cache entries that are likely not to be reused.
-func (c *Cache) Trim() {
+func (c *Cache) Trim() error {
now := c.now()
// We maintain in dir/trim.txt the time of the last completed cache trim.
if t, err := strconv.ParseInt(strings.TrimSpace(string(data)), 10, 64); err == nil {
lastTrim := time.Unix(t, 0)
if d := now.Sub(lastTrim); d < trimInterval && d > -mtimeInterval {
- return
+ return nil
}
}
}
var b bytes.Buffer
fmt.Fprintf(&b, "%d", now.Unix())
if err := lockedfile.Write(filepath.Join(c.dir, "trim.txt"), &b, 0666); err != nil {
- return
+ return err
}
+
+ return nil
}
// trimSubdir trims a single cache subdirectory.
"bytes"
"encoding/binary"
"fmt"
+ "internal/testenv"
"os"
"path/filepath"
- "runtime"
"testing"
"time"
)
}
func TestCacheTrim(t *testing.T) {
- if runtime.GOOS == "js" || runtime.GOOS == "wasip1" {
- t.Skip("file lock is unsupported on +" + runtime.GOOS)
- }
-
dir, err := os.MkdirTemp("", "cachetest-")
if err != nil {
t.Fatal(err)
checkTime(fmt.Sprintf("%x-d", entry.OutputID), mtime2)
// Trim should leave everything alone: it's all too new.
- c.Trim()
+ if err := c.Trim(); err != nil {
+ if testenv.SyscallIsNotSupported(err) {
+ t.Skipf("skipping: Trim is unsupported (%v)", err)
+ }
+ t.Fatal(err)
+ }
if _, err := c.Get(id); err != nil {
t.Fatal(err)
}
// Trim less than a day later should not do any work at all.
now = start + 80000
- c.Trim()
+ if err := c.Trim(); err != nil {
+ t.Fatal(err)
+ }
if _, err := c.Get(id); err != nil {
t.Fatal(err)
}
// and we haven't looked at it since, so 5 days later it should be gone.
now += 5 * 86400
checkTime(fmt.Sprintf("%x-a", dummyID(2)), start)
- c.Trim()
+ if err := c.Trim(); err != nil {
+ t.Fatal(err)
+ }
if _, err := c.Get(id); err != nil {
t.Fatal(err)
}
// Check that another 5 days later it is still not gone,
// but check by using checkTime, which doesn't bring mtime forward.
now += 5 * 86400
- c.Trim()
+ if err := c.Trim(); err != nil {
+ t.Fatal(err)
+ }
checkTime(fmt.Sprintf("%x-a", id), mtime3)
checkTime(fmt.Sprintf("%x-d", entry.OutputID), mtime3)
// Even though the entry for id is now old enough to be trimmed,
// it gets a reprieve until the time comes for a new Trim scan.
now += 86400 / 2
- c.Trim()
+ if err := c.Trim(); err != nil {
+ t.Fatal(err)
+ }
checkTime(fmt.Sprintf("%x-a", id), mtime3)
checkTime(fmt.Sprintf("%x-d", entry.OutputID), mtime3)
// Another half a day later, Trim should actually run, and it should remove id.
now += 86400/2 + 1
- c.Trim()
+ if err := c.Trim(); err != nil {
+ t.Fatal(err)
+ }
if _, err := c.Get(dummyID(1)); err == nil {
t.Fatal("Trim did not remove dummyID(1)")
}