memcached caches request? - django

after caching several views on my django project -#cache_page(60 * 5)- I've noticed that memcached caches the entire view, even the request argument! So if the first user that visits a page is logged in as userxyz, all other anonymous or registered users that will ask the same page will be presented with the page that was cached by user userxyz! Obviously this is not a desired behavior...So can I cache everything on the view, but not the request argument? Or memcached is suitable for anonymous sessions only?
Thanks in advance,
Markos Gogoulos

If you're mixing dynamic and static data on one page, in your case the dynamic data is the logged in user's username, then page caching isn't the right choice. This wouldn't change if you were using file based cache storage instead of memcached.
I suggest trying fragment caching. You can do something like this:
{% load cache %}
{% cache 500 sidebar %}
.. sidebar ..
{% endcache %}
This will cache the contents of the cache tag for 500 seconds with the identifier sidebar.
You can find more information on caching here:
http://docs.djangoproject.com/en/dev/topics/cache/
If this is a page that is going to be hit very often, for example a welcome page, that you feel would benefit from using page caching over fragment caching (for example the only dynamic data is the user name), then there are a few other options.
Say for example you want to have a completely static page except for a login/logout section at the top which displays different links depending on whether or not the user is logged in then you can check for the existence of an authentication cookie when the page is first loaded and conditionally display different data using javascript.

Memcached is just a backend. It caches whatever you tell it to cache. So really your question is "Is Django's full-page caching suitable for dynamic pages?"
Probably you don't want do cache full-pages, just part's of it. Or only pages for anonymous requests (using CACHE_MIDDLEWARE_ANONYMOUS_ONLY)
Refer to the book
http://www.djangobook.com/en/1.0/chapter13/

You might want to look into template fragments and caching those bits of content that aren't user specific.

Related

How to cache dynamic content of my website

I have a website developed using Flask framework.
I want to cache all my pages to make my website faster and increase traffic on search engines.
However, the page has user-specific dynamic content.
I would really appreciate it if you could tell me how to cache dynamic content.
User-specific dynamic content is only good for one user, so caching it is not useful, and Cloudflare doesn't cache it by default. If you force it to, it will break your site, because users will get content intended for other users.
Cloudflare will, however, cache your images, CSS, Javascript, and so forth.
I solved this dynamic content caching problem using ajax requests for user-specific data.
After loading static content, I requested user-specific data such as current user information via ajax.
I set cache option to false when making ajax request.
In this way, I can solve the dynamic content caching problem.

django memcached optimalization

I am deploying social networking site with django on VPS, my current stack is nginx, postgresql, gunicorn. I am going to add memcached, I will be forced to use {% cache %} tag in my templates or low level api in views though, as each site will be dynamic for authenticated users. I have 1 question regarding {% cache %} tag, lets say i will input a variable within cache tag like so:
{% cache 500 x %}
{{ variable }}
{% endcache %}
Lets assume that {{ variable }} is in fact very consuming database query written in related view. What would be the best way to prevent database hit from view (lets assume query will be evaluated in view) , should i cache it as well with low level api? If so isnt it a bit redundant as I would cache it in both places? Please give me your thoughs what would be the most convenient method. Btw, I know that this example is trivial, if I only wanted to cache variable I could do this in view, but I will surely need to use cache tag to cache some loops, multiple html lines generated by python etc. Thanks
First, the usage of authentication in your site doesn't involve the usage of low level cache in both sides views and templates, note for instance you can do something like {% cache 500 user %} and that will save different chunk of code of each user of your site.
Answering your second question: in general, you can prevent database hits just caching into the templates. Evaluate the query there instead of doing into the view, and make a dynamic clean up of the keys when the content changes instead of using fixed expiration times, that way let you get better performance result. Also, take in mind cache tag saves just chunk of HTML code.

partly caching a view method

I am using Memcached (with python-memcached binding) with one of my django projects. The scenario is that on the homepage i have:
A Top Bar : (This contains the links to login / User name with a link to the profile)
A Search Form
Few Blocks Of Results
Currently, I am using the cache_page() decorator to cache the whole page as follows:
#cache_page(3600)
def home(request):
# View Code Goes Here
and the above is working as expected. But as the homepage is a publicly accessible page i am facing a problem with one scenario where:
An anonymous user request the home page(the page get's cached if it
is not already).
Now the user logs in and is redirected to the homepage.
The cached homepage loads (Topbar still shows a login link instead of the logged in user's Name and profile link as the page was cached before the user logged in.)
Question:
Is there a way either on template level or on view level, that lets us specify a block we DO NOT want to cache while using cache_page() decorator ?
Please Note: I am aware that we can use {% cache %} template tag or cache.set for each block in the above scenario. But i am specifically looking for a solution where we can use the cache_page() decorator and still be able to specify a block that i do not want cached in a particular view or a template
use CACHE_MIDDLEWARE_ANONYMOUS_ONLY
yet it sounds as a middleware option, it affects #cache_page as well

Django: caching all but user auth in page

I'm experimenting with various Django 1.3 caching approaches to use for our landing page. This page has results from several database queries as well as user authentication. By far the best-performing solution has been to use the #cache_page decorator to cache the whole view function response. The problem is that user auth also gets cached.
I can create a key in the cache for each user but this seems wasteful; caching the same landing page over and over with the only difference being the user's authentication. I also used template fragment caching and low-level caching for all but the authentication but the performance was nowhere near as good as the #cache_page decorator approach.
Is there a way to get the performance of #cache_page without lots of redundant user-based caching?
Check out this blog post: http://www.holovaty.com/writing/django-two-phased-rendering/
Basically the technique is to wrap your user-specific segments in a template tag that prevents the template engine from rendering them, then cache, and then re-render the cached output to fill out the user details.
Django 1.5 includes a tag called verbatim which can be used instead of the custom raw tag in the post; you'll still need the custom CachedTemplateMiddleware or equivalent though.

Django Caching for a Blog

I have a blog written in Django, and I started using the basic Django caching middleware with the file system caching backend. Unfortunately, this led to two things being cached that shouldn't have been: admin links (e.g. "Edit this post") for logged-in users and
prepopulated comment forms based on cookies.
To get around that, I started using the template cache tags:
{% load cache %}
...admin links...
{% cache 500 blog_entry entry.id %}
...entry...
{% endcache %}
...comment form...
But it seemed that the whole page was still getting cached as well. How do you set up the caching system to only cache the parts of the template you explicitly set?
Edit: For the comments, if someone comments on the blog, I store their name, website, and email address in the session variables. If they come back to the site, then I prepopulate those parts of the form with that data. But that means it is possible for the caching system to cache a view with prepopulated data, which is not good.
You need to remove the caching middleware now that you are caching template fragments instead of entire pages.
Add this to your settings.py:
CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True
This should fix the issue with admin links, I don't quite understand what your second issue (prepopulated comments) is?