feat: Implement disk-based IO cache

This commit is contained in:
Felix Pojtinger
2021-12-23 22:12:29 +01:00
parent b4c8925299
commit 35ebd268a2
4 changed files with 93 additions and 17 deletions
+24 -2
View File
@@ -3,6 +3,7 @@ package cmd
import (
"context"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
@@ -18,6 +19,7 @@ import (
"github.com/pojntfx/stfs/pkg/config"
"github.com/pojntfx/stfs/pkg/operations"
"github.com/pojntfx/stfs/pkg/tape"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
@@ -32,6 +34,10 @@ const (
signatureRecipientFlag = "signature-recipient"
)
var (
cacheDir = filepath.Join(os.TempDir(), "stfs")
)
var serveFTPCmd = &cobra.Command{
Use: "ftp",
Aliases: []string{"f"},
@@ -187,6 +193,22 @@ var serveFTPCmd = &cobra.Command{
},
viper.GetString(compressionLevelFlag),
func() (afero.File, func() error, error) {
tmpdir := filepath.Join(viper.GetString(cacheDirFlag), "io")
if err := os.MkdirAll(tmpdir, os.ModePerm); err != nil {
return nil, nil, err
}
f, err := ioutil.TempFile(tmpdir, "*")
if err != nil {
return nil, nil, err
}
return f, func() error {
return os.Remove(filepath.Join(tmpdir, f.Name()))
}, nil
},
logger.PrintHeader,
)
@@ -196,7 +218,7 @@ var serveFTPCmd = &cobra.Command{
root,
viper.GetString(cacheFlag),
viper.GetDuration(cacheDurationFlag),
viper.GetString(cacheDirFlag),
filepath.Join(viper.GetString(cacheDirFlag), "cache"),
)
if err != nil {
return err
@@ -236,7 +258,7 @@ func init() {
serveFTPCmd.PersistentFlags().StringP(laddrFlag, "a", "localhost:1337", "Listen address")
serveFTPCmd.PersistentFlags().StringP(cacheFlag, "n", config.NoneKey, fmt.Sprintf("Cache to use (default %v, available are %v)", config.NoneKey, cache.KnownCacheTypes))
serveFTPCmd.PersistentFlags().DurationP(cacheDurationFlag, "u", time.Hour, "Duration until cache is invalidated")
serveFTPCmd.PersistentFlags().StringP(cacheDirFlag, "w", filepath.Join(os.TempDir(), "stfs", "cache"), "Directory to use if dir cache is enabled")
serveFTPCmd.PersistentFlags().StringP(cacheDirFlag, "w", cacheDir, "Directory to use if dir cache is enabled")
viper.AutomaticEnv()
+19 -2
View File
@@ -3,6 +3,7 @@ package cmd
import (
"context"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
@@ -127,6 +128,22 @@ var serveHTTPCmd = &cobra.Command{
},
"", // We never write
func() (afero.File, func() error, error) {
tmpdir := filepath.Join(viper.GetString(cacheDirFlag), "io")
if err := os.MkdirAll(tmpdir, os.ModePerm); err != nil {
return nil, nil, err
}
f, err := ioutil.TempFile(tmpdir, "*")
if err != nil {
return nil, nil, err
}
return f, func() error {
return os.Remove(filepath.Join(tmpdir, f.Name()))
}, nil
},
logger.PrintHeader,
)
@@ -136,7 +153,7 @@ var serveHTTPCmd = &cobra.Command{
root,
viper.GetString(cacheFlag),
viper.GetDuration(cacheDurationFlag),
viper.GetString(cacheDirFlag),
filepath.Join(viper.GetString(cacheDirFlag), "cache"),
)
if err != nil {
return err
@@ -163,7 +180,7 @@ func init() {
serveHTTPCmd.PersistentFlags().StringP(laddrFlag, "a", "localhost:1337", "Listen address")
serveHTTPCmd.PersistentFlags().StringP(cacheFlag, "n", config.NoneKey, fmt.Sprintf("Cache to use (default %v, available are %v)", config.NoneKey, cache.KnownCacheTypes))
serveHTTPCmd.PersistentFlags().DurationP(cacheDurationFlag, "u", time.Hour, "Duration until cache is invalidated")
serveHTTPCmd.PersistentFlags().StringP(cacheDirFlag, "w", filepath.Join(os.TempDir(), "stfs", "cache"), "Directory to use if dir cache is enabled")
serveHTTPCmd.PersistentFlags().StringP(cacheDirFlag, "w", cacheDir, "Directory to use if dir cache is enabled")
viper.AutomaticEnv()