2

I have many routes on blueprints that do something along these lines:

# charts.py
@charts.route('/pie')
def pie():
  # ...
  return jsonify(my_data)

The data comes from a CSV which is grabbed once every x hours by a script which is separate from the application. The application reads this using a class which is then bound to the blueprint.

# __init__.py
from flask import Blueprint
from helpers.csv_reader import CSVReader

chart_blueprint = Blueprint('chart_blueprint', __name__)
chart_blueprint.data = CSVReader('statistics.csv')

from . import charts

My goal is to cache several of the responses of the route, as the data does not change. However, the more challenging problem is to be able to explicitly purge the data on my fetch script finishing.

How would one go about this? I'm a bit lost but I do imagine I will need to register a before_request on my blueprints

corvid
  • 10,733
  • 11
  • 61
  • 130

2 Answers2

1

ETag and Expires were made for exactly this:

class CSVReader(object):
    def read_if_reloaded(self):
        # Do your thing here
        self.expires_on = self.calculate_next_load_time()
        self.checksum = self.calculate_current_checksum()


@charts.route('/pie')
def pie():
  if request.headers.get('ETag') == charts.data.checksum:
      return ('', 304, {})
  # ...
  response = jsonify(my_data)
  response.headers['Expires'] = charts.data.expires_on
  response.headers['ETag'] = charts.data.checksum
  return response
Community
  • 1
  • 1
Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
1

Sean's answer is great for clients that come back and request the same information before the next batch is read in, but it does not help for clients that are coming in cold.

For new clients you can use cache servers such as redis or memcachd that can store the pre-calculated results. These servers are very simple key-value stores, but they are very fast. You can even set how long the values will be valid before it expires.

Cache servers help if calculating the result is time consuming or computationally expensive, but if you are simply returning items from a file it will not make a dramatic improvement.

Here is a flask pattern for using the werkzeug cache interface flask pattern and here is a link to the flask cache extention