Skip to content

Commit

Permalink
feat(simplefs): supports go-humanize to configure with xxxMB
Browse files Browse the repository at this point in the history
  • Loading branch information
darkweak committed Dec 8, 2024
1 parent 75cc6df commit c2c36eb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
16 changes: 16 additions & 0 deletions simplefs/simplefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"time"

"github.com/darkweak/storages/core"
"github.com/dustin/go-humanize"
"github.com/jellydator/ttlcache/v3"
lz4 "github.com/pierrec/lz4/v4"
)
Expand Down Expand Up @@ -65,6 +66,10 @@ func Factory(simplefsCfg core.CacheProvider, logger core.Logger, stale time.Dura
directorySize = val
} else if val, ok := v.(float64); ok && val > 0 {
directorySize = int64(val)
} else if val, ok := v.(string); ok && val != "" {
s, _ := humanize.ParseBytes(val)
//nolint:gosec
directorySize = int64(s)
}
}
}
Expand Down Expand Up @@ -306,6 +311,17 @@ func (provider *Simplefs) Init() error {
}
})

files, _ := os.ReadDir(provider.path)
provider.logger.Debugf("Regenerating simplefs cache from files in the given directory.")

for _, f := range files {
if !f.IsDir() {
info, _ := f.Info()
provider.actualSize += info.Size()
provider.logger.Debugf("Add %v bytes to the actual size, sum to %v bytes.", info.Size(), provider.actualSize)
}
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion simplefs/simplefs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func TestSimplefs_EvictAfterXSeconds(t *testing.T) {
_ = client.Init()

for i := range 10 {
key := fmt.Sprintf("Test_%d", i)
key := fmt.Sprintf("Test_%d", i-1)
_ = client.SetMultiLevel(key, key, []byte(baseValue), http.Header{}, "", 1*time.Second, key)
}

Expand Down

0 comments on commit c2c36eb

Please sign in to comment.