Skip to content

Commit

Permalink
Load placeid2dcid on server start to speed up place search (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
shifucun authored Jul 23, 2020
1 parent efe2c83 commit 4448a9d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
7 changes: 7 additions & 0 deletions server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import os

from flask import Flask
from google.cloud import storage
from werkzeug.utils import import_string


Expand Down Expand Up @@ -57,4 +58,10 @@ def create_app():
chart_config = json.load(f)
app.config['CHART_CONFIG'] = chart_config

# Load placeid2dcid mapping from GCS
storage_client = storage.Client()
bucket = storage_client.get_bucket(app.config['GCS_BUCKET'])
blob = bucket.get_blob('placeid2dcid.json')
app.config['PLACEID2DCID'] = json.loads(blob.download_as_string())

return app
17 changes: 2 additions & 15 deletions server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,25 +115,12 @@ def api_placeid2dcid(placeid):
This is to use together with the Google Maps Autocomplete API:
https://developers.google.com/places/web-service/autocomplete.
"""
mapping = get_placeid2dcid()
if placeid in mapping:
return mapping[placeid]
if placeid in app.config['PLACEID2DCID']:
return app.config['PLACEID2DCID'][placeid]
else:
flask.abort('dcid not found for %s' % placeid, 404)


# Getting the blob at module level will make the server crash when deploying
# to AppEngine. Make it in a function and cache for 30 days will effectively
# achieve the same caching effect.
@cache.memoize(timeout=3600 * 24 * 30) # Cache for 30 days.
def get_placeid2dcid():
# Instantiates a client
storage_client = storage.Client()
bucket = storage_client.get_bucket(GCS_BUCKET)
blob = bucket.get_blob('placeid2dcid.json')
return json.loads(blob.download_as_string())


@cache.memoize(timeout=3600 * 24) # Cache for one day.
@app.route('/api/mapinfo/<path:dcid>')
def api_mapinfo(dcid):
Expand Down

0 comments on commit 4448a9d

Please sign in to comment.