1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-11-04 08:25:03 -05:00

* cache mirrors and banner lists

This commit is contained in:
Reinhard Pointner 2011-12-20 14:09:18 +00:00
parent b765b7d1f0
commit 4be0846a53
2 changed files with 74 additions and 23 deletions

View File

@ -22,24 +22,24 @@ public abstract class AbstractEpisodeListProvider implements EpisodeListProvider
return true;
}
@Override
public boolean hasLocaleSupport() {
return false;
}
protected abstract List<SearchResult> fetchSearchResult(String query, Locale locale) throws Exception;
protected abstract List<Episode> fetchEpisodeList(SearchResult searchResult, Locale locale) throws Exception;
public List<SearchResult> search(String query) throws Exception {
return search(query, getDefaultLocale());
}
public List<SearchResult> search(String query, Locale locale) throws Exception {
ResultCache cache = getCache();
List<SearchResult> results = (cache != null) ? cache.getSearchResult(query, locale) : null;
@ -54,12 +54,12 @@ public abstract class AbstractEpisodeListProvider implements EpisodeListProvider
return (cache != null) ? cache.putSearchResult(query, locale, results) : results;
}
public List<Episode> getEpisodeList(SearchResult searchResult) throws Exception {
return getEpisodeList(searchResult, getDefaultLocale());
}
public List<Episode> getEpisodeList(SearchResult searchResult, Locale locale) throws Exception {
ResultCache cache = getCache();
List<Episode> episodes = (cache != null) ? cache.getEpisodeList(searchResult, locale) : null;
@ -74,12 +74,12 @@ public abstract class AbstractEpisodeListProvider implements EpisodeListProvider
return (cache != null) ? cache.putEpisodeList(searchResult, locale, episodes) : episodes;
}
public List<Episode> getEpisodeList(SearchResult searchResult, int season) throws Exception {
return getEpisodeList(searchResult, season, getDefaultLocale());
}
@Override
public List<Episode> getEpisodeList(SearchResult searchResult, int season, Locale locale) throws Exception {
List<Episode> all = getEpisodeList(searchResult, locale);
@ -92,34 +92,34 @@ public abstract class AbstractEpisodeListProvider implements EpisodeListProvider
return eps;
}
public Locale getDefaultLocale() {
return Locale.ENGLISH;
}
public ResultCache getCache() {
return null;
}
protected static class ResultCache {
private final String id;
private final Cache cache;
public ResultCache(String id, Cache cache) {
this.id = id;
this.cache = cache;
}
protected String normalize(String query) {
return query == null ? null : query.trim().toLowerCase();
}
public <T extends SearchResult> List<T> putSearchResult(String query, Locale locale, List<T> value) {
try {
cache.put(new Element(new Key(id, normalize(query), locale), value.toArray(new SearchResult[0])));
@ -130,7 +130,7 @@ public abstract class AbstractEpisodeListProvider implements EpisodeListProvider
return value;
}
public List<SearchResult> getSearchResult(String query, Locale locale) {
try {
Element element = cache.get(new Key(id, normalize(query), locale));
@ -144,7 +144,7 @@ public abstract class AbstractEpisodeListProvider implements EpisodeListProvider
return null;
}
public List<Episode> putEpisodeList(SearchResult key, Locale locale, List<Episode> episodes) {
try {
cache.put(new Element(new Key(id, key, locale), episodes.toArray(new Episode[0])));
@ -155,7 +155,7 @@ public abstract class AbstractEpisodeListProvider implements EpisodeListProvider
return episodes;
}
public List<Episode> getEpisodeList(SearchResult key, Locale locale) {
try {
Element element = cache.get(new Key(id, key, locale));
@ -169,23 +169,46 @@ public abstract class AbstractEpisodeListProvider implements EpisodeListProvider
return null;
}
public void putData(Object category, Object key, Object object) {
try {
cache.put(new Element(new Key(id, category, key), object));
} catch (Exception e) {
Logger.getLogger(AbstractEpisodeListProvider.class.getName()).log(Level.WARNING, e.getMessage(), e);
}
}
public <T> T getData(Object category, Object key, Class<T> type) {
try {
Element element = cache.get(new Key(id, category, key));
if (element != null) {
return type.cast(element.getValue());
}
} catch (Exception e) {
Logger.getLogger(AbstractEpisodeListProvider.class.getName()).log(Level.WARNING, e.getMessage(), e);
}
return null;
}
private 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) {

View File

@ -7,10 +7,12 @@ import static net.sourceforge.filebot.web.WebRequest.*;
import static net.sourceforge.tuned.XPathUtilities.*;
import java.io.FileNotFoundException;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.LinkedHashMap;
@ -246,6 +248,18 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
protected String getMirror(MirrorType mirrorType) throws Exception {
synchronized (mirrors) {
if (mirrors.isEmpty()) {
// try cache first
try {
@SuppressWarnings("unchecked")
Map<MirrorType, String> cachedMirrors = (Map<MirrorType, String>) getCache().getData("mirrors", null, Map.class);
if (cachedMirrors != null) {
mirrors.putAll(cachedMirrors);
return mirrors.get(mirrorType);
}
} catch (Exception e) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, e.getMessage(), e);
}
// initialize mirrors
Document dom = getDocument(getResource(null, "/api/" + apikey + "/mirrors.xml"));
@ -279,6 +293,8 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
mirrors.put(type, list.get(random.nextInt(list.size())));
}
}
getCache().putData("mirrors", null, mirrors);
}
return mirrors.get(mirrorType);
@ -384,6 +400,12 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
public List<BannerDescriptor> getBannerList(int seriesid) throws Exception {
// check cache first
BannerDescriptor[] cachedList = getCache().getData("banners", seriesid, BannerDescriptor[].class);
if (cachedList != null) {
return Arrays.asList(cachedList);
}
Document dom = getDocument(getResource(MirrorType.XML, "/api/" + apikey + "/series/" + seriesid + "/banners.xml"));
List<Node> nodes = selectNodes("//Banner", dom);
@ -411,11 +433,12 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
}
}
getCache().putData("banners", seriesid, banners.toArray(new BannerDescriptor[0]));
return banners;
}
public static class BannerDescriptor {
public static class BannerDescriptor implements Serializable {
public static enum BannerProperty {
id,
@ -437,6 +460,11 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
private EnumMap<BannerProperty, String> fields;
protected BannerDescriptor() {
// used by serializer
}
protected BannerDescriptor(Map<BannerProperty, String> fields) {
this.fields = new EnumMap<BannerProperty, String>(fields);
}