I have this code that executes for around 9.2 secs locally on my machine. After deploying it to an AWS ec2 box the execution time raised up to 20secs.
Since I haven't implemented any caching before, I am wondering am I able to cache the response of a specific function or I need to cache every POST I get externally.
Essentially what I am asking for is how to cache the result of a POST, so whenever I ran it again, with the same arguments it uses the cache to faster response.
#!flask/bin/python
from flask import Flask, jsonify
from flask import request
import json
import nltk, string
import operator
from sklearn.feature_extraction.text import TfidfVectorizer
import re
from flask import make_response
import time
import access_json
app = Flask(__name__)
def output(word_list):
start = time.time()
#RUNNING ALGORITHM *****************
comparison_words = get_synonyms(word_list) + word_list
print(comparison_words)
dict_sim = {}
for i in json_list:
dict_sim[i['jobId']] = cosine_sim(str(comparison_words), i['jobDescription'])
sorted_sim = sorted(dict_sim.items(), key=operator.itemgetter(1), reverse = True)
toplist = sorted_sim[:10]
sort_toplist = list(map(operator.itemgetter(0), toplist))
filter_toplist = (list(filter(lambda job: job['jobId'] in sort_toplist, json_list)))
#print('Our top ten suggestion from SkillzMiner are those:\n')
end = time.time()
print (end-start)
# for j in filter_toplist:
# print ('JobTitle: ' + j['jobTitle'] +
# '\nEmployer Name: ' + j['employerName'] +
# '\nLocation: ' + j['locationName'] +
# '\nReed Job URL ' + j['jobUrl'] +
# '\nJob Description: ' + cleanhtml(j["jobDescription"]))
return filter_toplist
@app.route('/postjson', methods=['POST'])
def json_handler():
content = request.get_json(force=True)
word_list = access_json.read_parsed_JSON(content)
return jsonify ({'jobs': output(word_list)})
if __name__ == '__main__':
app.run(debug=True)
app.run(host='0.0.0.0', port=80)
P.S. I have ommited some non-related functions, this is not the full code. Apologise for the terminology, I am new to this web services.