| | |
| | | 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; |
| | |
| | | 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() { |
| | |
| | | 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); |
| | | } |
| | | } |