1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-08-13 17:03:45 -04:00
filebot/source/net/filebot/Cache.java

118 lines
2.7 KiB
Java
Raw Normal View History

2014-04-19 02:30:29 -04:00
package net.filebot;
2016-03-06 13:11:30 -05:00
import static net.filebot.Logging.*;
import java.io.Serializable;
2016-03-06 13:11:30 -05:00
import java.time.Duration;
import java.util.Arrays;
2016-03-06 13:11:30 -05:00
import java.util.concurrent.Callable;
import java.util.function.Predicate;
import net.sf.ehcache.Element;
public class Cache {
2016-03-06 13:11:30 -05:00
public static Cache getCache(String name, CacheType type) {
return CacheManager.getInstance().getCache(name.toLowerCase(), type);
}
private final net.sf.ehcache.Cache cache;
2016-03-06 13:11:30 -05:00
public Cache(net.sf.ehcache.Cache cache) {
this.cache = cache;
}
2016-03-06 13:11:30 -05:00
public Object get(Object key) {
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));
}
2016-03-06 13:11:30 -05:00
return null;
}
2016-03-06 13:11:30 -05:00
public Object computeIf(Object key, Predicate<Element> condition, Callable<?> callable) throws Exception {
// get if present
try {
Element element = cache.get(key);
2016-03-06 13:11:30 -05:00
if (element != null && condition.test(element)) {
return element.getObjectValue();
}
} catch (Exception e) {
2016-03-06 13:11:30 -05:00
debug.warning(format("Bad cache state: %s => %s", key, e));
}
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));
}
}
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));
}
}
2016-03-06 13:11:30 -05:00
@Deprecated
public <T> T get(Object key, Class<T> type) {
return type.cast(get(key));
}
@Deprecated
public static class Key implements Serializable {
protected Object[] fields;
public Key(Object... fields) {
this.fields = fields;
}
@Override
public int hashCode() {
return Arrays.hashCode(fields);
}
@Override
public boolean equals(Object other) {
if (other instanceof Key) {
return Arrays.equals(this.fields, ((Key) other).fields);
}
return false;
}
@Override
public String toString() {
return Arrays.toString(fields);
}
}
}