0

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.

Mario
  • 25
  • 1
  • 8
  • 1
    I'm not sure what you're asking. You can cache responses, but does the calculation differ with each request? – roganjosh Jan 12 '19 at 12:14
  • That's what I need, to cache responses, so whenever I test it with giving the same arguments, instead of running for 20 secs to use the cache and give a response in 2 secs. @roganjosh – Mario Jan 12 '19 at 12:34
  • You can setup a nginx in front of your app and caching in nginx may be done in minutes through the config. – Fine Jan 13 '19 at 08:49

1 Answers1

0

Does the caching need to be local and user specific?

Have you tried caching in session variables like session['my_variable'] = my_response

That should work for a users local sessions as long as they don't, for example, visit in incognito mode.

Avian
  • 126
  • 1
  • 9