title: Python: Flask Cache for a range of time
body: <p>My Flask app, will get data from an url only from certain time. If it is outside the range of time, it will used the last query data from the url that save in Cache. Outside the range of time, the url will return no data. Thus, I want to reuse the last data in cache</p>
  
  <pre><code>from flask_app import app
  from flask import jsonify,abort,make_response,request
  from flask.ext.sqlalchemy import SQLAlchemy
  from flask.ext.cache import Cache
  from datetime import datetime, time
  
  app.config['CACHE_TYPE'] = 'simple'
  app.cache = Cache(app)
  
  @app.route('/top', methods=['GET'])
  @app.cache.cached(timeout=60)
  def top():
      now = datetime.now()
      now_time = now.time()
      if now_time &gt;= time(10,30) and now_time &lt;= time(16,30):
         print "within time, use data save in cache"
         # function that use last data query from url, save in cache
      else:
         page = requests.get('http://www.abvfc.com')
         data = re.findall(r'items:(.*)',page.content)
  return jsonify(data)
  </code></pre>
  
  <p>The problem is I can't get the last Cache data. If there is no access to the api /top in the last 60 seconds, there will be no data.</p>
  
  <ol>
  <li><p>Cache the data one minutes before the url return no data at 16.30</p></li>
  <li><p>User can use the cache data outside range of time</p></li>
  </ol>
  
  <p>I am not familiar with cache, so may be my current idea is not the best way.</p>