Make sure "Clear Cache" works correctly

This commit is contained in:
Reinhard Pointner 2016-03-08 16:21:07 +00:00
parent 19197626d7
commit 7eb9192736
2 changed files with 23 additions and 13 deletions

View File

@ -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(); String cacheName = name.toLowerCase() + "_" + type.ordinal();
if (!manager.cacheExists(cacheName)) { if (!manager.cacheExists(cacheName)) {
debug.config("Create cache: " + cacheName); debug.config("Create cache: " + cacheName);
@ -44,11 +44,15 @@ public class CacheManager {
return new Cache(manager.getCache(cacheName)); return new Cache(manager.getCache(cacheName));
} }
public void clearAll() { public synchronized void clearAll() {
manager.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(); manager.shutdown();
} }
@ -58,6 +62,14 @@ public class CacheManager {
return config; 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 { private DiskStoreConfiguration getDiskStoreConfiguration() throws IOException {
// prepare cache folder for this application instance // prepare cache folder for this application instance
File cacheRoot = getApplicationCache().getCanonicalFile(); File cacheRoot = getApplicationCache().getCanonicalFile();
@ -92,13 +104,10 @@ public class CacheManager {
isNewCache = true; isNewCache = true;
// delete all files related to previous cache instances // delete all files related to previous cache instances
for (File it : getChildren(cache)) { clearDiskStore(cache);
if (!it.equals(lockFile)) {
delete(cache);
}
}
} }
// TODO: use UTF8
if (isNewCache) { if (isNewCache) {
// set new cache revision // set new cache revision
channel.position(0); channel.position(0);

View File

@ -82,15 +82,13 @@ public class Main {
Settings.forPackage(Main.class).clear(); Settings.forPackage(Main.class).clear();
} }
// clear preferences and cache
if (args.clearCache()) { if (args.clearCache()) {
// clear preferences and cache
log.info("Clear cache and temporary files"); log.info("Clear cache and temporary files");
for (File folder : getChildren(getApplicationFolder().getCanonicalFile(), FOLDERS)) { for (File folder : getChildren(getApplicationFolder().getCanonicalFile(), FOLDERS)) {
if (delete(folder)) { log.config("* Delete " + folder);
log.config("* Delete " + folder); delete(folder);
}
} }
CacheManager.getInstance().clearAll(); CacheManager.getInstance().clearAll();
} }
@ -131,6 +129,9 @@ public class Main {
// update system properties // update system properties
System.setProperty("http.agent", String.format("%s %s", getApplicationName(), getApplicationVersion())); 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("swing.crossplatformlaf", "javax.swing.plaf.nimbus.NimbusLookAndFeel");
System.setProperty("grape.root", new File(getApplicationFolder(), "grape").getAbsolutePath()); System.setProperty("grape.root", new File(getApplicationFolder(), "grape").getAbsolutePath());
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog"); System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");