Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Lazy load" of data from a context processor

In each view of my application I need to have navigation menu prepared. So right now in every view I execute complicated query and store the menu in a dictionary which is passed to a template. In templates the variable in which I have the data is surrounded with "cache", so even though the queries are quite costly, it doesn't bother me.

But I don't want to repeat myself in every view. I guessed that the best place to prepare the menu is in my own context processor. And so I did write one, but I noticed that even when I don't use the data from the context processor, the queries used to prepare the menu are executed. Is there a way to "lazy load" such data from CP or do I have to use "low level" cache in CP? Or maybe there's a better solution to my problem?

like image 975
szymond Avatar asked Dec 19 '11 15:12

szymond


2 Answers

Django has a SimpleLazyObject. In Django 1.3, this is used by the auth context processor (source code). This makes user available in the template context for every query, but the user is only accessed when the template contains {{ user }}.

You should be able to do something similar in your context processor.

from django.utils.functional import SimpleLazyObject
def my_context_processor(request):
    def complicated_query():
        do_stuff()
        return result

    return {
        'result': SimpleLazyObject(complicated_query)
like image 58
Alasdair Avatar answered Oct 11 '22 23:10

Alasdair


If you pass a callable object into the template context, Django will evaluate it when it is used in the template. This provides one simple way to do laziness — just pass in callables:

def my_context_processor(request):
    def complicated_query():
        do_stuff()
        return result                      
    return {'my_info': complicated_query}

The problem with this is it does not memoize the call — if you use it multiple times in a template, complicated_query gets called multiple times.

The fix is to use something like SimpleLazyObject as in the other answer, or to use something like functools.lru_cache:

from functools import lru_cache:

def my_context_processor(request):
    @lru_cache()
    def complicated_query():
        result = do_stuff()
        return result                      
    return {'my_info': complicated_query}

You can now use my_info in your template, and it will be evaluated lazily, just once.

Or, if the function already exists, you would do it like this:

from somewhere import complicated_query

def my_context_processor(request):        
    return {'my_info': lru_cache()(complicated_query)}

I would prefer this method over SimpleLazyObject because the latter can produce some strange bugs sometimes.

(I was the one who originally implemented LazyObject and SimpleLazyObject, and discovered for myself that there is curse on any code artefact labelled simple.)

like image 23
spookylukey Avatar answered Oct 11 '22 23:10

spookylukey