Caching JSON in Django

1,346 views
Skip to first unread message

buddhasystem

unread,
Aug 24, 2010, 5:15:14 PM8/24/10
to django...@googlegroups.com

Hello,

I'm trying to use native caching in Django as per
http://docs.djangoproject.com/en/dev/topics/cache/

I followed the setup procedure to the tee (it's not hard, of course). What I
observe is that my app caches only views that deliver HTML. Since my
application is heavily AJAX, that's not what I want -- I want to cache JSON
-- but it doesn't work!

Any hints how I can make it work?

I can always code it up myself (I have, actually), but I try to avoid that.

Thanks.

--
View this message in context: http://old.nabble.com/Caching-JSON-in-Django-tp29526535p29526535.html
Sent from the django-users mailing list archive at Nabble.com.

Reinout van Rees

unread,
Aug 25, 2010, 6:28:32 AM8/25/10
to django...@googlegroups.com
On 08/24/2010 11:15 PM, buddhasystem wrote:
>
> I'm trying to use native caching in Django as per
> http://docs.djangoproject.com/en/dev/topics/cache/
>
> I followed the setup procedure to the tee (it's not hard, of course). What I
> observe is that my app caches only views that deliver HTML. Since my
> application is heavily AJAX, that's not what I want -- I want to cache JSON
> -- but it doesn't work!
>
> Any hints how I can make it work?

Quick guess: difference in RequestContext between regular html pages and
ajax views?


Reinout

--
Reinout van Rees - rei...@vanrees.org - http://reinout.vanrees.org
Programmer at http://www.nelen-schuurmans.nl
"Military engineers build missiles. Civil engineers build targets"

buddhasystem

unread,
Aug 25, 2010, 7:14:52 AM8/25/10
to django...@googlegroups.com

Sort of -- with AJAX (meaning I just serve JSON) I don't
use RequestContext at all. Just HttpResponse(jsonData), which
works fine for my purposes.

It's really puzzling though why context request should affect
caching. It's a shame I can't use it.

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

--
View this message in context: http://old.nabble.com/Caching-JSON-in-Django-tp29526535p29531093.html

Jani Tiainen

unread,
Aug 25, 2010, 7:16:05 AM8/25/10
to django...@googlegroups.com
> Hello,
>
> I'm trying to use native caching in Django as per
> http://docs.djangoproject.com/en/dev/topics/cache/
>
> I followed the setup procedure to the tee (it's not hard, of course). What
> I observe is that my app caches only views that deliver HTML. Since my
> application is heavily AJAX, that's not what I want -- I want to cache
> JSON -- but it doesn't work!

It doesn't work how? How you observed caching?

> Any hints how I can make it work?
>
> I can always code it up myself (I have, actually), but I try to avoid that.

View code would be helpful and don't forget the caching parts.

--

Jani Tiainen

buddhasystem

unread,
Aug 25, 2010, 7:23:40 AM8/25/10
to django...@googlegroups.com

Thanks for you reply. I'm away from my coding machine, so I'll just answer a
part of your question --

When I use the cache decorator in a view, I expect that as long as cache is
valid, the code in the view
does not get executed. And that's what I see when rendering a template with
some little context attached to it. However, in a different view adorned
with same decorator, which does not use a template and just wraps
HttpResponse around JSON (which is plain ASCII after all) I see that the
whole database glue shebang inside the view comes into motion every time I
hit the view, any time. This correlates with the content of the
CACHE_BACKEND (which in my case is file storage) -- in the former case, I
have cryptic little dirs sprouting up, in the latter I see nothing. I think
that proves the general picture i.e. cache not working -- am I wrong?

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

--
View this message in context: http://old.nabble.com/Caching-JSON-in-Django-tp29526535p29531180.html

Jani Tiainen

unread,
Aug 25, 2010, 7:46:11 AM8/25/10
to django...@googlegroups.com
Is your AJAX query POST or GET query? Because as you may know, POST queries
are not cached, GET queries are.

Note that if you're still using large pieces of JSON data, upstream caches are
not used (data is always sent from Django) which makes some extra load on
Django app.

Jirka Vejrazka

unread,
Aug 25, 2010, 7:54:41 AM8/25/10
to django...@googlegroups.com
> Is your AJAX query POST or GET query? Because as you may know, POST queries
> are not cached, GET queries are.


Also, GET requests are not cached in some cases (e.g. when GET
parameters are used) - it's really difficult to figure out what's
wrong without knowing specific details.

Cheers

Jirka

buddhasystem

unread,
Aug 25, 2010, 8:11:23 AM8/25/10
to django...@googlegroups.com

Thanks much!

a) I'm using GET

b) In current set of queries, JSON data size is quite moderate

PS. I am considering tweaking the links I sent to the AJAX app such that
when possible, it accesses cache file space on the server as a directory,
statically, bypassing Django. That's fast. The set of URLs the AJAX client
is receiving is configurable.

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

--
View this message in context: http://old.nabble.com/Caching-JSON-in-Django-tp29526535p29531559.html

buddhasystem

unread,
Aug 25, 2010, 8:12:50 AM8/25/10
to django...@googlegroups.com

Hello --

I think that might be the case... I do extract parameters from GET.

Am I out of luck?

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

--
View this message in context: http://old.nabble.com/Caching-JSON-in-Django-tp29526535p29531574.html

Jirka Vejrazka

unread,
Aug 25, 2010, 8:23:45 AM8/25/10
to django...@googlegroups.com
> 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)

HTH

Jirka

Piotr Zalewa

unread,
Aug 25, 2010, 8:28:11 AM8/25/10
to django...@googlegroups.com
In such case it would better to not cache entire method, but simply the data - build the unique key per GET requests, check if the data is already stored in cache, if so - use it, else - retrieve it from database and store it in cache.

from django.core.cache import cache
def someview(req):

    par = req.GET.get('somepar')
    key = "someview:%s" % par
    data = cache.get(key, None)

    if not data:
        # get data from database
        # ...
        cache.set(key, data)

    # here normal usage of data

On 10-08-25 13:12, buddhasystem wrote:
Hello -- 

I think that might be the case... I do extract parameters from GET.

Am I out of luck?




Jirka Vejrazka wrote:

        
Is your AJAX query POST or GET query? Because as you may know, POST
queries
are not cached, GET queries are.

Also, GET requests are not cached in some cases (e.g. when GET
parameters are used) - it's really difficult to figure out what's
wrong without knowing specific details.

  Cheers

    Jirka

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




    


Reinout van Rees

unread,
Aug 25, 2010, 8:30:14 AM8/25/10
to django...@googlegroups.com

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

Collega's gezocht!
Django/python vacature in Utrecht: http://tinyurl.com/35v34f9

buddhasystem

unread,
Aug 25, 2010, 2:30:40 PM8/25/10
to django...@googlegroups.com

Thank you so very much. It works like a clock!
I use urlencode to to convert the complete URL hash into a key, that's just
logical.
I wrap set and get. A total of 10 lines of extra code isn't bad, of course
the simple
use of decorator would be even nicer!

--
View this message in context: http://old.nabble.com/Caching-JSON-in-Django-tp29526535p29535253.html

Ravi Bhushan

unread,
Jul 9, 2018, 5:07:51 AM7/9/18
to Django users

Melvyn Sopacua

unread,
Jul 9, 2018, 8:43:40 AM7/9/18
to django...@googlegroups.com
On maandag 9 juli 2018 09:19:59 CEST Ravi Bhushan wrote:

> plzz help me to solve this problem

Ask more times. It really helps getting you quality auto ignores.
--
Melvyn Sopacua
Reply all
Reply to author
Forward
0 new messages