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.
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"
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
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
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
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.
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
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
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
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
def someview(req):from django.core.cache import cache
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.
-- blog http://piotr.zalewa.info jobs http://webdev.zalewa.info twit http://twitter.com/zalun face http://www.facebook.com/zaloon
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 - rei...@vanrees.org - http://reinout.vanrees.org
Collega's gezocht!
Django/python vacature in Utrecht: http://tinyurl.com/35v34f9
--
View this message in context: http://old.nabble.com/Caching-JSON-in-Django-tp29526535p29535253.html