mirror of https://github.com/micromata/borgbackup-butler.git

Kai Reinhard
08.41.2018 c2489ecbd13878e205f8528eeb150d5cd0200d47
borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ButlerCache.java
@@ -5,13 +5,17 @@
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class ButlerCache {
    private static Logger log = LoggerFactory.getLogger(ButlerCache.class);
    public static final String CACHE_DIR_NAME = ".borgbutler";
    private static ButlerCache instance = new ButlerCache();
    private RepoInfoCache reposCache;
    private RepoInfoCache repoInfoCache;
    private RepoListCache repoListCache;
    private List<AbstractCache> caches;
    @JsonIgnore
    private File cacheDir;
@@ -20,12 +24,40 @@
        return instance;
    }
    public static RepoInfoCache getReposCache() {
        return instance.reposCache;
    public static RepoInfoCache getRepoInfoCache() {
        return instance.repoInfoCache;
    }
    public static RepoListCache getRepoListCache() {
        return instance.repoListCache;
    }
    public void read() {
        for (AbstractCache cache : caches) {
            cache.read();
        }
    }
    public void save() {
        reposCache.save();
        for (AbstractCache cache : caches) {
            cache.save();
        }
    }
    /**
     * Removes all cache files and clears all caches.
     */
    public void removeAllCacheFiles() {
        File[] files = cacheDir.listFiles();
        for (File file : files) {
            if (AbstractCache.isCacheFile(file)) {
                log.info("Deleting cache file: " + file.getAbsolutePath());
                file.delete();
            }
        }
        for (AbstractCache cache : caches) {
            cache.clear();
        }
    }
    private ButlerCache() {
@@ -35,6 +67,10 @@
            log.info("Creating cache dir: " + cacheDir.getAbsolutePath());
            cacheDir.mkdir();
        }
        reposCache = new RepoInfoCache(cacheDir);
        repoInfoCache = new RepoInfoCache(cacheDir);
        repoListCache = new RepoListCache(cacheDir);
        caches = new ArrayList<>();
        caches.add(repoInfoCache);
        caches.add(repoListCache);
    }
}