more cache issues

This commit is contained in:
Djeeberjr 2021-11-22 01:34:22 +01:00
parent 686630b2df
commit c5ab0156fd
4 changed files with 50 additions and 18 deletions

9
internal/cache/cache.go vendored Normal file
View File

@ -0,0 +1,9 @@
package cache
import (
"github.com/graph-gophers/dataloader"
)
type S3Cache interface {
dataloader.Cache
}

View File

@ -30,7 +30,7 @@ func deleteMutation(ctx context.Context, id types.ID) error {
return err
}
ctx.Value("loader").(*loader.Loader).InvalidateCacheForFile(ctx, id)
ctx.Value("loader").(*loader.Loader).InvalidedCacheForId(ctx, id)
return nil
}
@ -69,7 +69,7 @@ func copyMutation(ctx context.Context, src, dest types.ID) (*types.File, error)
newID.Normalize()
ctx.Value("loader").(*loader.Loader).InvalidateCacheForFile(ctx, newID)
ctx.Value("loader").(*loader.Loader).InvalidedCacheForId(ctx, newID)
return &types.File{
ID: newID,
@ -121,11 +121,16 @@ func moveDirMutation(ctx context.Context, src, dest types.ID) ([]*types.File, er
deleteMutation(ctx, file.ID)
loader.InvalidedCacheForId(ctx, newID)
loader.InvalidedCacheForId(ctx, file.ID)
result = append(result, &types.File{
ID: newID,
})
}
loader.InvalidedCacheForId(ctx, src)
return result, nil
}
@ -170,7 +175,7 @@ func moveFileMutation(ctx context.Context, src, dest types.ID) (*types.File, err
newId.Normalize()
ctx.Value("loader").(*loader.Loader).InvalidateCacheForFile(ctx, newId)
ctx.Value("loader").(*loader.Loader).InvalidedCacheForId(ctx, newId)
return &types.File{
ID: newId,
@ -201,7 +206,7 @@ func createDirectory(ctx context.Context, id types.ID) (*types.Directory, error)
newID.Normalize()
ctx.Value("loader").(*loader.Loader).InvalidateCacheForDir(ctx, newID)
ctx.Value("loader").(*loader.Loader).InvalidedCacheForId(ctx, newID)
return &types.Directory{
ID: newID,
@ -254,7 +259,7 @@ func deleteDirectory(ctx context.Context, id types.ID) error {
}
}
loader.InvalidateCacheForDir(ctx, id)
loader.InvalidedCacheForId(ctx, id)
return nil
}

View File

@ -194,8 +194,7 @@ func httpPostFile(ctx context.Context, rw http.ResponseWriter, r *http.Request)
}
loader := ctx.Value("loader").(*loader.Loader)
loader.InvalidateCacheForFile(ctx, *id)
loader.InvalidateCacheForDir(ctx, *id.Parent())
loader.InvalidedCacheForId(ctx, *id)
rw.WriteHeader(http.StatusCreated)
}

View File

@ -6,6 +6,7 @@ import (
"path/filepath"
"strings"
"git.kapelle.org/niklas/s3browser/internal/cache"
"git.kapelle.org/niklas/s3browser/internal/helper"
types "git.kapelle.org/niklas/s3browser/internal/types"
"github.com/graph-gophers/dataloader"
@ -17,26 +18,43 @@ type Loader struct {
listObjectsRecursiveLoader *dataloader.Loader
statObjectLoader *dataloader.Loader
listBucketsLoader *dataloader.Loader
listObjectsLoaderCache cache.S3Cache
listObjectsRecursiveLoaderCache cache.S3Cache
statObjectLoaderCache cache.S3Cache
listBucketsLoaderCache cache.S3Cache
}
func NewLoader(config types.AppConfig) *Loader {
listObjectsLoaderCache := &dataloader.NoCache{}
listObjectsRecursiveLoaderCache := &dataloader.NoCache{}
statObjectLoaderCache := cache.NewTTLCache(config.CacheTTL, config.CacheCleanup)
listBucketsLoaderCache := cache.NewTTLCache(config.CacheTTL, config.CacheCleanup)
return &Loader{
listObjectsLoader: dataloader.NewBatchedLoader(
listObjectsBatch,
dataloader.WithCache(&dataloader.NoCache{}),
dataloader.WithCache(listObjectsLoaderCache),
),
listObjectsLoaderCache: listObjectsLoaderCache,
listObjectsRecursiveLoader: dataloader.NewBatchedLoader(
listObjectsRecursiveBatch,
dataloader.WithCache(&dataloader.NoCache{}),
dataloader.WithCache(listObjectsRecursiveLoaderCache),
),
listObjectsRecursiveLoaderCache: listObjectsRecursiveLoaderCache,
statObjectLoader: dataloader.NewBatchedLoader(
statObjectBatch,
dataloader.WithCache(&dataloader.NoCache{}),
dataloader.WithCache(statObjectLoaderCache),
),
statObjectLoaderCache: statObjectLoaderCache,
listBucketsLoader: dataloader.NewBatchedLoader(
listBucketsBatch,
dataloader.WithCache(&dataloader.NoCache{}),
dataloader.WithCache(listBucketsLoaderCache),
),
listBucketsLoaderCache: listBucketsLoaderCache,
}
}
@ -143,16 +161,17 @@ func (l *Loader) GetFilesRecursive(ctx context.Context, path types.ID) ([]types.
return files, nil
}
func (l *Loader) InvalidateCacheForFile(ctx context.Context, id types.ID) {
func (l *Loader) InvalidedCacheForId(ctx context.Context, id types.ID) {
parent := id.Parent()
l.statObjectLoader.Clear(ctx, id)
// Code below is useless for now until we use a propper cache for "listObjectsLoader" and "listObjectsRecursiveLoader"
// TODO: implement cache invalidation for "listObjectsLoader" and "listObjectsRecursiveLoader"
l.listObjectsLoader.Clear(ctx, id).Clear(ctx, parent)
}
func (l *Loader) InvalidateCacheForDir(ctx context.Context, path types.ID) {
parent := helper.GetParentDir(path)
l.listObjectsLoader.Clear(ctx, path).Clear(ctx, parent)
l.listObjectsRecursiveLoader.Clear(ctx, path).Clear(ctx, parent)
// Remove up from recursive list
for rParent := parent; rParent != nil; rParent = rParent.Parent() {
l.listObjectsRecursiveLoader.Clear(ctx, rParent)
}
}