implemented ttl cache
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
43
internal/ttl_cache.go
Normal file
43
internal/ttl_cache.go
Normal file
@@ -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()
|
||||
}
|
||||
Reference in New Issue
Block a user