diff --git a/cmd/s3Browser.go b/cmd/s3Browser.go index 6605ea2..40c20ad 100644 --- a/cmd/s3Browser.go +++ b/cmd/s3Browser.go @@ -1,13 +1,19 @@ package main -import s3browser "git.kapelle.org/niklas/s3browser/internal" +import ( + "time" + + s3browser "git.kapelle.org/niklas/s3browser/internal" +) func main() { s3browser.Start(s3browser.AppConfig{ - S3Endoint: "localhost:9000", - S3SSL: false, - S3AccessKey: "testo", - S3SecretKey: "testotesto", - S3Buket: "dev", + S3Endoint: "localhost:9000", + S3SSL: false, + S3AccessKey: "testo", + S3SecretKey: "testotesto", + S3Buket: "dev", + CacheTTL: 20 * time.Second, + CacheCleanup: 1 * time.Minute, }) } diff --git a/go.mod b/go.mod index acd5336..90c2121 100644 --- a/go.mod +++ b/go.mod @@ -8,4 +8,5 @@ require ( github.com/graphql-go/handler v0.2.3 github.com/minio/minio-go/v7 v7.0.12 github.com/opentracing/opentracing-go v1.2.0 // indirect + github.com/patrickmn/go-cache v2.1.0+incompatible // indirect ) diff --git a/go.sum b/go.sum index 67f720e..289bfa4 100644 --- a/go.sum +++ b/go.sum @@ -40,6 +40,8 @@ github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9 github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs= github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc= +github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= +github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rs/xid v1.2.1 h1:mhH9Nq+C1fY2l1XIpgxIiUOfNpRBYH1kKcr+qfKgjRc= diff --git a/internal/dataloader.go b/internal/dataloader.go index ae6cc61..e1c600b 100644 --- a/internal/dataloader.go +++ b/internal/dataloader.go @@ -188,13 +188,13 @@ func handleLoaderError(k dataloader.Keys, err error) []*dataloader.Result { } // createDataloader create all dataloaders and return a map of them -func createDataloader() map[string]*dataloader.Loader { +func createDataloader(config AppConfig) map[string]*dataloader.Loader { loaderMap := make(map[string]*dataloader.Loader, 0) - loaderMap["getFiles"] = dataloader.NewBatchedLoader(getFilesBatch) - loaderMap["getFile"] = dataloader.NewBatchedLoader(getFileBatch) - loaderMap["listObjects"] = dataloader.NewBatchedLoader(listObjectsBatch) - loaderMap["getDirs"] = dataloader.NewBatchedLoader(getDirsBatch) + loaderMap["getFiles"] = dataloader.NewBatchedLoader(getFilesBatch, dataloader.WithCache(newCache(config.CacheTTL, config.CacheCleanup))) + loaderMap["getFile"] = dataloader.NewBatchedLoader(getFileBatch, dataloader.WithCache(newCache(config.CacheTTL, config.CacheCleanup))) + loaderMap["listObjects"] = dataloader.NewBatchedLoader(listObjectsBatch, dataloader.WithCache(newCache(config.CacheTTL, config.CacheCleanup))) + loaderMap["getDirs"] = dataloader.NewBatchedLoader(getDirsBatch, dataloader.WithCache(newCache(config.CacheTTL, config.CacheCleanup))) return loaderMap } diff --git a/internal/s3Broswer.go b/internal/s3Broswer.go index 05651ee..b0c7271 100644 --- a/internal/s3Broswer.go +++ b/internal/s3Broswer.go @@ -7,6 +7,7 @@ import ( "log" "net/http" "path/filepath" + "time" "github.com/graph-gophers/dataloader" "github.com/graphql-go/graphql" @@ -17,11 +18,13 @@ import ( // AppConfig general config type AppConfig struct { - S3Endoint string - S3AccessKey string - S3SecretKey string - S3SSL bool - S3Buket string + S3Endoint string + S3AccessKey string + S3SecretKey string + S3SSL bool + S3Buket string + CacheTTL time.Duration + CacheCleanup time.Duration } // File represents a file with its metadata @@ -116,7 +119,7 @@ func setupS3Client(config AppConfig) *minio.Client { func Start(config AppConfig) { s3Client := setupS3Client(config) - loader := createDataloader() + loader := createDataloader(config) graphqlTypes() schema, err := graphqlSchema() diff --git a/internal/ttl_cache.go b/internal/ttl_cache.go new file mode 100644 index 0000000..f68b6a8 --- /dev/null +++ b/internal/ttl_cache.go @@ -0,0 +1,43 @@ +package s3browser + +import ( + "context" + "time" + + "github.com/graph-gophers/dataloader" + gocache "github.com/patrickmn/go-cache" +) + +type cache struct { + c *gocache.Cache +} + +func newCache(ttl, cleanupInterval time.Duration) *cache { + return &cache{ + c: gocache.New(ttl, cleanupInterval), + } +} + +func (c *cache) Get(_ context.Context, key dataloader.Key) (dataloader.Thunk, bool) { + v, ok := c.c.Get(key.String()) + if ok { + return v.(dataloader.Thunk), ok + } + return nil, ok +} + +func (c *cache) Set(_ context.Context, key dataloader.Key, value dataloader.Thunk) { + c.c.Set(key.String(), value, 0) +} + +func (c *cache) Delete(_ context.Context, key dataloader.Key) bool { + if _, found := c.c.Get(key.String()); found { + c.c.Delete(key.String()) + return true + } + return false +} + +func (c *cache) Clear() { + c.c.Flush() +}