From b42f45afe99fbfae8225e4167534b7fa3e27c8d2 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Fri, 6 Aug 2021 13:48:49 +0200 Subject: [PATCH] LESS CACHES --- internal/dataloader.go | 54 ++++++++++-------------------------------- internal/s3Broswer.go | 3 +-- 2 files changed, 13 insertions(+), 44 deletions(-) diff --git a/internal/dataloader.go b/internal/dataloader.go index 2d131eb..332fe3d 100644 --- a/internal/dataloader.go +++ b/internal/dataloader.go @@ -8,7 +8,6 @@ import ( "github.com/graph-gophers/dataloader" "github.com/minio/minio-go/v7" - gocache "github.com/patrickmn/go-cache" ) // listObjectsBatch batch func for calling s3.ListObjects() @@ -21,15 +20,9 @@ func listObjectsBatch(c context.Context, k dataloader.Keys) []*dataloader.Result return handleLoaderError(k, fmt.Errorf("Failed to get s3Client from context")) } - objCache, ok := c.Value("objCache").(*gocache.Cache) - - if !ok { - return handleLoaderError(k, fmt.Errorf("Failed to get objCache from context")) - } - for _, v := range k { results = append(results, &dataloader.Result{ - Data: listObjects(s3Client, bucketName, v.String(), false, objCache), + Data: listObjects(s3Client, bucketName, v.String(), false), }) } @@ -37,17 +30,14 @@ func listObjectsBatch(c context.Context, k dataloader.Keys) []*dataloader.Result } // listObjects helper func for listObjectsBatch -func listObjects(s3Client *minio.Client, bukitName, path string, recursive bool, objCache *gocache.Cache) []minio.ObjectInfo { +func listObjects(s3Client *minio.Client, bukitName, path string, recursive bool) []minio.ObjectInfo { objectCh := s3Client.ListObjects(context.Background(), bukitName, minio.ListObjectsOptions{ Prefix: path, Recursive: false, }) result := make([]minio.ObjectInfo, 0) - for obj := range objectCh { - // Add to object cache - objCache.SetDefault(obj.Key, obj) result = append(result, obj) } @@ -113,18 +103,16 @@ func getFileBatch(c context.Context, k dataloader.Keys) []*dataloader.Result { return handleLoaderError(k, fmt.Errorf("Failed to get s3Client from context")) } - objCache, ok := c.Value("objCache").(*gocache.Cache) - - if !ok { - return handleLoaderError(k, fmt.Errorf("Failed to get objCache from context")) - } - for _, v := range k { - cachedObj, ok := objCache.Get(v.String()) + obj, err := s3Client.StatObject(context.Background(), bucketName, v.String(), minio.StatObjectOptions{}) - if ok { - obj := cachedObj.(minio.ObjectInfo) + if err != nil { + results = append(results, &dataloader.Result{ + Data: nil, + Error: err, + }) + } else { results = append(results, &dataloader.Result{ Data: &File{ ID: obj.Key, @@ -134,26 +122,8 @@ func getFileBatch(c context.Context, k dataloader.Keys) []*dataloader.Result { }, Error: nil, }) - } else { - obj, err := s3Client.StatObject(context.Background(), bucketName, v.String(), minio.StatObjectOptions{}) - - if err != nil { - results = append(results, &dataloader.Result{ - Data: nil, - Error: err, - }) - } else { - results = append(results, &dataloader.Result{ - Data: &File{ - ID: obj.Key, - Size: obj.Size, - ContentType: obj.ContentType, - ETag: obj.ETag, - }, - Error: nil, - }) - } } + } return results @@ -219,7 +189,7 @@ func handleLoaderError(k dataloader.Keys, err error) []*dataloader.Result { } // createDataloader create all dataloaders and return a map of them plus a cache for objects -func createDataloader(config AppConfig) (map[string]*dataloader.Loader, *gocache.Cache) { +func createDataloader(config AppConfig) map[string]*dataloader.Loader { loaderMap := make(map[string]*dataloader.Loader, 0) loaderMap["getFiles"] = dataloader.NewBatchedLoader(getFilesBatch, dataloader.WithCache(newCache(config.CacheTTL, config.CacheCleanup))) @@ -227,5 +197,5 @@ func createDataloader(config AppConfig) (map[string]*dataloader.Loader, *gocache 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, gocache.New(config.CacheTTL, config.CacheCleanup) + return loaderMap } diff --git a/internal/s3Broswer.go b/internal/s3Broswer.go index b08cb39..c5fba84 100644 --- a/internal/s3Broswer.go +++ b/internal/s3Broswer.go @@ -69,7 +69,7 @@ func setupS3Client(config AppConfig) *minio.Client { func Start(config AppConfig) { s3Client := setupS3Client(config) - loaderMap, objCache := createDataloader(config) + loaderMap := createDataloader(config) graphqlTypes() schema, err := graphqlSchema() @@ -80,7 +80,6 @@ func Start(config AppConfig) { resolveContext := context.WithValue(context.Background(), "s3Client", s3Client) resolveContext = context.WithValue(resolveContext, "loader", loaderMap) - resolveContext = context.WithValue(resolveContext, "objCache", objCache) initHttp(schema, resolveContext) }