--- android/TrainInfoServiceGoogle/src/dk/thoerup/traininfoservice/StationDAO.java 2010/09/22 23:09:26 1109 +++ android/TrainInfoServiceGoogle/src/dk/thoerup/traininfoservice/StationDAO.java 2010/09/23 06:10:35 1110 @@ -4,13 +4,22 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; -import java.util.Iterator; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.logging.Level; import java.util.logging.Logger; +import javax.jdo.Extent; import javax.jdo.PersistenceManager; import javax.jdo.Query; +import net.sf.jsr107cache.Cache; +import net.sf.jsr107cache.CacheException; +import net.sf.jsr107cache.CacheManager; + +import com.google.appengine.api.memcache.jsr107cache.GCacheFactory; + import dk.thoerup.android.traininfo.common.StationBean; import dk.thoerup.android.traininfo.common.StationBean.StationEntry; import dk.thoerup.traininfoservice.geo.Geo; @@ -20,6 +29,22 @@ public class StationDAO { final static int LOCATION_LIMIT = 8; static final Logger logger = Logger.getLogger(StationDAO.class.getName()); + + final int TIMEOUT_SECONDS = 10*60; + + Cache cache; + + public StationDAO() { + Map props = new HashMap(); + props.put(GCacheFactory.EXPIRATION_DELTA, TIMEOUT_SECONDS); + + try { + cache = CacheManager.getInstance().getCacheFactory().createCache(props); + } catch (CacheException e) { + logger.log(Level.WARNING, "error creating cache", e); + } + + } public StationEntry getById(int id) { @@ -40,13 +65,16 @@ PersistenceManager pm = null; try { - pm = PMF.get().getPersistenceManager(); + /*pm = PMF.get().getPersistenceManager(); Query q = pm.newQuery(JdoStationBean.class); q.setOrdering("name"); - List beanList = (List) q.execute(); + List beanList = (List) q.execute();*/ + + List beanList = getAllStations(); + StationBean stationBean = new StationBean(); @@ -65,12 +93,48 @@ return stationBean; } finally { - pm.close(); + // pm.close(); } } + public List getAllStations() { + final String key = "allstations"; + List result = (List) cache.get(key); + + if (result == null) { + logger.info("getAllStations Cache miss"); + + PersistenceManager pm = null; + final double LAT = 0.4; + final double LNG = 0.75; + + try { + pm = PMF.get().getPersistenceManager(); + Extent all = pm.getExtent(JdoStationBean.class, false); + + result = new ArrayList(); + for (JdoStationBean station : all) { + result.add(station); + } + + cache.put(key, result); + + } finally { + pm.close(); + } + } else { + logger.info("getAllStations Cache hit"); + } + + + + return result; + + } + //String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : ""; + /* public List getByLocationList(double latitude, double longitude, boolean geolimit) { PersistenceManager pm = null; @@ -102,17 +166,19 @@ } finally { pm.close(); } - } + }*/ public StationBean getByLocation(double latitude, double longitude) { - + /* List beanList = getByLocationList(latitude,longitude,true); if (beanList.size() < LOCATION_LIMIT ) { logger.info("getByLocation failover: " +latitude + "," + longitude); beanList = getByLocationList(latitude,longitude, false); - } + }*/ + + List beanList = getAllStations(); StationBean stationBean = new StationBean();