implemented ttl cache
This commit is contained in:
parent
5746e79e8f
commit
23c67e0b81
@ -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,
|
||||
})
|
||||
}
|
||||
|
1
go.mod
1
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
|
||||
)
|
||||
|
2
go.sum
2
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=
|
||||
|
@ -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()
|
||||
}
|
Loading…
Reference in New Issue
Block a user