librelist archives

« back to archive

[Code review ]: Caching a function for a day

[Code review ]: Caching a function for a day

From:
amri.aymen@gmail.com
Date:
2013-12-14 @ 18:33
I am writing a flask app that I will deploy it on Heroku to test it. 
There is a page that will generate daily photos and information based on 
a JSON file. The view is valid for 24 hours beginning from 00:00 to 
23:59 of everyday.

I am thinking about using a cache, otherwise the app will consume 
unnecessary resources.


The is the code :

|import  ..


TODAY=  date.today()

  app=  Flask(__name__)
  app.jinja_env.auto_reload=  False
  cache=  Cache(config={'CACHE_TYPE':  'simple',  'CACHE_ARGS'  :  TODAY})
#This function reads a json file, filter the daily data to process and 
render a html template with photos and text .

# 3600 *  24 = 86400
@cache.cached(timeout=86400)
@app.route('/inf')
def  inf():
     input_file=  open(os.path.join(__location__,  'json/output.json'));     
     j=  json.loads(input_file.read().decode("utf-8-sig"))
     etc..

     return  render_template('inf.html',  
                         title=  title,
                         config={'environment_args':{'autoescape':False}},
                         zipped=zipped)  


if  __name__==  '__main__':
     if  TODAY!=  cache.config['CACHE_ARGS']  :
         with  app.app_context():
             cache.clear()                 
     port=  int(os.environ.get('PORT',  33507))
     app.run(debug=  True,  host='0.0.0.0',  port=port)


|

I am not sure if this is the best solution to cache the view for a day. 
That's why I am asking here for more suggestions. Thanks in advance .

-- 
-------------------------------------------
Aymen Amri