2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-30 00:33:57 +00:00

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() arch.m = NewMap()
// init cache // init cache
arch.c, err = NewCache() arch.c, err = NewCache(s)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -7,22 +7,29 @@ import (
"path/filepath" "path/filepath"
"github.com/restic/restic/backend" "github.com/restic/restic/backend"
"github.com/restic/restic/debug"
) )
// for testing
var getCacheDir = GetCacheDir
type Cache struct { type Cache struct {
base string base string
} }
func NewCache() (*Cache, error) { func NewCache(be backend.IDer) (c *Cache, err error) {
dir, err := getCacheDir() // 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 { if err != nil {
return nil, err 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) { 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 return err
} }
cache, err := restic.NewCache() cache, err := restic.NewCache(s)
if err != nil { if err != nil {
return err return err
} }

View File

@ -79,7 +79,7 @@ func (cmd CmdInit) Execute(args []string) error {
os.Exit(1) 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 return nil
} }

View File

@ -6,6 +6,7 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath"
"testing" "testing"
"github.com/restic/restic" "github.com/restic/restic"
@ -22,7 +23,12 @@ func setupBackend(t testing.TB) restic.Server {
tempdir, err := ioutil.TempDir(*testTempDir, "restic-test-") tempdir, err := ioutil.TempDir(*testTempDir, "restic-test-")
ok(t, err) 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) ok(t, err)
return restic.NewServer(b) return restic.NewServer(b)