* make cache more resilient and fail-safe even if the key class structure changes

This commit is contained in:
Reinhard Pointner 2013-09-15 01:38:56 +00:00
parent 9551767595
commit 5b9ab0826a
1 changed files with 25 additions and 32 deletions

View File

@ -1,7 +1,5 @@
package net.sourceforge.filebot; package net.sourceforge.filebot;
import java.io.Serializable; import java.io.Serializable;
import java.util.Arrays; import java.util.Arrays;
import java.util.logging.Level; import java.util.logging.Level;
@ -10,7 +8,6 @@ import java.util.logging.Logger;
import net.sf.ehcache.CacheManager; import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element; import net.sf.ehcache.Element;
public class Cache { public class Cache {
public static Cache getCache(String name) { public static Cache getCache(String name) {
@ -19,12 +16,10 @@ public class Cache {
private final net.sf.ehcache.Cache cache; private final net.sf.ehcache.Cache cache;
protected Cache(net.sf.ehcache.Cache cache) { protected Cache(net.sf.ehcache.Cache cache) {
this.cache = cache; this.cache = cache;
} }
public void put(Object key, Object value) { public void put(Object key, Object value) {
try { try {
cache.put(new Element(key, value)); cache.put(new Element(key, value));
@ -34,12 +29,10 @@ public class Cache {
} }
} }
public Object get(Object key) { public Object get(Object key) {
return get(key, Object.class); return get(key, Object.class);
} }
public <T> T get(Object key, Class<T> type) { public <T> T get(Object key, Class<T> type) {
try { try {
Element element = cache.get(key); Element element = cache.get(key);
@ -47,39 +40,40 @@ public class Cache {
return type.cast(element.getValue()); return type.cast(element.getValue());
} }
} catch (Exception e) { } catch (Exception e) {
Logger.getLogger(Cache.class.getName()).log(Level.WARNING, e.getMessage(), e); Logger.getLogger(Cache.class.getName()).log(Level.WARNING, e.getMessage());
remove(key); // fail-safe remove(key); // fail-safe
} }
return null; return null;
} }
public void remove(Object key) { public void remove(Object key) {
try { try {
cache.remove(key); cache.remove(key);
} catch (Exception e) { } catch (Exception e1) {
Logger.getLogger(Cache.class.getName()).log(Level.WARNING, e.getMessage(), e); Logger.getLogger(Cache.class.getName()).log(Level.WARNING, e1.getMessage());
try {
Logger.getLogger(Cache.class.getName()).log(Level.INFO, "Cached data has become invalid: Clearing cache now");
cache.removeAll();
} catch (Exception e2) {
Logger.getLogger(Cache.class.getName()).log(Level.SEVERE, e2.getMessage());
}
} }
} }
public static class Key implements Serializable { public static class Key implements Serializable {
protected Object[] fields; protected Object[] fields;
public Key(Object... fields) { public Key(Object... fields) {
this.fields = fields; this.fields = fields;
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Arrays.hashCode(fields); return Arrays.hashCode(fields);
} }
@Override @Override
public boolean equals(Object other) { public boolean equals(Object other) {
if (other instanceof Key) { if (other instanceof Key) {
@ -89,7 +83,6 @@ public class Cache {
return false; return false;
} }
@Override @Override
public String toString() { public String toString() {
return Arrays.toString(fields); return Arrays.toString(fields);