mirror of
https://github.com/mitb-archive/filebot
synced 2024-12-24 16:58:51 -05:00
Experiment with new CachedResource framework
This commit is contained in:
parent
7d3b099c07
commit
9605ab7e63
@ -10,7 +10,6 @@ import java.time.Duration;
|
||||
import java.util.Arrays;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import net.filebot.CachedResource2.Fetch;
|
||||
import net.filebot.CachedResource2.Transform;
|
||||
import net.sf.ehcache.Element;
|
||||
|
||||
@ -25,20 +24,16 @@ public class Cache {
|
||||
return CacheManager.getInstance().getCache(name.toLowerCase(), type);
|
||||
}
|
||||
|
||||
public CachedResource2<String, String> text(String key, Transform<String, URL> resource, Duration expirationTime, Fetch fetch) {
|
||||
return new CachedResource2<String, String>(key, resource, fetch, getText(UTF_8), String.class::cast, expirationTime, this);
|
||||
public CachedResource2<String, String> text(String key, Transform<String, URL> resource) {
|
||||
return new CachedResource2<String, String>(key, resource, fetchIfModified(), getText(UTF_8), String.class::cast, ONE_DAY, this);
|
||||
}
|
||||
|
||||
public CachedResource2<String, Document> xml(String key, Transform<String, URL> resource, Duration expirationTime) {
|
||||
return new CachedResource2<String, Document>(key, resource, fetchIfModified(), validateXml(getText(UTF_8)), getXml(String.class::cast), expirationTime, this);
|
||||
public CachedResource2<String, Document> xml(String key, Transform<String, URL> resource) {
|
||||
return new CachedResource2<String, Document>(key, resource, fetchIfModified(), validateXml(getText(UTF_8)), getXml(String.class::cast), ONE_DAY, this);
|
||||
}
|
||||
|
||||
public CachedResource2<String, Object> json(String key, Transform<String, URL> resource, Duration expirationTime) {
|
||||
return json(key, resource, expirationTime, fetchIfModified());
|
||||
}
|
||||
|
||||
public CachedResource2<String, Object> json(String key, Transform<String, URL> resource, Duration expirationTime, Fetch fetch) {
|
||||
return new CachedResource2<String, Object>(key, resource, fetch, validateJson(getText(UTF_8)), getJson(String.class::cast), expirationTime, this);
|
||||
public CachedResource2<String, Object> json(String key, Transform<String, URL> resource) {
|
||||
return new CachedResource2<String, Object>(key, resource, fetchIfModified(), validateJson(getText(UTF_8)), getJson(String.class::cast), ONE_DAY, this);
|
||||
}
|
||||
|
||||
private final net.sf.ehcache.Cache cache;
|
||||
|
@ -32,8 +32,8 @@ public class CachedResource2<K, R> {
|
||||
|
||||
private Duration expirationTime;
|
||||
|
||||
private int retryCountLimit;
|
||||
private Duration retryWaitTime;
|
||||
private int retryLimit;
|
||||
private Duration retryWait;
|
||||
|
||||
private final Cache cache;
|
||||
|
||||
@ -41,25 +41,40 @@ public class CachedResource2<K, R> {
|
||||
this(key, resource, fetch, parse, cast, DEFAULT_RETRY_LIMIT, DEFAULT_RETRY_DELAY, expirationTime, cache);
|
||||
}
|
||||
|
||||
public CachedResource2(K key, Transform<K, URL> resource, Fetch fetch, Transform<ByteBuffer, ? extends Object> parse, Transform<? super Object, R> cast, int retryCountLimit, Duration retryWaitTime, Duration expirationTime, Cache cache) {
|
||||
public CachedResource2(K key, Transform<K, URL> resource, Fetch fetch, Transform<ByteBuffer, ? extends Object> parse, Transform<? super Object, R> cast, int retryLimit, Duration retryWait, Duration expirationTime, Cache cache) {
|
||||
this.key = key;
|
||||
this.resource = resource;
|
||||
this.fetch = fetch;
|
||||
this.parse = parse;
|
||||
this.cast = cast;
|
||||
this.expirationTime = expirationTime;
|
||||
this.retryCountLimit = retryCountLimit;
|
||||
this.retryWaitTime = retryWaitTime;
|
||||
this.retryLimit = retryLimit;
|
||||
this.retryWait = retryWait;
|
||||
this.cache = cache;
|
||||
}
|
||||
|
||||
public synchronized CachedResource2<K, R> fetch(Fetch fetch) {
|
||||
this.fetch = fetch;
|
||||
return this;
|
||||
}
|
||||
|
||||
public synchronized CachedResource2<K, R> expire(Duration expirationTime) {
|
||||
this.expirationTime = expirationTime;
|
||||
return this;
|
||||
}
|
||||
|
||||
public synchronized CachedResource2<K, R> retry(int retryLimit) {
|
||||
this.retryLimit = retryLimit;
|
||||
return this;
|
||||
}
|
||||
|
||||
public synchronized R get() throws Exception {
|
||||
Object value = cache.computeIfStale(key, expirationTime, element -> {
|
||||
URL url = resource.transform(key);
|
||||
long lastModified = element == null ? 0 : element.getLatestOfCreationAndUpdateTime();
|
||||
|
||||
try {
|
||||
ByteBuffer data = retry(() -> fetch.fetch(url, lastModified), retryCountLimit, retryWaitTime);
|
||||
ByteBuffer data = retry(() -> fetch.fetch(url, lastModified), retryLimit, retryWait);
|
||||
|
||||
// 304 Not Modified
|
||||
if (data == null && element != null && element.getObjectValue() != null) {
|
||||
|
@ -272,7 +272,7 @@ public class Main {
|
||||
*/
|
||||
private static void checkUpdate() throws Exception {
|
||||
Cache cache = Cache.getCache(getApplicationName(), CacheType.Persistent);
|
||||
Document dom = cache.xml("update.url", s -> new URL(getApplicationProperty(s)), Cache.ONE_WEEK).get();
|
||||
Document dom = cache.xml("update.url", s -> new URL(getApplicationProperty(s))).expire(Cache.ONE_WEEK).retry(0).get();
|
||||
|
||||
// parse update xml
|
||||
final Map<String, String> update = streamChildren(dom.getFirstChild()).collect(toMap(n -> n.getNodeName(), n -> n.getTextContent().trim()));
|
||||
|
@ -138,7 +138,7 @@ public class OMDbClient implements MovieIdentificationService {
|
||||
String key = '?' + encodeParameters(parameters, true);
|
||||
|
||||
Cache cache = Cache.getCache(getName(), CacheType.Weekly);
|
||||
Object json = cache.json(key, s -> getResource(s), Cache.ONE_WEEK, withPermit(fetchIfModified(), r -> REQUEST_LIMIT.acquirePermit() != null)).get();
|
||||
Object json = cache.json(key, s -> getResource(s)).fetch(withPermit(fetchIfModified(), r -> REQUEST_LIMIT.acquirePermit() != null)).expire(Cache.ONE_WEEK).get();
|
||||
|
||||
return asMap(json);
|
||||
}
|
||||
|
@ -375,7 +375,7 @@ public class TMDbClient implements MovieIdentificationService {
|
||||
|
||||
Cache etagStorage = Cache.getCache("etag", CacheType.Monthly);
|
||||
Cache cache = Cache.getCache(getName(), CacheType.Monthly);
|
||||
String json = cache.text(key, s -> getResource(s), Cache.ONE_WEEK, withPermit(fetchIfNoneMatch(etagStorage), r -> REQUEST_LIMIT.acquirePermit() != null)).get();
|
||||
String json = cache.text(key, s -> getResource(s)).fetch(withPermit(fetchIfNoneMatch(etagStorage), r -> REQUEST_LIMIT.acquirePermit() != null)).expire(Cache.ONE_WEEK).get();
|
||||
|
||||
JSONObject object = (JSONObject) JSONValue.parse(json);
|
||||
if (object == null || object.isEmpty()) {
|
||||
|
@ -112,7 +112,7 @@ public class TVMazeClient extends AbstractEpisodeListProvider {
|
||||
|
||||
protected Object request(String resource) throws Exception {
|
||||
Cache cache = Cache.getCache(getName(), CacheType.Monthly);
|
||||
return cache.json(resource, s -> getResource(resource), Cache.ONE_DAY).get();
|
||||
return cache.json(resource, s -> getResource(resource)).get();
|
||||
}
|
||||
|
||||
protected URL getResource(String resource) throws Exception {
|
||||
|
@ -300,7 +300,7 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
|
||||
|
||||
protected Document getXmlResource(MirrorType mirror, String resource) throws Exception {
|
||||
Cache cache = Cache.getCache(getName(), CacheType.Monthly);
|
||||
return cache.xml(resource, s -> getResource(mirror, s), Cache.ONE_DAY).get();
|
||||
return cache.xml(resource, s -> getResource(mirror, s)).get();
|
||||
}
|
||||
|
||||
protected URL getResource(MirrorType mirror, String path) throws Exception {
|
||||
|
Loading…
Reference in New Issue
Block a user