Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(filecache): make cache size configurable #554

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/bandd/cmd/gends.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func AddGenesisDataSourceCmd(defaultNodeHome string) *cobra.Command {

config.SetRoot(clientCtx.HomeDir)

f := filecache.New(filepath.Join(defaultNodeHome, "files"))
f := filecache.New(filepath.Join(defaultNodeHome, "files"), 0)
data, err := os.ReadFile(args[3])
if err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions cmd/bandd/cmd/genos.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func AddGenesisOracleScriptCmd(defaultNodeHome string) *cobra.Command {
config := serverCtx.Config
config.SetRoot(clientCtx.HomeDir)

f := filecache.New(filepath.Join(defaultNodeHome, "files"))
dataDir := filepath.Join(defaultNodeHome, "files")
fileCache := filecache.New(dataDir, 0)
data, err := os.ReadFile(args[5])
if err != nil {
return err
Expand All @@ -49,7 +50,7 @@ func AddGenesisOracleScriptCmd(defaultNodeHome string) *cobra.Command {
if err != nil {
return err
}
filename := f.AddFile(compiledData)
filename := fileCache.AddFile(compiledData)
owner, err := sdk.AccAddressFromBech32(args[4])
if err != nil {
return err
Expand Down
8 changes: 6 additions & 2 deletions pkg/filecache/filecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ type Cache struct {
}

// New creates and returns a new file-backed data caching instance.
func New(basePath string) Cache {
// If cacheSizeBytes is 0, it defaults to 32MB.
func New(basePath string, cacheSizeBytes int64) Cache {
if cacheSizeBytes <= 0 {
cacheSizeBytes = 32 * 1024 * 1024 // Default to 32MB
}
return Cache{
fileCache: diskv.New(diskv.Options{
BasePath: basePath,
Transform: func(s string) []string { return []string{} },
CacheSizeMax: 32 * 1024 * 1024, // 32MB TODO: Make this configurable
CacheSizeMax: cacheSizeBytes,
}),
}
}
Expand Down
22 changes: 11 additions & 11 deletions pkg/filecache/filecache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestAddFile(t *testing.T) {
}
}()

f := filecache.New(dir)
f := filecache.New(dir, 0)
filename := f.AddFile([]byte("HELLO_WORLD"))
require.Equal(t, filename, "6f9b514093848217355d76365df1f54f42bdfd5f4e5f54a654c46b493d162c39")

Expand All @@ -46,7 +46,7 @@ func TestMustGetFileOK(t *testing.T) {
}
}()

f := filecache.New(dir)
f := filecache.New(dir, 0)
filename := f.AddFile([]byte("BAND"))
require.Equal(t, filename, "52f1b54ce34b64a02f9946b29f670a12933152b1122514ea969a91c211aa32fc")

Expand All @@ -66,7 +66,7 @@ func TestGetFileOK(t *testing.T) {
}
}()

f := filecache.New(dir)
f := filecache.New(dir, 0)
filename := f.AddFile([]byte("BAND"))
require.Equal(t, filename, "52f1b54ce34b64a02f9946b29f670a12933152b1122514ea969a91c211aa32fc")

Expand All @@ -87,7 +87,7 @@ func TestMustGetFileNotExist(t *testing.T) {
}
}()

f := filecache.New(dir)
f := filecache.New(dir, 0)
require.Panics(t, func() {
_ = f.MustGetFile("52f1b54ce34b64a02f9946b29f670a12933152b1122514ea969a91c211aa32fc")
})
Expand All @@ -105,7 +105,7 @@ func TestGetFileNotExist(t *testing.T) {
}
}()

f := filecache.New(dir)
f := filecache.New(dir, 0)
_, err = f.GetFile("52f1b54ce34b64a02f9946b29f670a12933152b1122514ea969a91c211aa32fc")
require.Error(t, err)
}
Expand All @@ -122,7 +122,7 @@ func TestMustGetFileGoodContent(t *testing.T) {
}
}()

f := filecache.New(dir)
f := filecache.New(dir, 0)
filename := "b20727a9b7cc4198d8785b0ef1fa4c774eb9a360e1563dd4f095ddc7af02bd55" // Correct
filepath := filepath.Join(dir, filename)
err = os.WriteFile(filepath, []byte("NOT_LIKE_THIS"), 0o600)
Expand All @@ -144,7 +144,7 @@ func TestGetFileGoodContent(t *testing.T) {
}
}()

f := filecache.New(dir)
f := filecache.New(dir, 0)
filename := "b20727a9b7cc4198d8785b0ef1fa4c774eb9a360e1563dd4f095ddc7af02bd55" // Correct
filepath := filepath.Join(dir, filename)
err = os.WriteFile(filepath, []byte("NOT_LIKE_THIS"), 0o600)
Expand All @@ -167,7 +167,7 @@ func TestMustGetFileBadContent(t *testing.T) {
}
}()

f := filecache.New(dir)
f := filecache.New(dir, 0)
filename := "b20727a9b7cc4198d8785b0ef1fa4c774eb9a360e1563dd4f095ddc7af02bd56" // Not correct
filepath := filepath.Join(dir, filename)
err = os.WriteFile(filepath, []byte("NOT_LIKE_THIS"), 0o600)
Expand All @@ -190,7 +190,7 @@ func TesGetFileBadContent(t *testing.T) {
}
}()

f := filecache.New(dir)
f := filecache.New(dir, 0)
filename := "b20727a9b7cc4198d8785b0ef1fa4c774eb9a360e1563dd4f095ddc7af02bd56" // Not correct
filepath := filepath.Join(dir, filename)
err = os.WriteFile(filepath, []byte("NOT_LIKE_THIS"), 0o600)
Expand All @@ -212,7 +212,7 @@ func TestMustGetFileInconsistentContent(t *testing.T) {
}
}()

f := filecache.New(dir)
f := filecache.New(dir, 0)
filename := "b20727a9b7cc4198d8785b0ef1fa4c774eb9a360e1563dd4f095ddc7af02bd55"
filepath := filepath.Join(dir, filename)
err = os.WriteFile(filepath, []byte("INCONSISTENT"), 0o600) // Not consistent with name
Expand All @@ -234,7 +234,7 @@ func TestGetFileInconsistentContent(t *testing.T) {
}
}()

f := filecache.New(dir)
f := filecache.New(dir, 0)
filename := "b20727a9b7cc4198d8785b0ef1fa4c774eb9a360e1563dd4f095ddc7af02bd55"
filepath := filepath.Join(dir, filename)
err = os.WriteFile(filepath, []byte("INCONSISTENT"), 0o600) // Not consistent with name
Expand Down