cache invalidation for mutations

This commit is contained in:
Djeeberjr 2021-08-07 01:42:47 +02:00
parent 818264093f
commit ad80860368

View File

@ -3,7 +3,9 @@ package s3browser
import (
"context"
"fmt"
"path/filepath"
"github.com/graph-gophers/dataloader"
"github.com/minio/minio-go/v7"
)
@ -16,7 +18,23 @@ func deleteMutation(ctx context.Context, id string) error {
// TODO: it is posible to remove multiple objects with a single call.
// Is it better to batch this?
return s3Client.RemoveObject(ctx, bucketName, id, minio.RemoveObjectOptions{})
err := s3Client.RemoveObject(ctx, bucketName, id, minio.RemoveObjectOptions{})
if err != nil {
return err
}
// Invalidate cache
loader, ok := ctx.Value("loader").(map[string]*dataloader.Loader)
if !ok {
return fmt.Errorf("Failed to get loader from context")
}
loader["getFile"].Clear(ctx, dataloader.StringKey(id))
loader["listObjects"].Clear(ctx, dataloader.StringKey(id))
loader["getFiles"].Clear(ctx, dataloader.StringKey(filepath.Dir(id)))
return nil
}
func copyMutation(ctx context.Context, src, dest string) (*File, error) {
@ -38,6 +56,18 @@ func copyMutation(ctx context.Context, src, dest string) (*File, error) {
return nil, err
}
// Invalidate cache
loader, ok := ctx.Value("loader").(map[string]*dataloader.Loader)
// TODO: Do we want to error when the operation
// has succeeded but the cache invalidation has failed ?
if ok {
loader["getFile"].Clear(ctx, dataloader.StringKey(info.Key))
loader["listObjects"].Clear(ctx, dataloader.StringKey(info.Key))
loader["getFiles"].Clear(ctx, dataloader.StringKey(filepath.Dir(info.Key)))
}
return &File{
ID: info.Key,
}, nil