Replace duplicate type checking in cache with a function

This commit is contained in:
greatroar 2020-07-17 10:24:22 +02:00 committed by Michael Eischer
parent 1b1a2115fa
commit f9b6f8fd45
1 changed files with 4 additions and 10 deletions

View File

@ -43,14 +43,13 @@ func (b *Backend) Remove(ctx context.Context, h restic.Handle) error {
return b.Cache.remove(h)
}
var autoCacheTypes = map[restic.FileType]struct{}{
restic.IndexFile: {},
restic.SnapshotFile: {},
func autoCached(t restic.FileType) bool {
return t == restic.IndexFile || t == restic.SnapshotFile
}
// Save stores a new file in the backend and the cache.
func (b *Backend) Save(ctx context.Context, h restic.Handle, rd restic.RewindReader) error {
if _, ok := autoCacheTypes[h.Type]; !ok {
if !autoCached(h.Type) {
return b.Backend.Save(ctx, h, rd)
}
@ -84,11 +83,6 @@ func (b *Backend) Save(ctx context.Context, h restic.Handle, rd restic.RewindRea
return nil
}
var autoCacheFiles = map[restic.FileType]bool{
restic.IndexFile: true,
restic.SnapshotFile: true,
}
func (b *Backend) cacheFile(ctx context.Context, h restic.Handle) error {
finish := make(chan struct{})
@ -192,7 +186,7 @@ func (b *Backend) Load(ctx context.Context, h restic.Handle, length int, offset
}
// if we don't automatically cache this file type, fall back to the backend
if _, ok := autoCacheFiles[h.Type]; !ok {
if !autoCached(h.Type) {
debug.Log("Load(%v, %v, %v): delegating to backend", h, length, offset)
return b.Backend.Load(ctx, h, length, offset, consumer)
}