Experiment with new CachedResource framework

This commit is contained in:
Reinhard Pointner 2016-03-09 19:51:41 +00:00
parent 4390752fc0
commit 1ab9d36938
7 changed files with 24 additions and 16 deletions

View File

@ -76,6 +76,10 @@ public class Cache {
return value; return value;
} }
public Object computeIfAbsent(Object key, Compute<?> compute) throws Exception {
return computeIf(key, isAbsent(), compute);
}
public void put(Object key, Object value) { public void put(Object key, Object value) {
try { try {
cache.put(createElement(key, value)); cache.put(createElement(key, value));
@ -155,6 +159,10 @@ public class Cache {
return (V) super.computeIf(key, condition, compute); return (V) super.computeIf(key, condition, compute);
} }
public V computeIfAbsent(Object key, Compute<?> compute) throws Exception {
return (V) super.computeIfAbsent(key, compute);
}
@Override @Override
protected Object getElementValue(Element element) { protected Object getElementValue(Element element) {
return read.apply(super.getElementValue(element)); return read.apply(super.getElementValue(element));

View File

@ -447,7 +447,7 @@ public class MediaBindingBean {
// calculate checksum from file // calculate checksum from file
Cache cache = Cache.getCache("crc32", CacheType.Ephemeral); 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") @Define("fn")

View File

@ -24,7 +24,7 @@ public abstract class AbstractEpisodeListProvider implements EpisodeListProvider
@Override @Override
public List<SearchResult> search(String query, Locale language) throws Exception { public List<SearchResult> 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); return fetchSearchResult(query, language);
}); });
} }
@ -54,7 +54,7 @@ public abstract class AbstractEpisodeListProvider implements EpisodeListProvider
SortOrder requestOrder = vetoRequestParameter(order); SortOrder requestOrder = vetoRequestParameter(order);
Locale requestLanguage = vetoRequestParameter(language); 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); return fetchSeriesData(searchResult, requestOrder, requestLanguage);
}); });
} }

View File

@ -84,7 +84,7 @@ public class AcoustIDClient implements MusicIdentificationService {
// e.g. // 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_CC8kPv4THzEGZS8GPI3x0iGVUxC1hRSizC5VzoamYDi-uR7iKPhGSI82PkiWeB_eHijvsaIWfBCWH5AjjCfVxZ1TQ3CvCTclGnEMfHbnZFA8pjD6KXwd__Cn-Y8e_I9cq6CR-4S9KLXqQcsxxoWh3eMxiHI6TIzyPv0M43YHz4yte-Cv-4D16Hv9F9C9SPUdyGtZRHV-OHEeeGD--BKcjVLOK_NCDXMfx44dzHEiOZ0Z44Rf6DH5R3uiPj4d_PKolJNyRJzyu4_CTD2WOvzjKH9GPb4cUP1Av9EuQd8fGCFee4JlRHi18xQh96NLxkCgfWFKOH6WGeoe4I3za4c5hTscTPEZTES1x8kE-9MQPjT8a8gh5fPgQZtqCFj9MDvp6fDx6NCd07bjx7MLR9AhtnFnQ70GjOcV0opmm4zpY3SOa7HiwdTtyHa6NC4e-HN-OfC5-OP_gLe2QDxfUCz_0w9l65HiPAz9-IaGOUA7-4MZ5CWFOlIfe4yUa6AiZGxf6w0fFxsjTOdC6Itbh4mGD63iPH9-RFy909XAMj7mC5_BvlDyO6kGTZKJxHUd4NDwuZUffw_5RMsde5CWkJAgXnDReNEaP6DTOQ65yaD88HoeX8fge-DSeHo9Qa8cTHc80I-_RoHxx_UHeBxrJw62Q34Kd7MEfpCcu6BLeB1ePw6OO4sOF_sHhmB504WWDZiEu8sKPpkcfCT9xfej0o0lr4T5yNJeOvjmu40w-TDmqHXmYgfFhFy_M7tD1o0cO_B2ms2j-ACEEQgQgAIwzTgAGmBIKIImNQAABwgQATAlhDGCCEIGIIM4BaBgwQBogEBIOESEIA8ARI5xAhxEFmAGAMCKAURKQQpQzRAAkCCBQEAKkQYIYIQQxCixCDADCABMAE0gpJIgyxhEDiCKCCIGAEIgJIQByAhFgGACCACMRQEyBAoxQiHiCBCFOECQFAIgAABR2QAgFjCDMA0AUMIoAIMChQghChASGEGeYEAIAIhgBSErnJPPEGWYAMgw05AhiiGHiBBBGGSCQcQgwRYJwhDDhgCSCSSEIQYwILoyAjAIigBFEUQK8gAYAQ5BCAAjkjCCAEEMZAUQAZQCjCCkpCgFMCCiIcVIAZZgilAQAiSHQECOcQAQIc4QClAHAjDDGkAGAMUoBgyhihgEChFCAAWEIEYwIJYwViAAlHCBIGEIEAEIQAoBwwgwiEBAEEEOoEwBY4wRwxAhBgAcKAESIQAwwIowRFhoBhAE // 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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pQzRAAkCCBQEAKkQYIYIQQxCixCDADCABMAE0gpJIgyxhEDiCKCCIGAEIgJIQByAhFgGACCACMRQEyBAoxQiHiCBCFOECQFAIgAABR2QAgFjCDMA0AUMIoAIMChQghChASGEGeYEAIAIhgBSErnJPPEGWYAMgw05AhiiGHiBBBGGSCQcQgwRYJwhDDhgCSCSSEIQYwILoyAjAIigBFEUQK8gAYAQ5BCAAjkjCCAEEMZAUQAZQCjCCkpCgFMCCiIcVIAZZgilAQAiSHQECOcQAQIc4QClAHAjDDGkAGAMUoBgyhihgEChFCAAWEIEYwIJYwViAAlHCBIGEIEAEIQAoBwwgwiEBAEEEOoEwBY4wRwxAhBgAcKAESIQAwwIowRFhoBhAE
return (String) getCache().computeIf(postParam.toString(), Cache.isAbsent(), it -> { return (String) getCache().computeIfAbsent(postParam.toString(), it -> {
REQUEST_LIMIT.acquirePermit(); REQUEST_LIMIT.acquirePermit();
URL url = new URL("http://api.acoustid.org/v2/lookup?client=" + apikey + "&meta=recordings+releases+releasegroups+tracks+compress"); URL url = new URL("http://api.acoustid.org/v2/lookup?client=" + apikey + "&meta=recordings+releases+releasegroups+tracks+compress");

View File

@ -95,7 +95,7 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS
@Override @Override
public synchronized List<SubtitleSearchResult> guess(String tag) throws Exception { public synchronized List<SubtitleSearchResult> guess(String tag) throws Exception {
// require login // require login
return getSearchCache("tag").computeIf(tag, Cache.isAbsent(), it -> { return getSearchCache("tag").computeIfAbsent(tag, it -> {
login(); login();
return xmlrpc.guessMovie(singleton(tag)).getOrDefault(tag, emptyList()); return xmlrpc.guessMovie(singleton(tag)).getOrDefault(tag, emptyList());
}); });
@ -103,7 +103,7 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS
public synchronized List<SubtitleSearchResult> searchIMDB(String query) throws Exception { public synchronized List<SubtitleSearchResult> searchIMDB(String query) throws Exception {
// require login // require login
return getSearchCache("query").computeIf(query, Cache.isAbsent(), it -> { return getSearchCache("query").computeIfAbsent(query, it -> {
login(); login();
return xmlrpc.searchMoviesOnIMDB(query); return xmlrpc.searchMoviesOnIMDB(query);
}); });
@ -111,7 +111,7 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS
public synchronized List<SubtitleDescriptor> getSubtitleList(Query query) throws Exception { public synchronized List<SubtitleDescriptor> getSubtitleList(Query query) throws Exception {
// require login // require login
return getSubtitlesCache().computeIf(query, Cache.isAbsent(), it -> { return getSubtitlesCache().computeIfAbsent(query, it -> {
login(); login();
return xmlrpc.searchSubtitles(singleton(query)); 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)); Query query = Query.forImdbId(searchResult.getImdbId(), season, episode, getLanguageFilter(languageName));
// require login // require login
return getSubtitlesCache().computeIf(query, Cache.isAbsent(), it -> { return getSubtitlesCache().computeIfAbsent(query, it -> {
login(); login();
return xmlrpc.searchSubtitles(singleton(query)); return xmlrpc.searchSubtitles(singleton(query));
}); });
@ -326,7 +326,7 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS
} }
// require login // require login
return getLookupCache(locale).computeIf(id.getImdbId(), Cache.isAbsent(), it -> { return getLookupCache(locale).computeIfAbsent(id.getImdbId(), it -> {
login(); login();
return xmlrpc.getIMDBMovieDetails(id.getImdbId()); return xmlrpc.getIMDBMovieDetails(id.getImdbId());
}); });
@ -348,7 +348,7 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS
if (f.length() > HASH_CHUNK_SIZE) { if (f.length() > HASH_CHUNK_SIZE) {
String hash = computeHash(f); 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); return xmlrpc.checkMovieHash(singleton(hash), minSeenCount).get(hash);
}); });
@ -381,7 +381,7 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS
} }
// require login // require login
List<String> languages = getCache("detect").castList(String.class).computeIf(md5(data), Cache.isAbsent(), it -> { List<String> languages = getCache("detect").castList(String.class).computeIfAbsent(md5(data), it -> {
login(); login();
return xmlrpc.detectLanguage(data); return xmlrpc.detectLanguage(data);
}); });
@ -435,7 +435,7 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS
// try to get language map from cache // try to get language map from cache
Cache cache = Cache.getCache(getName() + "_languages", CacheType.Persistent); 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 { try {
return xmlrpc.getSubLanguages(); return xmlrpc.getSubLanguages();
} catch (Exception e) { } catch (Exception e) {

View File

@ -90,7 +90,7 @@ public class ShooterSubtitles implements VideoHashSubtitleService {
param.put("lang", LANGUAGE_CHINESE.equals(languageName) ? "Chn" : "Eng"); param.put("lang", LANGUAGE_CHINESE.equals(languageName) ? "Chn" : "Eng");
// use the first best option and ignore the rest // 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); ByteBuffer post = WebRequest.post(endpoint, param, null);
Object response = readJson(UTF_8.decode(post)); Object response = readJson(UTF_8.decode(post));

View File

@ -225,7 +225,7 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
throw new IllegalArgumentException("Illegal TheTVDB ID: " + id); 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"); Document dom = getXmlResource(MirrorType.XML, "series/" + id + "/all/" + getLanguageCode(language) + ".xml");
String name = selectString("//SeriesName", dom); String name = selectString("//SeriesName", dom);
@ -238,7 +238,7 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
throw new IllegalArgumentException("Illegal IMDbID ID: " + imdbid); 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)); Document dom = getXmlResource(MirrorType.SEARCH, "GetSeriesByRemoteID.php?imdbid=" + imdbid + "&language=" + getLanguageCode(locale));
String id = selectString("//seriesid", dom); String id = selectString("//seriesid", dom);
@ -370,7 +370,7 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
} }
public List<BannerDescriptor> getBannerList(TheTVDBSearchResult series) throws Exception { public List<BannerDescriptor> 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"); Document dom = getXmlResource(MirrorType.XML, "series/" + series.getId() + "/banners.xml");
String bannerMirror = getResource(MirrorType.BANNER, "").toString(); String bannerMirror = getResource(MirrorType.BANNER, "").toString();