This commit is contained in:
Reinhard Pointner 2016-03-07 23:56:32 +00:00
parent a4d363bb97
commit be5e65a844
5 changed files with 22 additions and 27 deletions

View File

@ -21,7 +21,7 @@ public class Cache {
public static final Duration ONE_WEEK = Duration.ofDays(7); public static final Duration ONE_WEEK = Duration.ofDays(7);
public static Cache getCache(String name, CacheType type) { public static Cache getCache(String name, CacheType type) {
return CacheManager.getInstance().getCache(name.toLowerCase(), type); return CacheManager.getInstance().getCache(name, type);
} }
public CachedResource2<String, String> text(String key, Transform<String, URL> resource) { public CachedResource2<String, String> text(String key, Transform<String, URL> resource) {

View File

@ -35,7 +35,7 @@ public class CacheManager {
} }
public Cache getCache(String name, CacheType type) { public Cache getCache(String name, CacheType type) {
String cacheName = name + "_" + type.ordinal(); String cacheName = name.toLowerCase() + "_" + type.ordinal();
if (!manager.cacheExists(cacheName)) { if (!manager.cacheExists(cacheName)) {
debug.config("Create cache: " + cacheName); debug.config("Create cache: " + cacheName);
manager.addCache(new net.sf.ehcache.Cache(type.getConfiguration(cacheName))); manager.addCache(new net.sf.ehcache.Cache(type.getConfiguration(cacheName)));

View File

@ -123,8 +123,8 @@ public class CachedResource2<K, R> {
} }
@FunctionalInterface @FunctionalInterface
public interface Permit<P> { public interface Permit {
boolean acquirePermit(URL resource) throws Exception; void acquire(URL resource) throws Exception;
} }
public static Transform<ByteBuffer, String> getText(Charset charset) { public static Transform<ByteBuffer, String> getText(Charset charset) {
@ -194,12 +194,10 @@ public class CachedResource2<K, R> {
}; };
} }
public static Fetch withPermit(Fetch fetch, Permit<?> permit) { public static Fetch withPermit(Fetch fetch, Permit permit) {
return (url, lastModified) -> { return (url, lastModified) -> {
if (permit.acquirePermit(url)) { permit.acquire(url);
return fetch.fetch(url, lastModified); return fetch.fetch(url, lastModified);
}
return null;
}; };
} }

View File

@ -80,7 +80,7 @@ public class OMDbClient implements MovieIdentificationService {
param.put("y", movieYear); param.put("y", movieYear);
} }
Map<?, ?> response = request(param); Object response = request(param);
List<Movie> result = new ArrayList<Movie>(); List<Movie> result = new ArrayList<Movie>();
for (Object it : getArray(response, "Search")) { for (Object it : getArray(response, "Search")) {
@ -134,13 +134,11 @@ public class OMDbClient implements MovieIdentificationService {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
public Map<?, ?> request(Map<String, Object> parameters) throws Exception { public Object request(Map<String, Object> parameters) throws Exception {
Cache cache = Cache.getCache(getName(), CacheType.Weekly);
String key = '?' + encodeParameters(parameters, true); String key = '?' + encodeParameters(parameters, true);
Cache cache = Cache.getCache(getName(), CacheType.Weekly); return cache.json(key, s -> getResource(s)).fetch(withPermit(fetchIfModified(), r -> REQUEST_LIMIT.acquirePermit())).expire(Cache.ONE_WEEK).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);
} }
public URL getResource(String file) throws Exception { public URL getResource(String file) throws Exception {

View File

@ -87,7 +87,7 @@ public class TMDbClient implements MovieIdentificationService {
param.put("year", movieYear); param.put("year", movieYear);
} }
Map<?, ?> response = request("search/movie", param, locale, SEARCH_LIMIT); Object response = request("search/movie", param, locale, SEARCH_LIMIT);
// e.g. {"id":16320,"title":"冲出宁静号","release_date":"2005-09-30","original_title":"Serenity"} // e.g. {"id":16320,"title":"冲出宁静号","release_date":"2005-09-30","original_title":"Serenity"}
return streamJsonObjects(response, "results").map(it -> { return streamJsonObjects(response, "results").map(it -> {
@ -110,7 +110,7 @@ public class TMDbClient implements MovieIdentificationService {
if (extendedInfo) { if (extendedInfo) {
try { try {
Map<?, ?> titles = request("movie/" + id + "/alternative_titles", null, null, REQUEST_LIMIT); Object titles = request("movie/" + id + "/alternative_titles", null, null, REQUEST_LIMIT);
streamJsonObjects(titles, "titles").map(n -> { streamJsonObjects(titles, "titles").map(n -> {
return getString(n, "title"); return getString(n, "title");
}).filter(t -> t != null && t.length() >= 3).forEach(alternativeTitles::add); }).filter(t -> t != null && t.length() >= 3).forEach(alternativeTitles::add);
@ -169,7 +169,7 @@ public class TMDbClient implements MovieIdentificationService {
} }
public MovieInfo getMovieInfo(String id, Locale locale, boolean extendedInfo) throws Exception { public MovieInfo getMovieInfo(String id, Locale locale, boolean extendedInfo) throws Exception {
Map<?, ?> response = request("movie/" + id, extendedInfo ? singletonMap("append_to_response", "alternative_titles,releases,casts,trailers") : null, locale, REQUEST_LIMIT); Object response = request("movie/" + id, extendedInfo ? singletonMap("append_to_response", "alternative_titles,releases,casts,trailers") : null, locale, REQUEST_LIMIT);
Map<MovieProperty, String> fields = mapStringValues(response, MovieProperty.class); Map<MovieProperty, String> fields = mapStringValues(response, MovieProperty.class);
@ -270,10 +270,10 @@ public class TMDbClient implements MovieIdentificationService {
public List<Artwork> getArtwork(String id) throws Exception { public List<Artwork> getArtwork(String id) throws Exception {
// http://api.themoviedb.org/3/movie/11/images // http://api.themoviedb.org/3/movie/11/images
Map<?, ?> config = request("configuration", null, null, REQUEST_LIMIT); Object config = request("configuration", null, null, REQUEST_LIMIT);
String baseUrl = getString(getMap(config, "images"), "base_url"); String baseUrl = getString(getMap(config, "images"), "base_url");
Map<?, ?> images = request("movie/" + id + "/images", null, null, REQUEST_LIMIT); Object images = request("movie/" + id + "/images", null, null, REQUEST_LIMIT);
return Stream.of("backdrops", "posters").flatMap(section -> { return Stream.of("backdrops", "posters").flatMap(section -> {
Stream<Artwork> artwork = streamJsonObjects(images, section).map(it -> { Stream<Artwork> artwork = streamJsonObjects(images, section).map(it -> {
@ -292,7 +292,7 @@ public class TMDbClient implements MovieIdentificationService {
}).collect(toList()); }).collect(toList());
} }
public Map<?, ?> request(String resource, Map<String, Object> parameters, Locale locale, final FloodLimit limit) throws Exception { public Object request(String resource, Map<String, Object> parameters, Locale locale, final FloodLimit limit) throws Exception {
// default parameters // default parameters
LinkedHashMap<String, Object> data = new LinkedHashMap<String, Object>(); LinkedHashMap<String, Object> data = new LinkedHashMap<String, Object>();
if (parameters != null) { if (parameters != null) {
@ -313,17 +313,16 @@ public class TMDbClient implements MovieIdentificationService {
} }
data.put("api_key", apikey); data.put("api_key", apikey);
Cache cache = Cache.getCache(getName(), CacheType.Monthly);
Cache etagStorage = Cache.getCache("etag", CacheType.Monthly);
String key = resource + '?' + encodeParameters(data, true); String key = resource + '?' + encodeParameters(data, true);
Cache etagStorage = Cache.getCache("etag", CacheType.Monthly); Object json = cache.json(key, s -> getResource(s)).fetch(withPermit(fetchIfNoneMatch(etagStorage), r -> limit.acquirePermit())).expire(Cache.ONE_WEEK).get();
Cache cache = Cache.getCache(getName(), CacheType.Monthly);
Object json = cache.json(key, s -> getResource(s)).fetch(withPermit(fetchIfNoneMatch(etagStorage), r -> limit.acquirePermit() != null)).expire(Cache.ONE_WEEK).get();
Map<?, ?> root = asMap(json); if (asMap(json).isEmpty()) {
if (root.isEmpty()) {
throw new FileNotFoundException(String.format("Resource is empty: %s => %s", json, getResource(key))); throw new FileNotFoundException(String.format("Resource is empty: %s => %s", json, getResource(key)));
} }
return root; return json;
} }
public URL getResource(String file) throws Exception { public URL getResource(String file) throws Exception {