Skip to content

Commit 08ca116

Browse files
committed
feat(simplefs): supports go-humanize to configure with xxxMB
1 parent 75cc6df commit 08ca116

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

simplefs/simplefs.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"time"
1616

1717
"github.com/darkweak/storages/core"
18+
"github.com/dustin/go-humanize"
1819
"github.com/jellydator/ttlcache/v3"
1920
lz4 "github.com/pierrec/lz4/v4"
2021
)
@@ -65,6 +66,10 @@ func Factory(simplefsCfg core.CacheProvider, logger core.Logger, stale time.Dura
6566
directorySize = val
6667
} else if val, ok := v.(float64); ok && val > 0 {
6768
directorySize = int64(val)
69+
} else if val, ok := v.(string); ok && val != "" {
70+
s, _ := humanize.ParseBytes(val)
71+
//nolint:gosec
72+
directorySize = int64(s)
6873
}
6974
}
7075
}
@@ -306,6 +311,17 @@ func (provider *Simplefs) Init() error {
306311
}
307312
})
308313

314+
files, _ := os.ReadDir(provider.path)
315+
provider.logger.Debugf("Regenerating simplefs cache from files in the given directory.")
316+
317+
for _, f := range files {
318+
if !f.IsDir() {
319+
info, _ := f.Info()
320+
provider.actualSize += info.Size()
321+
provider.logger.Debugf("Add %v bytes to the actual size, sum to %v bytes.", info.Size(), provider.actualSize)
322+
}
323+
}
324+
309325
return nil
310326
}
311327

simplefs/simplefs_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ func TestSimplefs_EvictAfterXSeconds(t *testing.T) {
142142

143143
for i := range 10 {
144144
key := fmt.Sprintf("Test_%d", i)
145-
_ = client.SetMultiLevel(key, key, []byte(baseValue), http.Header{}, "", 1*time.Second, key)
145+
_ = client.SetMultiLevel(key, key, []byte(baseValue), http.Header{}, "", time.Second, key)
146+
time.Sleep(100 * time.Millisecond)
146147
}
147148

148149
res := client.Get("Test_0")

0 commit comments

Comments
 (0)