Wednesday, April 5, 2017

Using Redis as Django Cache

I am using(trying) redis as a cache to my django app. This is how I am trying to do it.



def postview(request):      post_list = []      if cache.get("posts") == None:             post_list = Post.objects.all()             cache.set("posts", post_list, timeout=None)      else :              post_list = cache.get("posts")      context = {"post_list" : post_list}      return render(request, 'post_list.html', context)    @cache_page(60*15, key_prefix="test_cache")  def new(request):      print("testing")      return HttpResponse("hello, I am mohammed")



This is the output in redis-cli


luvpreet@DHARI-Inspiron-3542:~/test_venv_wrapper/test_redis$ redis-cli 127.0.0.1:6379> select 2 # I have redis db 2 as backend in django settings OK 127.0.0.1:6379[2]> keys * 1) ":1:views.decorators.cache.cache_page.cache_test.GET.26488770af116d67b33750e5f304aa3e.d41d8cd98f00b204e9800998ecf8427e.en-us.UTC" 2) ":1:views.decorators.cache.cache_header..d314df08d6409ed165873dfa23271c50.en-us.UTC" 3) ":1:posts" 4) ":1:views.decorators.cache.cache_page..GET.d314df08d6409ed165873dfa23271c50.d41d8cd98f00b204e9800998ecf8427e.en-us.UTC" 5) ":1:views.decorators.cache.cache_header..26488770af116d67b33750e5f304aa3e.en-us.UTC" 6) ":1:views.decorators.cache.cache_page..GET.26488770af116d67b33750e5f304aa3e.d41d8cd98f00b204e9800998ecf8427e.en-us.UTC" 7) ":1:views.decorators.cache.cache_header.cache_test.26488770af116d67b33750e5f304aa3e.en-us.UTC"



This is the value under one of the keys,


 

127.0.0.1> get :1:views.decorators.cache.cache_page.cache_test.GET.26488770af116d67b33750e5f304aa3e.d41d8cd98f00b204e9800998ecf8427e.en-us.UTC

"\x80\x02cdjango.http.response\nHttpResponse\nq\x01)\x81q\x02}q\x03(U\x0e_handler_classq\x04NU\b_headersq\x05}q\x06(U\rlast-modifiedU\rLast-ModifiedU\x1dWed, 05 Apr 2017 10:56:58 GMT\x86U\aexpiresU\aExpiresU\x1dWed, 05 Apr 2017 15:06:58 GMT\x86U\x0ccontent-typeU\x0cContent-TypeU\x18text/html; charset=utf-8\x86U\rcache-controlU\rCache-ControlU\rmax-age=15000\x86uU\b_charsetq\aNU\x11_closable_objectsq\b]U\acookiesq\tcdjango.http.cookie\nSimpleCookie\nq\n)\x81q\x0b}q\x0cbU\x06closedq\r\x89U\n_containerq\x0e]q\x0fU\x14Hello, I am Mohammedq\x10aU\x0e_reason_phraseq\x11Nub."

This is some serialized value.


The queryset Post.objects.all() is cached and I have no problem in getting this from cache. But I am failing to understand this @cache_page() decorator.


Why is it making so many keys in the redis database ? Please explain the keys made in the redis database. How can I get to know this is working or not ?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/aa101f9a-7d3b-42ef-b289-5ddc1aca29fc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment