0

I am working on a django application. The main task of the application is providing suggestion like "Should I go outside today?". There is only a single endpoint to get the suggestion such as example.com/.

The main logic for providing the suggestion is:

  1. Does the user has any pending task today? (querying from UserTaskModel)

  2. Is today's weather is comfortable? (calculating weather forecasting)

If two user try to fetch data at the same date then the UserTask query will be different. But weather forecast query task will be same. If I use view based django caching then the weather forecast query will be execute for each user. But I want to cache the weather query data for all user at a same date. It can view implement by creating different view for the weather. But I don't want to use another endpoint for the weather.

Django cache set-get method can be use for this task. But is this way is the best way to do this type of task? In my example I use a simple weather calculation query depending the the date. But is this technique is good for complex query?

1 Answers1

0

As you said cache set-get is your solution. but note these two things:

  • Assuming you want to cache weather for each day, set expire time +24h (cache won't expire soon)
  • Also your cache key should be something like weather_2019_09_22

I think creating a utility class can be really good (something like this, this is a pseudo code)

class WeatherCache:
    def get(self):
        date = today()
        if forecast for date in cache:
            return forecast
        get forecast for date
        insert forecast into cache
        return forecast

Another idea can be simply creating a model and putting forecasts there, the good point is you will keep the history of forecasts. maybe it can be useful for later queries (and the table won't get too big so you don't need to worry about it)

aliva
  • 5,450
  • 1
  • 33
  • 44