From 1ab9d36938f2b3156581c59ef5a1f2956ef5561d Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Wed, 9 Mar 2016 19:51:41 +0000 Subject: [PATCH] Experiment with new CachedResource framework --- source/net/filebot/Cache.java | 8 ++++++++ source/net/filebot/format/MediaBindingBean.java | 2 +- .../filebot/web/AbstractEpisodeListProvider.java | 4 ++-- source/net/filebot/web/AcoustIDClient.java | 2 +- source/net/filebot/web/OpenSubtitlesClient.java | 16 ++++++++-------- source/net/filebot/web/ShooterSubtitles.java | 2 +- source/net/filebot/web/TheTVDBClient.java | 6 +++--- 7 files changed, 24 insertions(+), 16 deletions(-) diff --git a/source/net/filebot/Cache.java b/source/net/filebot/Cache.java index 7a329070..eb4081a9 100644 --- a/source/net/filebot/Cache.java +++ b/source/net/filebot/Cache.java @@ -76,6 +76,10 @@ public class Cache { return value; } + public Object computeIfAbsent(Object key, Compute compute) throws Exception { + return computeIf(key, isAbsent(), compute); + } + public void put(Object key, Object value) { try { cache.put(createElement(key, value)); @@ -155,6 +159,10 @@ public class Cache { return (V) super.computeIf(key, condition, compute); } + public V computeIfAbsent(Object key, Compute compute) throws Exception { + return (V) super.computeIfAbsent(key, compute); + } + @Override protected Object getElementValue(Element element) { return read.apply(super.getElementValue(element)); diff --git a/source/net/filebot/format/MediaBindingBean.java b/source/net/filebot/format/MediaBindingBean.java index 8da9ab34..043f4d4c 100644 --- a/source/net/filebot/format/MediaBindingBean.java +++ b/source/net/filebot/format/MediaBindingBean.java @@ -447,7 +447,7 @@ public class MediaBindingBean { // calculate checksum from file Cache cache = Cache.getCache("crc32", CacheType.Ephemeral); - return (String) cache.computeIf(inferredMediaFile.getCanonicalPath(), Cache.isAbsent(), it -> crc32(inferredMediaFile)); + return (String) cache.computeIfAbsent(inferredMediaFile.getCanonicalPath(), it -> crc32(inferredMediaFile)); } @Define("fn") diff --git a/source/net/filebot/web/AbstractEpisodeListProvider.java b/source/net/filebot/web/AbstractEpisodeListProvider.java index 676cd12e..875edf09 100644 --- a/source/net/filebot/web/AbstractEpisodeListProvider.java +++ b/source/net/filebot/web/AbstractEpisodeListProvider.java @@ -24,7 +24,7 @@ public abstract class AbstractEpisodeListProvider implements EpisodeListProvider @Override public List search(String query, Locale language) throws Exception { - return getSearchCache(language).computeIf(query, Cache.isAbsent(), it -> { + return getSearchCache(language).computeIfAbsent(query, it -> { return fetchSearchResult(query, language); }); } @@ -54,7 +54,7 @@ public abstract class AbstractEpisodeListProvider implements EpisodeListProvider SortOrder requestOrder = vetoRequestParameter(order); Locale requestLanguage = vetoRequestParameter(language); - return getDataCache(requestOrder, requestLanguage).computeIf(searchResult.getId(), Cache.isAbsent(), it -> { + return getDataCache(requestOrder, requestLanguage).computeIfAbsent(searchResult.getId(), it -> { return fetchSeriesData(searchResult, requestOrder, requestLanguage); }); } diff --git a/source/net/filebot/web/AcoustIDClient.java b/source/net/filebot/web/AcoustIDClient.java index 4a93b475..ffc72111 100644 --- a/source/net/filebot/web/AcoustIDClient.java +++ b/source/net/filebot/web/AcoustIDClient.java @@ -84,7 +84,7 @@ public class AcoustIDClient implements MusicIdentificationService { // e.g. // http://api.acoustid.org/v2/lookup?client=8XaBELgH&meta=recordings+releasegroups+compress&duration=641&fingerprint=AQABz0qUkZK4oOfhL-CPc4e5C_wW2H2QH9uDL4cvoT8UNQ-eHtsE8cceeFJx-LiiHT-aPzhxoc-Opj_eI5d2hOFyMJRzfDk-QSsu7fBxqZDMHcfxPfDIoPWxv9C1o3yg44d_3Df2GJaUQeeR-cb2HfaPNsdxHj2PJnpwPMN3aPcEMzd-_MeB_Ej4D_CLP8ghHjkJv_jh_UDuQ8xnILwunPg6hF2R8HgzvLhxHVYP_ziJX0eKPnIE1UePMByDJyg7wz_6yELsB8n4oDmDa0Gv40hf6D3CE3_wH6HFaxCPUD9-hNeF5MfWEP3SCGym4-SxnXiGs0mRjEXD6fgl4LmKWrSChzzC33ge9PB3otyJMk-IVC6R8MTNwD9qKQ_CC8kPv4THzEGZS8GPI3x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jCCAEEMZAUQAZQCjCCkpCgFMCCiIcVIAZZgilAQAiSHQECOcQAQIc4QClAHAjDDGkAGAMUoBgyhihgEChFCAAWEIEYwIJYwViAAlHCBIGEIEAEIQAoBwwgwiEBAEEEOoEwBY4wRwxAhBgAcKAESIQAwwIowRFhoBhAE - return (String) getCache().computeIf(postParam.toString(), Cache.isAbsent(), it -> { + return (String) getCache().computeIfAbsent(postParam.toString(), it -> { REQUEST_LIMIT.acquirePermit(); URL url = new URL("http://api.acoustid.org/v2/lookup?client=" + apikey + "&meta=recordings+releases+releasegroups+tracks+compress"); diff --git a/source/net/filebot/web/OpenSubtitlesClient.java b/source/net/filebot/web/OpenSubtitlesClient.java index 019ff5e7..a44b7cb0 100644 --- a/source/net/filebot/web/OpenSubtitlesClient.java +++ b/source/net/filebot/web/OpenSubtitlesClient.java @@ -95,7 +95,7 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS @Override public synchronized List guess(String tag) throws Exception { // require login - return getSearchCache("tag").computeIf(tag, Cache.isAbsent(), it -> { + return getSearchCache("tag").computeIfAbsent(tag, it -> { login(); return xmlrpc.guessMovie(singleton(tag)).getOrDefault(tag, emptyList()); }); @@ -103,7 +103,7 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS public synchronized List searchIMDB(String query) throws Exception { // require login - return getSearchCache("query").computeIf(query, Cache.isAbsent(), it -> { + return getSearchCache("query").computeIfAbsent(query, it -> { login(); return xmlrpc.searchMoviesOnIMDB(query); }); @@ -111,7 +111,7 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS public synchronized List getSubtitleList(Query query) throws Exception { // require login - return getSubtitlesCache().computeIf(query, Cache.isAbsent(), it -> { + return getSubtitlesCache().computeIfAbsent(query, it -> { login(); return xmlrpc.searchSubtitles(singleton(query)); }); @@ -166,7 +166,7 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS Query query = Query.forImdbId(searchResult.getImdbId(), season, episode, getLanguageFilter(languageName)); // require login - return getSubtitlesCache().computeIf(query, Cache.isAbsent(), it -> { + return getSubtitlesCache().computeIfAbsent(query, it -> { login(); return xmlrpc.searchSubtitles(singleton(query)); }); @@ -326,7 +326,7 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS } // require login - return getLookupCache(locale).computeIf(id.getImdbId(), Cache.isAbsent(), it -> { + return getLookupCache(locale).computeIfAbsent(id.getImdbId(), it -> { login(); return xmlrpc.getIMDBMovieDetails(id.getImdbId()); }); @@ -348,7 +348,7 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS if (f.length() > HASH_CHUNK_SIZE) { String hash = computeHash(f); - Movie match = getLookupCache(locale).computeIf(hash, Cache.isAbsent(), it -> { + Movie match = getLookupCache(locale).computeIfAbsent(hash, it -> { return xmlrpc.checkMovieHash(singleton(hash), minSeenCount).get(hash); }); @@ -381,7 +381,7 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS } // require login - List languages = getCache("detect").castList(String.class).computeIf(md5(data), Cache.isAbsent(), it -> { + List languages = getCache("detect").castList(String.class).computeIfAbsent(md5(data), it -> { login(); return xmlrpc.detectLanguage(data); }); @@ -435,7 +435,7 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS // try to get language map from cache Cache cache = Cache.getCache(getName() + "_languages", CacheType.Persistent); - Map m = (Map) cache.computeIf("subLanguageMap", Cache.isAbsent(), it -> { + Map m = (Map) cache.computeIfAbsent("subLanguageMap", it -> { try { return xmlrpc.getSubLanguages(); } catch (Exception e) { diff --git a/source/net/filebot/web/ShooterSubtitles.java b/source/net/filebot/web/ShooterSubtitles.java index d834d27e..38563198 100644 --- a/source/net/filebot/web/ShooterSubtitles.java +++ b/source/net/filebot/web/ShooterSubtitles.java @@ -90,7 +90,7 @@ public class ShooterSubtitles implements VideoHashSubtitleService { param.put("lang", LANGUAGE_CHINESE.equals(languageName) ? "Chn" : "Eng"); // use the first best option and ignore the rest - return getCache().castList(SubtitleDescriptor.class).computeIf(param.toString(), Cache.isAbsent(), it -> { + return getCache().castList(SubtitleDescriptor.class).computeIfAbsent(param.toString(), it -> { ByteBuffer post = WebRequest.post(endpoint, param, null); Object response = readJson(UTF_8.decode(post)); diff --git a/source/net/filebot/web/TheTVDBClient.java b/source/net/filebot/web/TheTVDBClient.java index ed8aab53..d114dd14 100644 --- a/source/net/filebot/web/TheTVDBClient.java +++ b/source/net/filebot/web/TheTVDBClient.java @@ -225,7 +225,7 @@ public class TheTVDBClient extends AbstractEpisodeListProvider { throw new IllegalArgumentException("Illegal TheTVDB ID: " + id); } - return getLookupCache("id", language).computeIf(id, Cache.isAbsent(), it -> { + return getLookupCache("id", language).computeIfAbsent(id, it -> { Document dom = getXmlResource(MirrorType.XML, "series/" + id + "/all/" + getLanguageCode(language) + ".xml"); String name = selectString("//SeriesName", dom); @@ -238,7 +238,7 @@ public class TheTVDBClient extends AbstractEpisodeListProvider { throw new IllegalArgumentException("Illegal IMDbID ID: " + imdbid); } - return getLookupCache("imdbid", locale).computeIf(imdbid, Cache.isAbsent(), it -> { + return getLookupCache("imdbid", locale).computeIfAbsent(imdbid, it -> { Document dom = getXmlResource(MirrorType.SEARCH, "GetSeriesByRemoteID.php?imdbid=" + imdbid + "&language=" + getLanguageCode(locale)); String id = selectString("//seriesid", dom); @@ -370,7 +370,7 @@ public class TheTVDBClient extends AbstractEpisodeListProvider { } public List getBannerList(TheTVDBSearchResult series) throws Exception { - return getBannerCache().computeIf(series.getId(), Cache.isAbsent(), it -> { + return getBannerCache().computeIfAbsent(series.getId(), it -> { Document dom = getXmlResource(MirrorType.XML, "series/" + series.getId() + "/banners.xml"); String bannerMirror = getResource(MirrorType.BANNER, "").toString();