2014-04-19 02:30:29 -04:00
|
|
|
package net.filebot;
|
2012-07-25 00:34:20 -04:00
|
|
|
|
2016-03-06 13:11:30 -05:00
|
|
|
import static net.filebot.Logging.*;
|
|
|
|
|
2012-07-25 00:34:20 -04:00
|
|
|
import java.io.Serializable;
|
2016-03-06 13:11:30 -05:00
|
|
|
import java.time.Duration;
|
2012-07-25 00:34:20 -04:00
|
|
|
import java.util.Arrays;
|
2016-03-06 13:11:30 -05:00
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
import java.util.function.Predicate;
|
2012-07-25 00:34:20 -04:00
|
|
|
|
|
|
|
import net.sf.ehcache.Element;
|
|
|
|
|
|
|
|
public class Cache {
|
2013-09-14 21:38:56 -04:00
|
|
|
|
2016-03-06 13:11:30 -05:00
|
|
|
public static Cache getCache(String name, CacheType type) {
|
|
|
|
return CacheManager.getInstance().getCache(name.toLowerCase(), type);
|
2012-07-25 00:34:20 -04:00
|
|
|
}
|
2013-09-14 21:38:56 -04:00
|
|
|
|
2012-07-25 00:34:20 -04:00
|
|
|
private final net.sf.ehcache.Cache cache;
|
2013-09-14 21:38:56 -04:00
|
|
|
|
2016-03-06 13:11:30 -05:00
|
|
|
public Cache(net.sf.ehcache.Cache cache) {
|
2012-07-25 00:34:20 -04:00
|
|
|
this.cache = cache;
|
|
|
|
}
|
2013-09-14 21:38:56 -04:00
|
|
|
|
2016-03-06 13:11:30 -05:00
|
|
|
public Object get(Object key) {
|
2012-07-25 00:34:20 -04:00
|
|
|
try {
|
2016-03-06 13:11:30 -05:00
|
|
|
return cache.get(key).getObjectValue();
|
|
|
|
} catch (Exception e) {
|
|
|
|
debug.warning(format("Bad cache state: %s => %s", key, e));
|
2012-07-25 00:34:20 -04:00
|
|
|
}
|
2016-03-06 13:11:30 -05:00
|
|
|
return null;
|
2012-07-25 00:34:20 -04:00
|
|
|
}
|
2013-09-14 21:38:56 -04:00
|
|
|
|
2016-03-06 13:11:30 -05:00
|
|
|
public Object computeIf(Object key, Predicate<Element> condition, Callable<?> callable) throws Exception {
|
|
|
|
// get if present
|
2012-07-25 00:34:20 -04:00
|
|
|
try {
|
|
|
|
Element element = cache.get(key);
|
2016-03-06 13:11:30 -05:00
|
|
|
if (element != null && condition.test(element)) {
|
|
|
|
return element.getObjectValue();
|
2012-07-25 00:34:20 -04:00
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
2016-03-06 13:11:30 -05:00
|
|
|
debug.warning(format("Bad cache state: %s => %s", key, e));
|
2012-07-25 00:34:20 -04:00
|
|
|
}
|
2013-09-14 21:38:56 -04:00
|
|
|
|
2016-03-06 13:11:30 -05:00
|
|
|
// compute if absent
|
|
|
|
Object value = callable.call();
|
|
|
|
try {
|
|
|
|
cache.put(new Element(key, value));
|
|
|
|
} catch (Exception e) {
|
|
|
|
debug.warning(format("Bad cache state: %s => %s", key, e));
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Object computeIfAbsent(Object key, Callable<?> callable) throws Exception {
|
|
|
|
return computeIf(key, Element::isExpired, callable);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Object computeIfStale(Object key, Duration expirationTime, Callable<?> callable) throws Exception {
|
|
|
|
return computeIf(key, isStale(expirationTime), callable);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Predicate<Element> isStale(Duration expirationTime) {
|
|
|
|
return (element) -> element.isExpired() || System.currentTimeMillis() - element.getLatestOfCreationAndUpdateTime() < expirationTime.toMillis();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void put(Object key, Object value) {
|
|
|
|
try {
|
|
|
|
cache.put(new Element(key, value));
|
|
|
|
} catch (Exception e) {
|
|
|
|
debug.warning(format("Bad cache state: %s => %s", key, e));
|
|
|
|
}
|
2012-07-25 00:34:20 -04:00
|
|
|
}
|
2013-09-14 21:38:56 -04:00
|
|
|
|
2012-07-25 00:34:20 -04:00
|
|
|
public void remove(Object key) {
|
|
|
|
try {
|
|
|
|
cache.remove(key);
|
2016-03-06 13:11:30 -05:00
|
|
|
} catch (Exception e) {
|
|
|
|
debug.warning(format("Bad cache state: %s => %s", key, e));
|
2012-07-25 00:34:20 -04:00
|
|
|
}
|
|
|
|
}
|
2013-09-14 21:38:56 -04:00
|
|
|
|
2016-03-06 13:11:30 -05:00
|
|
|
@Deprecated
|
|
|
|
public <T> T get(Object key, Class<T> type) {
|
|
|
|
return type.cast(get(key));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Deprecated
|
2012-07-25 00:34:20 -04:00
|
|
|
public static class Key implements Serializable {
|
2013-09-14 21:38:56 -04:00
|
|
|
|
2012-07-25 00:34:20 -04:00
|
|
|
protected Object[] fields;
|
2013-09-14 21:38:56 -04:00
|
|
|
|
2012-07-25 00:34:20 -04:00
|
|
|
public Key(Object... fields) {
|
|
|
|
this.fields = fields;
|
|
|
|
}
|
2013-09-14 21:38:56 -04:00
|
|
|
|
2012-07-25 00:34:20 -04:00
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return Arrays.hashCode(fields);
|
|
|
|
}
|
2013-09-14 21:38:56 -04:00
|
|
|
|
2012-07-25 00:34:20 -04:00
|
|
|
@Override
|
|
|
|
public boolean equals(Object other) {
|
|
|
|
if (other instanceof Key) {
|
|
|
|
return Arrays.equals(this.fields, ((Key) other).fields);
|
|
|
|
}
|
2013-09-14 21:38:56 -04:00
|
|
|
|
2012-07-25 00:34:20 -04:00
|
|
|
return false;
|
|
|
|
}
|
2013-09-14 21:38:56 -04:00
|
|
|
|
2012-07-26 07:09:42 -04:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return Arrays.toString(fields);
|
|
|
|
}
|
2012-07-25 00:34:20 -04:00
|
|
|
}
|
2013-09-14 21:38:56 -04:00
|
|
|
|
2012-07-25 00:34:20 -04:00
|
|
|
}
|