forked from TrueCloudLab/restic
cache: Just try to open cache entry without calling stat first
Instead of first checking whether a file is in the repository cache and then opening it, we just can open the file. This saves one stat call. If the file is in the cache, everything is fine and otherwise the code follows its normal fallback path.
This commit is contained in:
parent
0d9ac78437
commit
ce902aac67
2 changed files with 11 additions and 13 deletions
22
internal/cache/backend.go
vendored
22
internal/cache/backend.go
vendored
|
@ -160,19 +160,17 @@ func (b *Backend) Load(ctx context.Context, h restic.Handle, length int, offset
|
||||||
debug.Log("downloading %v finished", h)
|
debug.Log("downloading %v finished", h)
|
||||||
}
|
}
|
||||||
|
|
||||||
if b.Cache.Has(h) {
|
// try loading from cache without checking that the handle is actually cached
|
||||||
debug.Log("Load(%v, %v, %v) from cache", h, length, offset)
|
rd, err := b.Cache.load(h, length, offset)
|
||||||
rd, err := b.Cache.load(h, length, offset)
|
if err == nil {
|
||||||
if err == nil {
|
err = consumer(rd)
|
||||||
err = consumer(rd)
|
if err != nil {
|
||||||
if err != nil {
|
_ = rd.Close() // ignore secondary errors
|
||||||
_ = rd.Close() // ignore secondary errors
|
return err
|
||||||
return err
|
|
||||||
}
|
|
||||||
return rd.Close()
|
|
||||||
}
|
}
|
||||||
debug.Log("error loading %v from cache: %v", h, err)
|
return rd.Close()
|
||||||
}
|
}
|
||||||
|
debug.Log("error loading %v from cache: %v", h, err)
|
||||||
|
|
||||||
// if we don't automatically cache this file type, fall back to the backend
|
// if we don't automatically cache this file type, fall back to the backend
|
||||||
if !autoCacheTypes(h) {
|
if !autoCacheTypes(h) {
|
||||||
|
@ -181,7 +179,7 @@ func (b *Backend) Load(ctx context.Context, h restic.Handle, length int, offset
|
||||||
}
|
}
|
||||||
|
|
||||||
debug.Log("auto-store %v in the cache", h)
|
debug.Log("auto-store %v in the cache", h)
|
||||||
err := b.cacheFile(ctx, h)
|
err = b.cacheFile(ctx, h)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return b.loadFromCacheOrDelegate(ctx, h, length, offset, consumer)
|
return b.loadFromCacheOrDelegate(ctx, h, length, offset, consumer)
|
||||||
}
|
}
|
||||||
|
|
2
internal/cache/file.go
vendored
2
internal/cache/file.go
vendored
|
@ -43,7 +43,7 @@ type readCloser struct {
|
||||||
// given handle. rd must be closed after use. If an error is returned, the
|
// given handle. rd must be closed after use. If an error is returned, the
|
||||||
// ReadCloser is nil.
|
// ReadCloser is nil.
|
||||||
func (c *Cache) load(h restic.Handle, length int, offset int64) (io.ReadCloser, error) {
|
func (c *Cache) load(h restic.Handle, length int, offset int64) (io.ReadCloser, error) {
|
||||||
debug.Log("Load from cache: %v", h)
|
debug.Log("Load(%v, %v, %v) from cache", h, length, offset)
|
||||||
if !c.canBeCached(h.Type) {
|
if !c.canBeCached(h.Type) {
|
||||||
return nil, errors.New("cannot be cached")
|
return nil, errors.New("cannot be cached")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue