--- android/TrainInfoServiceGoogle/src/dk/thoerup/traininfoservice/banedk/DepartureFetcher.java 2010/09/21 16:45:24 1092 +++ android/TrainInfoServiceGoogle/src/dk/thoerup/traininfoservice/banedk/DepartureFetcher.java 2010/09/21 20:10:46 1093 @@ -4,13 +4,21 @@ import java.net.URL; import java.net.URLEncoder; import java.util.Collections; +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.DepartureBean; import dk.thoerup.android.traininfo.common.DepartureEntry; import dk.thoerup.android.traininfo.common.StationBean.StationEntry; @@ -25,38 +33,48 @@ STOG, REGIONAL } + Cache cache; - Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); - - Map cache; + Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); StationDAO stationDao = new StationDAO(); private boolean useAzureSite; private int replyTimeout; + @SuppressWarnings("unchecked") public DepartureFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) { this.replyTimeout = replyTimeout; useAzureSite = azureSite; - cache = new TimeoutMap(cacheTimeout); + + Map props = new HashMap(); + props.put(GCacheFactory.EXPIRATION_DELTA_MILLIS, cacheTimeout); + + try { + cache = CacheManager.getInstance().getCacheFactory().createCache(props); + } catch (CacheException e) { + logger.log(Level.WARNING, "error creating cache", e); + } + } public DepartureBean cachedLookupDepartures(int stationID, boolean arrival) throws Exception { - final String key = "" + stationID + ":" + arrival; + final String key = "departure:" + stationID + ":" + arrival; - DepartureBean departureBean = cache.get(key); - + DepartureBean departureBean = (DepartureBean) cache.get(key); if (departureBean == null) { departureBean = lookupDepartures(stationID,arrival); cache.put(key, departureBean); + logger.info("Departure: Cache miss " + key + " !!! "); //remove before production } else { Statistics.getInstance().incrementDepartureCacheHits(); - logger.info("Departure: Cache hit " + key); //remove before production + logger.info("Departure: Cache hit " + key); } + return departureBean; } @@ -65,10 +83,12 @@ DepartureBean departureBean = new DepartureBean(); + //TODO: debug code StationEntry station = new StationEntry(); // stationDao.getById(stationID); station.setId(82); station.setName("Test Station"); station.setRegional("HS"); + //TODO: end of debug code departureBean.stationName = station.getName();