Implement per-repository cache

This commit is contained in:
Alexander Neumann 2015-03-14 12:10:08 +01:00
parent f69a39cff5
commit e1fc17aeb1
5 changed files with 25 additions and 12 deletions

View File

@ -53,7 +53,7 @@ func NewArchiver(s Server) (*Archiver, error) {
arch.m = NewMap()
// init cache
arch.c, err = NewCache()
arch.c, err = NewCache(s)
if err != nil {
return nil, err
}

View File

@ -7,22 +7,29 @@ import (
"path/filepath"
"github.com/restic/restic/backend"
"github.com/restic/restic/debug"
)
// for testing
var getCacheDir = GetCacheDir
type Cache struct {
base string
}
func NewCache() (*Cache, error) {
dir, err := getCacheDir()
if err != nil {
return nil, err
func NewCache(be backend.IDer) (c *Cache, err error) {
// try to get explicit cache dir from environment
dir := os.Getenv("RESTIC_CACHE")
// otherwise try OS specific default
if dir == "" {
dir, err = GetCacheDir()
if err != nil {
return nil, err
}
}
return &Cache{base: dir}, nil
basedir := filepath.Join(dir, be.ID().String())
debug.Log("Cache.New", "opened cache at %v", basedir)
return &Cache{base: basedir}, nil
}
func (c *Cache) Has(t backend.Type, subtype string, id backend.ID) (bool, error) {

View File

@ -42,7 +42,7 @@ func (cmd CmdCache) Execute(args []string) error {
return err
}
cache, err := restic.NewCache()
cache, err := restic.NewCache(s)
if err != nil {
return err
}

View File

@ -79,7 +79,7 @@ func (cmd CmdInit) Execute(args []string) error {
os.Exit(1)
}
fmt.Printf("created restic backend at %s\n", opts.Repo)
fmt.Printf("created restic backend %v at %s\n", s.ID().Str(), opts.Repo)
return nil
}

View File

@ -6,6 +6,7 @@ import (
"io"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/restic/restic"
@ -22,7 +23,12 @@ func setupBackend(t testing.TB) restic.Server {
tempdir, err := ioutil.TempDir(*testTempDir, "restic-test-")
ok(t, err)
b, err := backend.CreateLocal(tempdir)
// create repository below temp dir
b, err := backend.CreateLocal(filepath.Join(tempdir, "repo"))
ok(t, err)
// set cache dir
err = os.Setenv("RESTIC_CACHE", filepath.Join(tempdir, "cache"))
ok(t, err)
return restic.NewServer(b)