Cole
Cole

Reputation: 2509

django pre-calculate and cache a view

This might be a python question. It is a noobish one to be sure.

A client requests a calculation-intensive page [page-1] and will ultimately request a second calculation-intensive page [page-2], which can be calculated the instance the request for page-1 is known. I don't want to calculate each set of data before serving page-1 because it will significantly slow down the performance of the initial response.

I do want to calculate the value for page-2 while the client reads page-1. The client also might click on some buttons which cause a response that provides a different view of page-1 data, but don't necessitate an intensive calculating. Eventually but not necessarily immediately, the client will ask for page-2 and I want to be able to response with a pre-rendered response.

How do I do this?

Upvotes: 7

Views: 1453

Answers (1)

csinchok
csinchok

Reputation: 751

As mentioned in the comments, it sounds like you're going to need to handle this with an asynchronous background task, saving the result in the Django low level cache. I would personally use celery for the task queue.

Basically, after page one is requested, you would add an asynchronous task to start the page 2 calculations, storing the result in the cache. So, when page 2 is requested, you check for the pre-rendered response in the cache, and if it doesn't exist, you could calculate the value synchronously.

So, your code would look something like this (the task would be in a task.py file in your app, but this should give you a general idea):

from celery import task
from django.core.cache import cache

def page_two_calculation(arg1, arg2):
    return arg1 + arg2

@task
def page_two_task(arg1, arg2):
    result = page_two_calculation(arg1, arg2)
    cache_key = "page-two-%s-%s" (arg1, arg2)
    cache.set(cache_key, result)

def page_one(request, arg1, arg2):

    # Start the page two task
    page_two_task.delay(arg1, arg2)

    # Return the page one response
    return HttpResponse('page one')

def page_two(request, arg1, arg2)
    cache_key = "page-two-%s-%s" (arg1, arg2)
    result = cache.get(cache_key)
    if result is None:
         # the result will only be None if the page 2 calculation
         # doesn't exist in the cache, in which case we'll have to
         # return the value synchronously.
         result = page_two_calculation(arg1, arg2)
    return result

Upvotes: 9

Related Questions