Files
kopia/cli/command_cache_clear.go
Jarek Kowalski c990fc9ec1 cache: streamlined flags and cache handling (#831)
* cache: improved cache cleanup on exit

Ensure we do one full sweep before closing if cache has been modified.

Before we would do periodic sweep every minute which would not kick in
for very short snapshots, which Kopia does very frequently. This leads
to build-up of metadata cache entries (q blobs) that never
get cleaned until some long session.

* caching: streamlined cache handling

- deprecated caching-related flags, now cache is always on or off with
  no way to disable it per invocation.
- reduced default list cache duration from 10min to 30s
- moved blob-list cache to separate subdirectory
- cleaned up cache info output to include blob-list cache parameters
- removed ability to disable cache for per-context (this was only
  used in 'snapshot verify' codepath)
- added ability to partially clear individual caches via CLI
2021-02-16 17:30:49 -08:00

57 lines
1.4 KiB
Go

package cli
import (
"context"
"os"
"path/filepath"
"github.com/pkg/errors"
"github.com/kopia/kopia/internal/retry"
"github.com/kopia/kopia/repo"
)
var (
cacheClearCommand = cacheCommands.Command("clear", "Clears the cache")
cacheClearCommandPartial = cacheClearCommand.Flag("partial", "Specifies the cache to clear").Enum("contents", "indexes", "metadata", "own-writes", "blob-list")
)
func runCacheClearCommand(ctx context.Context, rep repo.DirectRepository) error {
d := rep.CachingOptions().CacheDirectory
if d == "" {
return errors.New("caching not enabled")
}
// close repository before removing cache
if err := rep.Close(ctx); err != nil {
return errors.Wrap(err, "unable to close repository")
}
if *cacheClearCommandPartial == "" {
return clearCacheDirectory(ctx, d)
}
return clearCacheDirectory(ctx, filepath.Join(d, *cacheClearCommandPartial))
}
func clearCacheDirectory(ctx context.Context, d string) error {
log(ctx).Infof("Clearing cache directory: %v.", d)
err := retry.WithExponentialBackoffNoValue(ctx, "delete cache", func() error {
return os.RemoveAll(d)
}, retry.Always)
if err != nil {
return errors.Wrap(err, "error removing cache directory")
}
if err := os.MkdirAll(d, 0o700); err != nil {
return errors.Wrap(err, "error creating cache directory")
}
return nil
}
func init() {
cacheClearCommand.Action(directRepositoryReadAction(runCacheClearCommand))
}