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..61d9c97 100644 --- a/simplefs/simplefs_test.go +++ b/simplefs/simplefs_test.go @@ -142,7 +142,8 @@ func TestSimplefs_EvictAfterXSeconds(t *testing.T) { for i := range 10 { key := fmt.Sprintf("Test_%d", i) - _ = client.SetMultiLevel(key, key, []byte(baseValue), http.Header{}, "", 1*time.Second, key) + _ = client.SetMultiLevel(key, key, []byte(baseValue), http.Header{}, "", time.Second, key) + time.Sleep(100 * time.Millisecond) } res := client.Get("Test_0")