From 9605ab7e6370edb635f32b5aa72b26f6473dcbc7 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Mon, 7 Mar 2016 16:01:10 +0000 Subject: [PATCH] Experiment with new CachedResource framework --- source/net/filebot/Cache.java | 17 +++++--------- source/net/filebot/CachedResource2.java | 27 ++++++++++++++++++----- source/net/filebot/Main.java | 2 +- source/net/filebot/web/OMDbClient.java | 2 +- source/net/filebot/web/TMDbClient.java | 2 +- source/net/filebot/web/TVMazeClient.java | 2 +- source/net/filebot/web/TheTVDBClient.java | 2 +- 7 files changed, 32 insertions(+), 22 deletions(-) diff --git a/source/net/filebot/Cache.java b/source/net/filebot/Cache.java index 3b2d7daf..96657468 100644 --- a/source/net/filebot/Cache.java +++ b/source/net/filebot/Cache.java @@ -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 text(String key, Transform resource, Duration expirationTime, Fetch fetch) { - return new CachedResource2(key, resource, fetch, getText(UTF_8), String.class::cast, expirationTime, this); + public CachedResource2 text(String key, Transform resource) { + return new CachedResource2(key, resource, fetchIfModified(), getText(UTF_8), String.class::cast, ONE_DAY, this); } - public CachedResource2 xml(String key, Transform resource, Duration expirationTime) { - return new CachedResource2(key, resource, fetchIfModified(), validateXml(getText(UTF_8)), getXml(String.class::cast), expirationTime, this); + public CachedResource2 xml(String key, Transform resource) { + return new CachedResource2(key, resource, fetchIfModified(), validateXml(getText(UTF_8)), getXml(String.class::cast), ONE_DAY, this); } - public CachedResource2 json(String key, Transform resource, Duration expirationTime) { - return json(key, resource, expirationTime, fetchIfModified()); - } - - public CachedResource2 json(String key, Transform resource, Duration expirationTime, Fetch fetch) { - return new CachedResource2(key, resource, fetch, validateJson(getText(UTF_8)), getJson(String.class::cast), expirationTime, this); + public CachedResource2 json(String key, Transform resource) { + return new CachedResource2(key, resource, fetchIfModified(), validateJson(getText(UTF_8)), getJson(String.class::cast), ONE_DAY, this); } private final net.sf.ehcache.Cache cache; diff --git a/source/net/filebot/CachedResource2.java b/source/net/filebot/CachedResource2.java index bec9315f..43661965 100644 --- a/source/net/filebot/CachedResource2.java +++ b/source/net/filebot/CachedResource2.java @@ -32,8 +32,8 @@ public class CachedResource2 { 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 { this(key, resource, fetch, parse, cast, DEFAULT_RETRY_LIMIT, DEFAULT_RETRY_DELAY, expirationTime, cache); } - public CachedResource2(K key, Transform resource, Fetch fetch, Transform parse, Transform cast, int retryCountLimit, Duration retryWaitTime, Duration expirationTime, Cache cache) { + public CachedResource2(K key, Transform resource, Fetch fetch, Transform parse, Transform 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 fetch(Fetch fetch) { + this.fetch = fetch; + return this; + } + + public synchronized CachedResource2 expire(Duration expirationTime) { + this.expirationTime = expirationTime; + return this; + } + + public synchronized CachedResource2 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) { diff --git a/source/net/filebot/Main.java b/source/net/filebot/Main.java index d213fc89..183b55b2 100644 --- a/source/net/filebot/Main.java +++ b/source/net/filebot/Main.java @@ -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 update = streamChildren(dom.getFirstChild()).collect(toMap(n -> n.getNodeName(), n -> n.getTextContent().trim())); diff --git a/source/net/filebot/web/OMDbClient.java b/source/net/filebot/web/OMDbClient.java index ba572202..1fa0d925 100644 --- a/source/net/filebot/web/OMDbClient.java +++ b/source/net/filebot/web/OMDbClient.java @@ -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); } diff --git a/source/net/filebot/web/TMDbClient.java b/source/net/filebot/web/TMDbClient.java index e38667b8..58bb178e 100644 --- a/source/net/filebot/web/TMDbClient.java +++ b/source/net/filebot/web/TMDbClient.java @@ -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()) { diff --git a/source/net/filebot/web/TVMazeClient.java b/source/net/filebot/web/TVMazeClient.java index feaf92f9..c4e65a25 100644 --- a/source/net/filebot/web/TVMazeClient.java +++ b/source/net/filebot/web/TVMazeClient.java @@ -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 { diff --git a/source/net/filebot/web/TheTVDBClient.java b/source/net/filebot/web/TheTVDBClient.java index 6c2a88ab..34660069 100644 --- a/source/net/filebot/web/TheTVDBClient.java +++ b/source/net/filebot/web/TheTVDBClient.java @@ -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 {