Wednesday, August 25, 2010

Re: Caching JSON in Django

On 08/25/2010 02:23 PM, Jirka Vejrazka wrote:
>> I think that might be the case... I do extract parameters from GET.
>>
>> Am I out of luck?
>
> Well, nothing can stop your from doing the caching manually so you can
> tune it exactlly to your needs. It's actually not that difficult:
>
> from django.core.cache import cache
>
> def my_view(request):
> # some way to determine exact instaned of JSON data needed
> cache_key = 'some_way_to_uniquely_identify_json_data'
> json_data = cache.get(cache_key)
> if not json_data:
> # get the response data the hard way
> json_data = get_json_data()
> cache.set(cache_key, json_data)
> return HttpResponse(json_data)

Good example.


Small addition:

If you replace "if not json_data:" with "if json_data is not None", you
can also cache empty values. Otherwise you might do an expensive
calculation for [] over and over again without caching it :-)


Reinout

--
Reinout van Rees - reinout@vanrees.org - http://reinout.vanrees.org
Collega's gezocht!
Django/python vacature in Utrecht: http://tinyurl.com/35v34f9

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate