mirror of
https://github.com/restic/restic.git
synced 2025-10-09 02:13:03 +00:00
Simplify cache logic
This commit is contained in:
33
internal/cache/backend.go
vendored
33
internal/cache/backend.go
vendored
@@ -21,7 +21,7 @@ type Backend struct {
|
||||
inProgress map[restic.Handle]chan struct{}
|
||||
}
|
||||
|
||||
// ensure cachedBackend implements restic.Backend
|
||||
// ensure Backend implements restic.Backend
|
||||
var _ restic.Backend = &Backend{}
|
||||
|
||||
func newBackend(be restic.Backend, c *Cache) *Backend {
|
||||
@@ -43,13 +43,19 @@ func (b *Backend) Remove(ctx context.Context, h restic.Handle) error {
|
||||
return b.Cache.remove(h)
|
||||
}
|
||||
|
||||
func autoCached(t restic.FileType) bool {
|
||||
return t == restic.IndexFile || t == restic.SnapshotFile
|
||||
func autoCacheTypes(h restic.Handle) bool {
|
||||
switch h.Type {
|
||||
case restic.IndexFile, restic.SnapshotFile:
|
||||
return true
|
||||
case restic.PackFile:
|
||||
return h.ContainedBlobType == restic.TreeBlob
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// 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 !autoCached(h.Type) {
|
||||
if !autoCacheTypes(h) {
|
||||
return b.Backend.Save(ctx, h, rd)
|
||||
}
|
||||
|
||||
@@ -168,25 +174,8 @@ func (b *Backend) Load(ctx context.Context, h restic.Handle, length int, offset
|
||||
debug.Log("error loading %v from cache: %v", h, err)
|
||||
}
|
||||
|
||||
// partial file requested
|
||||
if offset != 0 || length != 0 {
|
||||
if b.Cache.PerformReadahead(h) {
|
||||
debug.Log("performing readahead for %v", h)
|
||||
|
||||
err := b.cacheFile(ctx, h)
|
||||
if err == nil {
|
||||
return b.loadFromCacheOrDelegate(ctx, h, length, offset, consumer)
|
||||
}
|
||||
|
||||
debug.Log("error caching %v: %v", h, err)
|
||||
}
|
||||
|
||||
debug.Log("Load(%v, %v, %v): partial file requested, delegating to backend", h, length, offset)
|
||||
return b.Backend.Load(ctx, h, length, offset, consumer)
|
||||
}
|
||||
|
||||
// if we don't automatically cache this file type, fall back to the backend
|
||||
if !autoCached(h.Type) {
|
||||
if !autoCacheTypes(h) {
|
||||
debug.Log("Load(%v, %v, %v): delegating to backend", h, length, offset)
|
||||
return b.Backend.Load(ctx, h, length, offset, consumer)
|
||||
}
|
||||
|
11
internal/cache/cache.go
vendored
11
internal/cache/cache.go
vendored
@@ -17,10 +17,9 @@ import (
|
||||
|
||||
// Cache manages a local cache.
|
||||
type Cache struct {
|
||||
path string
|
||||
Base string
|
||||
Created bool
|
||||
PerformReadahead func(restic.Handle) bool
|
||||
path string
|
||||
Base string
|
||||
Created bool
|
||||
}
|
||||
|
||||
const dirMode = 0700
|
||||
@@ -152,10 +151,6 @@ func New(id string, basedir string) (c *Cache, err error) {
|
||||
path: cachedir,
|
||||
Base: basedir,
|
||||
Created: created,
|
||||
PerformReadahead: func(restic.Handle) bool {
|
||||
// do not perform readahead by default
|
||||
return false
|
||||
},
|
||||
}
|
||||
|
||||
return c, nil
|
||||
|
Reference in New Issue
Block a user