More caching nonsense

This commit is contained in:
RunasSudo 2019-01-11 22:08:39 +11:00
parent 6553e77c02
commit 66c39629fc
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A

View File

@ -16,14 +16,14 @@ from urllib.parse import urlencode
from urllib.request import Request, urlopen from urllib.request import Request, urlopen
request_cache = {} request_cache = {}
def do_request(endpoint, args=None): def do_request(endpoint, args=None, cachetime=3600):
url = endpoint + '?devid=' + config.PTV_USER_ID url = endpoint + '?devid=' + config.PTV_USER_ID
if args: if args:
url += '&' + urlencode(args) url += '&' + urlencode(args)
# Check cache # Check cache
timenow = pytz.utc.localize(datetime.utcnow()).astimezone(timezone) timenow = pytz.utc.localize(datetime.utcnow()).astimezone(timezone)
cachexp = timenow - timedelta(seconds=60) cachexp = timenow - timedelta(seconds=cachetime)
if url in request_cache: if url in request_cache:
if request_cache[url][0] >= cachexp: if request_cache[url][0] >= cachexp:
# Use cached response # Use cached response
@ -183,7 +183,7 @@ def latest():
result = {} result = {}
result['time_offset'] = timenow.utcoffset().total_seconds() result['time_offset'] = timenow.utcoffset().total_seconds()
departures = do_request('/v3/departures/route_type/{}/stop/{}'.format(ROUTE_TYPE, flask.request.args['stop_id']), {'platform_numbers': flask.request.args['plat_id'], 'max_results': '5', 'expand': 'all'}) departures = do_request('/v3/departures/route_type/{}/stop/{}'.format(ROUTE_TYPE, flask.request.args['stop_id']), {'platform_numbers': flask.request.args['plat_id'], 'max_results': '5', 'expand': 'all'}, cachetime=60)
departures['departures'].sort(key=lambda x: x['scheduled_departure_utc']) departures['departures'].sort(key=lambda x: x['scheduled_departure_utc'])
if len(departures['departures']) == 0: if len(departures['departures']) == 0:
@ -229,7 +229,10 @@ def get_station_list():
stns.sort(key=lambda x: x[1]) stns.sort(key=lambda x: x[1])
if not app.debug: if not app.debug:
get_station_list() try:
get_station_list()
except Exception as ex:
pass
@app.route('/stations') @app.route('/stations')
def stations(): def stations():