♻️ Use ezcache to only remember files for five minutes

This commit is contained in:
Dan Jones 2025-04-21 15:19:40 -05:00
commit 8d74dc24e5
3 changed files with 22 additions and 21 deletions

View file

@ -10,7 +10,9 @@ import (
fp "path/filepath"
"regexp"
"strings"
"time"
"codeberg.org/danjones000/ezcache"
"gopkg.in/yaml.v3"
)
@ -82,24 +84,6 @@ func processFile(input processInput) (processed bool, processError, cbError erro
return
}
type fileMap map[string][]os.DirEntry
func (fm fileMap) GetFiles(path string) []os.DirEntry {
files, ok := fm[path]
if ok {
return files
}
fm.ReadPath(path)
return fm[path]
}
func (fm fileMap) ReadPath(path string) {
if _, ok := fm[path]; ok {
return
}
fm[path], _ = os.ReadDir(path)
}
func WalkFiles(d *Data, stopOnError bool, gp GroupPrinter, cb ShowWalker) (err error) {
err = ensureExtRe(d.Config)
if err != nil {
@ -109,8 +93,13 @@ func WalkFiles(d *Data, stopOnError bool, gp GroupPrinter, cb ShowWalker) (err e
cb = DryRun(os.Stdout)
}
count := 0
allFiles := fileMap{}
allFiles, cacheErr := ezcache.New(os.ReadDir, 5*time.Minute)
if cacheErr != nil {
return err
}
var files []os.DirEntry
var filesErr error
if err != nil {
return
@ -121,7 +110,10 @@ func WalkFiles(d *Data, stopOnError bool, gp GroupPrinter, cb ShowWalker) (err e
}
showstart:
for _, source := range s.Sources {
files = allFiles.GetFiles(source)
files, filesErr = allFiles.Get(source)
if filesErr != nil {
return filesErr
}
for _, file := range files {
pIn := processInput{file, source, s, d.Config.extRe, d.Config.Skip, cb}
processed, processErr, cbErr := processFile(pIn)

3
go.mod
View file

@ -1,8 +1,9 @@
module codeberg.org/danjones000/utils
go 1.23.1
go 1.23.7
require (
codeberg.org/danjones000/ezcache v0.5.2
github.com/charmbracelet/bubbles v0.20.0
github.com/charmbracelet/bubbletea v1.1.1
github.com/spf13/pflag v1.0.5

8
go.sum
View file

@ -1,3 +1,5 @@
codeberg.org/danjones000/ezcache v0.5.2 h1:msWRwbLj+HHCU3dnFbsdedCRuhyPQCOFbT9EgjwN9KM=
codeberg.org/danjones000/ezcache v0.5.2/go.mod h1:wLTvTXfXxaEDUS9dOILqQmhAlB/9lUZCC2yB5CyGBfk=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/charmbracelet/bubbles v0.20.0 h1:jSZu6qD8cRQ6k9OMfR1WlM+ruM8fkPWkHvQWD9LIutE=
@ -10,6 +12,8 @@ github.com/charmbracelet/x/ansi v0.2.3 h1:VfFN0NUpcjBRd4DnKfRaIRo53KRgey/nhOoEqo
github.com/charmbracelet/x/ansi v0.2.3/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/charmbracelet/x/term v0.2.0 h1:cNB9Ot9q8I711MyZ7myUR5HFWL/lc3OpU8jZ4hwm0x0=
github.com/charmbracelet/x/term v0.2.0/go.mod h1:GVxgxAbjUrmpvIINHIQnJJKpMlHiZ4cktEQCN6GWyF0=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
@ -26,11 +30,15 @@ github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELU
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=