s3browser-backend/internal/helper.go

60 lines
1.3 KiB
Go
Raw Normal View History

2021-08-14 12:13:49 +00:00
package s3browser
import (
"context"
"fmt"
"path/filepath"
"github.com/graph-gophers/dataloader"
2021-08-16 20:40:10 +00:00
log "github.com/sirupsen/logrus"
2021-08-14 12:13:49 +00:00
)
func invalidateCache(ctx context.Context, id string) error {
loader, ok := ctx.Value("loader").(map[string]*dataloader.Loader)
if !ok {
return fmt.Errorf("Failed to get loader from context")
}
2021-08-16 20:40:10 +00:00
log.Debug("Invalidate cache for id: ", id)
2021-08-14 12:13:49 +00:00
loader["getFile"].Clear(ctx, dataloader.StringKey(id))
loader["getFiles"].Clear(ctx, dataloader.StringKey(getPathFromId(id)))
2021-08-16 20:40:10 +00:00
// FIXME: this is the only way i can invalidate the cache without it breaking stuff
// loader["listObjects"].Clear(ctx, dataloader.StringKey(id))
loader["listObjects"].ClearAll()
2021-08-14 12:13:49 +00:00
return nil
}
func getPathFromId(id string) string {
dir := filepath.Dir(id)
if dir == "." {
return "/"
}
return dir + "/"
}
2021-08-15 23:40:01 +00:00
func getFilenameFromID(id string) string {
return filepath.Base(id)
}
2021-08-16 20:16:42 +00:00
func invalidateCacheForDir(ctx context.Context, path string) error {
loader, ok := ctx.Value("loader").(map[string]*dataloader.Loader)
if !ok {
return fmt.Errorf("Failed to get loader from context")
}
log.Debug("Invalidate cache for dir: ", path)
// FIXME: only clear required ids
loader["getFile"].ClearAll()
loader["listObjects"].ClearAll()
loader["getFiles"].ClearAll()
loader["getDirs"].ClearAll()
return nil
}