Gidsy/django-threaded-messages — comment on commit 81455bbdb0bfefd4787f5b12272b96b85ba65a8f
File: threaded_messages/context_processors.py line 5
By avsd:

It's better to make cached lazy instance of counter, instead of calculating count of messages for each call of context processor. For example:

def lazy_counter(user):
def cached_counter():
if not hasattr(user, 'inbox_count'):
user.inbox_count = inbox_count_for(user)
return user.inbox_count
return cached_counter

...

return {'messages_inbox_count': lazy_counter(request.user)}

This will put a callable to the context, which will be called on demand, only once.