Permalink
Browse files

Add tile cache optimizations.

  • Loading branch information...
2 parents b231d28 + e5ccfca commit 881aa4815ed0bb4e97de65596d4a36642ac704bd @eightysteele eightysteele committed Apr 6, 2012
Showing with 33 additions and 85 deletions.
  1. +7 −38 app/js/mol.map.results.js
  2. +1 −1 app/js/mol.map.tiles.js
  3. +8 −39 app/static/js/mol.js
  4. +0 −1 app/templates/map-index-template.html
  5. +17 −6 app/tile_handler.py
@@ -566,29 +566,29 @@ mol.modules.map.results = function(mol) {
this.exists(type, nameProfile.types)) {
return _.intersect(
nameProfile.layers,
- this.getLayers(name, source, type, 'sourceProfile'));
+ this.getLayers(name, source, type, englishname, 'sourceProfile'));
}
}
if (source && !type) {
if (this.exists(source, nameProfile.sources)) {
return _.intersect(
nameProfile.layers,
- this.getLayers(name, source, type, 'sourceProfile'));
+ this.getLayers(name, source, type, englishname, 'sourceProfile'));
}
}
if (!source && type) {
if (this.exists(type, nameProfile.types)) {
return _.intersect(
nameProfile.layers,
- this.getLayers(name, source, type, 'typeProfile'));
+ this.getLayers(name, source, type, englishname, 'typeProfile'));
}
}
}
return [];
case 'sourceProfile':
if (!source) {
- return this.getLayers(name, source, type, 'typeProfile');
+ return this.getLayers(name, source, type, englishname,'typeProfile');
}
if (sourceProfile) {
@@ -600,7 +600,7 @@ mol.modules.map.results = function(mol) {
this.exists(type, sourceProfile.types)) {
return _.intersect(
sourceProfile.layers,
- this.getLayers(name, source, type, 'typeProfile'));
+ this.getLayers(name, source, type, englishname,'typeProfile'));
}
}
if (name && !type) {
@@ -612,43 +612,12 @@ mol.modules.map.results = function(mol) {
if (this.exists(type, sourceProfile.types)) {
return _.intersect(
sourceProfile.layers,
- this.getLayers(name, source, type, 'typeProfile'));
- }
- }
- }
- return [];
- /* TODO FIX THIS case */
- case 'englishnameProfile':
- if (!englishname) {
- return this.getLayers(name, source, type, englishname, 'typeProfile');
- }
-
- if (englishnameProfile) {
- if (!name && !type) {
- return sourceProfile.layers;
- }
- if (name && type) {
- if (this.exists(name, sourceProfile.names) &&
- this.exists(type, sourceProfile.types)) {
- return _.intersect(
- sourceProfile.layers,
- this.getLayers(name, source, type, 'typeProfile'));
- }
- }
- if (name && !type) {
- if (this.exists(name, sourceProfile.names)) {
- return sourceProfile.layers;
- }
- }
- if (!name && type) {
- if (this.exists(type, sourceProfile.types)) {
- return _.intersect(
- sourceProfile.layers,
- this.getLayers(name, source, type, 'typeProfile'));
+ this.getLayers(name, source, type, englishname, 'typeProfile'));
}
}
}
return [];
+ /*TODO englishname profile */
case 'typeProfile':
if (!type) {
@@ -314,7 +314,7 @@ mol.modules.map.tiles = function(mol) {
sql = "SELECT cartodb_id, st_transform(the_geom, 3785) AS the_geom_webmercator, identifier " +
"FROM {0} WHERE lower(scientificname)='{1}'".format("gbif_import", layer.name.toLowerCase());
table = 'gbif_import';
- style_table_name = 'styles';
+ style_table_name = 'names_old';
info_query = "SELECT cartodb_id, st_transform(the_geom, 3785) AS the_geom_webmercator, 'GBIF' || '' AS source, 'point' || '' AS type, 'http://data.gbif.org/ws/rest/occurrence/get/' || identifier as URL, scientificname AS name FROM {0} WHERE lower(scientificname)='{1}'".format("gbif_import", layer.name.toLowerCase());
} else {
sql = sql.format(table, layer.name, layer.type);
View
@@ -2269,29 +2269,29 @@ mol.modules.map.results = function(mol) {
this.exists(type, nameProfile.types)) {
return _.intersect(
nameProfile.layers,
- this.getLayers(name, source, type, 'sourceProfile'));
+ this.getLayers(name, source, type, englishname, 'sourceProfile'));
}
}
if (source && !type) {
if (this.exists(source, nameProfile.sources)) {
return _.intersect(
nameProfile.layers,
- this.getLayers(name, source, type, 'sourceProfile'));
+ this.getLayers(name, source, type, englishname, 'sourceProfile'));
}
}
if (!source && type) {
if (this.exists(type, nameProfile.types)) {
return _.intersect(
nameProfile.layers,
- this.getLayers(name, source, type, 'typeProfile'));
+ this.getLayers(name, source, type, englishname, 'typeProfile'));
}
}
}
return [];
case 'sourceProfile':
if (!source) {
- return this.getLayers(name, source, type, 'typeProfile');
+ return this.getLayers(name, source, type, englishname,'typeProfile');
}
if (sourceProfile) {
@@ -2303,7 +2303,7 @@ mol.modules.map.results = function(mol) {
this.exists(type, sourceProfile.types)) {
return _.intersect(
sourceProfile.layers,
- this.getLayers(name, source, type, 'typeProfile'));
+ this.getLayers(name, source, type, englishname,'typeProfile'));
}
}
if (name && !type) {
@@ -2315,43 +2315,12 @@ mol.modules.map.results = function(mol) {
if (this.exists(type, sourceProfile.types)) {
return _.intersect(
sourceProfile.layers,
- this.getLayers(name, source, type, 'typeProfile'));
- }
- }
- }
- return [];
- /* TODO FIX THIS case */
- case 'englishnameProfile':
- if (!englishname) {
- return this.getLayers(name, source, type, englishname, 'typeProfile');
- }
-
- if (englishnameProfile) {
- if (!name && !type) {
- return sourceProfile.layers;
- }
- if (name && type) {
- if (this.exists(name, sourceProfile.names) &&
- this.exists(type, sourceProfile.types)) {
- return _.intersect(
- sourceProfile.layers,
- this.getLayers(name, source, type, 'typeProfile'));
- }
- }
- if (name && !type) {
- if (this.exists(name, sourceProfile.names)) {
- return sourceProfile.layers;
- }
- }
- if (!name && type) {
- if (this.exists(type, sourceProfile.types)) {
- return _.intersect(
- sourceProfile.layers,
- this.getLayers(name, source, type, 'typeProfile'));
+ this.getLayers(name, source, type, englishname, 'typeProfile'));
}
}
}
return [];
+ /*TODO englishname profile */
case 'typeProfile':
if (!type) {
@@ -3020,7 +2989,7 @@ mol.modules.map.tiles = function(mol) {
sql = "SELECT cartodb_id, st_transform(the_geom, 3785) AS the_geom_webmercator, identifier " +
"FROM {0} WHERE lower(scientificname)='{1}'".format("gbif_import", layer.name.toLowerCase());
table = 'gbif_import';
- style_table_name = 'styles';
+ style_table_name = 'names_old';
info_query = "SELECT cartodb_id, st_transform(the_geom, 3785) AS the_geom_webmercator, 'GBIF' || '' AS source, 'point' || '' AS type, 'http://data.gbif.org/ws/rest/occurrence/get/' || identifier as URL, scientificname AS name FROM {0} WHERE lower(scientificname)='{1}'".format("gbif_import", layer.name.toLowerCase());
} else {
sql = sql.format(table, layer.name, layer.type);
@@ -20,7 +20,6 @@
<script type="text/javascript" src="../../../js/lib/jquery/js/jquery-ui-1.8.18.custom.min.js"></script>
<script type="text/javascript" src="../../../js/lib/jquery.imagesloaded.js"></script>
<script type="text/javascript" src="../../../js/lib/jquery.tablesorter.js"></script>
- <script type="text/javascript" src="../../../js/lib/jquery.mousewheel.js"></script>
<script type="text/javascript" src="../../../js/lib/underscore-min.js"></script>
<script type="text/javascript" src="../../../js/lib/backbone-min.js"></script>
<script type="text/javascript" src="../../../js/lib/class.js"></script>
View
@@ -6,12 +6,14 @@
import cache
# Standard Python imports
+import hashlib
import logging
import os
import urllib
import webapp2
# Google App Engine imports
+from google.appengine.api import memcache
from google.appengine.api import urlfetch
from google.appengine.ext.webapp.util import run_wsgi_app
@@ -31,23 +33,32 @@ class TileHandler(webapp2.RequestHandler):
def get(self):
tile_url = self.request.url.replace(app_host, 'http://mol.cartodb.com')
- tile_png = cache.get(tile_url, value_type='blob')
+ tile_key = 'tc-%s' % hashlib.sha224(tile_url).hexdigest() # tc means Tile Cache
+ tile_png = memcache.get(tile_key)
if not tile_png:
- tile_png = urlfetch.fetch(tile_url, deadline=60).content
- cache.add(tile_url, tile_png, value_type='blob')
+ tile_png = cache.get(tile_key, value_type='blob')
+ if not tile_png:
+ tile_png = urlfetch.fetch(tile_url, deadline=60).content
+ cache.add(tile_key, tile_png, value_type='blob')
+ memcache.add(tile_key, tile_png)
self.response.headers["Content-Type"] = "image/png"
+ self.response.headers["Cache-Control"] = "max-age=2629743" # Cache 1 month
self.response.out.write(tile_png)
class GridHandler(webapp2.RequestHandler):
"""Request handler for cache requests."""
def get(self):
grid_url = self.request.url.replace(app_host, 'http://mol.cartodb.com')
- grid_json = cache.get(grid_url)
+ grid_key = 'gc-%s' % hashlib.sha224(grid_url).hexdigest() # gc means Grid Cache
+ grid_json = memcache.get(grid_key)
if not grid_json:
- grid_json = urlfetch.fetch(grid_url, deadline=60).content
- cache.add(grid_url, grid_json)
+ grid_json = cache.get(grid_key)
+ if not grid_json:
+ grid_json = urlfetch.fetch(grid_url, deadline=60).content
+ cache.add(grid_key, grid_json)
self.response.headers["Content-Type"] = "application/json"
+ self.response.headers["Cache-Control"] = "max-age=2629743" # Cache 1 month
self.response.out.write(grid_json)
application = webapp2.WSGIApplication(

0 comments on commit 881aa48

Please sign in to comment.