From 7eb919273630517cb9d62a5ff261c2ec2feefcd3 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Tue, 8 Mar 2016 16:21:07 +0000 Subject: [PATCH] Make sure "Clear Cache" works correctly --- source/net/filebot/CacheManager.java | 25 +++++++++++++++++-------- source/net/filebot/Main.java | 11 ++++++----- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/source/net/filebot/CacheManager.java b/source/net/filebot/CacheManager.java index 30714851..57b01038 100644 --- a/source/net/filebot/CacheManager.java +++ b/source/net/filebot/CacheManager.java @@ -35,7 +35,7 @@ public class CacheManager { } } - public Cache getCache(String name, CacheType type) { + public synchronized Cache getCache(String name, CacheType type) { String cacheName = name.toLowerCase() + "_" + type.ordinal(); if (!manager.cacheExists(cacheName)) { debug.config("Create cache: " + cacheName); @@ -44,11 +44,15 @@ public class CacheManager { return new Cache(manager.getCache(cacheName)); } - public void clearAll() { + public synchronized void clearAll() { manager.clearAll(); + manager.removeAllCaches(); + + // clear all caches that have not been added yet + clearDiskStore(new File(manager.getConfiguration().getDiskStoreConfiguration().getPath())); } - public void shutdown() { + public synchronized void shutdown() { manager.shutdown(); } @@ -58,6 +62,14 @@ public class CacheManager { return config; } + private void clearDiskStore(File cache) { + getChildren(cache).stream().filter(f -> f.isFile() && !f.getName().startsWith(".")).forEach(f -> { + if (!delete(f)) { + debug.warning(format("Failed to delete cache: %s", f.getName())); + } + }); + } + private DiskStoreConfiguration getDiskStoreConfiguration() throws IOException { // prepare cache folder for this application instance File cacheRoot = getApplicationCache().getCanonicalFile(); @@ -92,13 +104,10 @@ public class CacheManager { isNewCache = true; // delete all files related to previous cache instances - for (File it : getChildren(cache)) { - if (!it.equals(lockFile)) { - delete(cache); - } - } + clearDiskStore(cache); } + // TODO: use UTF8 if (isNewCache) { // set new cache revision channel.position(0); diff --git a/source/net/filebot/Main.java b/source/net/filebot/Main.java index 183b55b2..062a74f3 100644 --- a/source/net/filebot/Main.java +++ b/source/net/filebot/Main.java @@ -82,15 +82,13 @@ public class Main { Settings.forPackage(Main.class).clear(); } + // clear preferences and cache if (args.clearCache()) { - // clear preferences and cache log.info("Clear cache and temporary files"); for (File folder : getChildren(getApplicationFolder().getCanonicalFile(), FOLDERS)) { - if (delete(folder)) { - log.config("* Delete " + folder); - } + log.config("* Delete " + folder); + delete(folder); } - CacheManager.getInstance().clearAll(); } @@ -131,6 +129,9 @@ public class Main { // update system properties System.setProperty("http.agent", String.format("%s %s", getApplicationName(), getApplicationVersion())); + System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); + System.setProperty("sun.net.client.defaultReadTimeout", "60000"); + System.setProperty("swing.crossplatformlaf", "javax.swing.plaf.nimbus.NimbusLookAndFeel"); System.setProperty("grape.root", new File(getApplicationFolder(), "grape").getAbsolutePath()); System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");