From 4448a9d921e6c15f08476bbfc2e831ea69f41682 Mon Sep 17 00:00:00 2001 From: Bo Xu Date: Thu, 23 Jul 2020 15:44:16 -0700 Subject: [PATCH] Load placeid2dcid on server start to speed up place search (#160) --- server/__init__.py | 7 +++++++ server/main.py | 17 ++--------------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/server/__init__.py b/server/__init__.py index 6321d35aa6..2a41459a57 100644 --- a/server/__init__.py +++ b/server/__init__.py @@ -16,6 +16,7 @@ import os from flask import Flask +from google.cloud import storage from werkzeug.utils import import_string @@ -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 diff --git a/server/main.py b/server/main.py index a2b4da25d5..0532275ae9 100644 --- a/server/main.py +++ b/server/main.py @@ -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/') def api_mapinfo(dcid):