--- android/TrainInfoServiceGoogle/src/dk/thoerup/traininfoservice/banedk/MetroFetcher.java 2010/09/21 16:45:24 1092 +++ android/TrainInfoServiceGoogle/src/dk/thoerup/traininfoservice/banedk/MetroFetcher.java 2010/09/21 20:10:46 1093 @@ -2,20 +2,28 @@ import java.net.URL; +import java.util.HashMap; import java.util.Map; +import java.util.logging.Level; import java.util.logging.Logger; +import net.sf.jsr107cache.Cache; +import net.sf.jsr107cache.CacheException; +import net.sf.jsr107cache.CacheManager; + import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; +import com.google.appengine.api.memcache.jsr107cache.GCacheFactory; + import dk.thoerup.android.traininfo.common.MetroBean; import dk.thoerup.android.traininfo.common.MetroBean.MetroEntry; -import dk.thoerup.android.traininfo.common.StationBean.StationEntry; import dk.thoerup.circuitbreaker.CircuitBreaker; import dk.thoerup.circuitbreaker.CircuitBreakerManager; import dk.thoerup.traininfoservice.StationDAO; import dk.thoerup.traininfoservice.Statistics; +import dk.thoerup.traininfoservice.jdo.JdoStationBean; @@ -24,12 +32,24 @@ Logger logger = Logger.getLogger(MetroFetcher.class.getName()); StationDAO stationDAO = new StationDAO(); - Map cache = new TimeoutMap(60000); //TODO: make metro cache timeout configurable + //Map cache = new TimeoutMap(60000); //TODO: make metro cache timeout configurable + Cache cache; + + public MetroFetcher() { + Map props = new HashMap(); + props.put(GCacheFactory.EXPIRATION_DELTA_MILLIS, 60000); + + try { + cache = CacheManager.getInstance().getCacheFactory().createCache(props); + } catch (CacheException e) { + logger.log(Level.WARNING, "error creating cache", e); + } + } public MetroBean cachedLookupMetroDepartures(int stationID) throws Exception { final String key = "metro:" + stationID; - MetroBean metroBean = cache.get(key); + MetroBean metroBean = (MetroBean) cache.get(key); if (metroBean == null) { metroBean = lookupMetroDepartures(stationID); @@ -43,7 +63,7 @@ public MetroBean lookupMetroDepartures(int stationID) throws Exception { - StationEntry station = stationDAO.getById(stationID) ; + JdoStationBean station = stationDAO.getById(stationID) ; URL url = new URL("http://www.m.dk/layouts/Metro/Widgets/MetroWidget.ashx?StationId=" + station.getMetro() + "&Congestion=true&Application=MyMetro&Expiration=true");