diff --git a/simplefs/simplefs.go b/simplefs/simplefs.go index a9217c7..3f74b05 100644 --- a/simplefs/simplefs.go +++ b/simplefs/simplefs.go @@ -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" ) @@ -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) } } } @@ -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 } diff --git a/simplefs/simplefs_test.go b/simplefs/simplefs_test.go index 40bcf09..52602d6 100644 --- a/simplefs/simplefs_test.go +++ b/simplefs/simplefs_test.go @@ -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) }