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.
Related
I have a django application and I would like to enable cloudflare to speedup the web response.So I would like to know how much cloudflare can caches my application?
In the application most of the pages are dynamic and eachone atleast contain the loggedin user name.Is there any other way cloudflare handle highly cacheable websites
CloudFlare's CDN caches the following types of static content by extension for all account types by default:
css,bmp,tif,ttf,docx,js,pict,tiff,eot,xlsx,jpg,csv,eps,woff,xls,jpeg,doc,ejs,otf,pptx,gif,pdf,swf,svg,ps,ico,pls,midi,svgz,class,png,ppt,mid,webp,jar
From this page, it is said that:
If you are looking for more advanced caching or performance options,
please look at our paid plans that can help extend caching and
performance for your site.
Also, the about Cloudflare's caching mechanism, its explained in this page:
To speed up the response time for a request that goes to a one of our
frontline servers, CloudFlare caches parts of websites that are static
in these servers. For example, we cache things like images, CSS, and
JavaScript. We are very conservative with our caching because we never
want to mess up dynamic content. So, for example, as a general rule we
do not cache HTML. We also refresh the cache relatively frequently, so
files are never more than a few hours old. Even being conservative,
however, typically 50% of the resources on any given web page are
cacheable.
And about the point about eachone atleast contain the loggedin user name.. Its related to django backend. I have used a django powered site with cloudflare, had no problem with logged in users.
You can check this library for caching: https://github.com/koalalorenzo/django-smartcc
Try a page rule like this as your last one:
example.com/
There should be an asterisk at the beginning and end of this rule
Choose "cache everything." This will cache html.
Test results on your dynamic content caching.
The static page content comes from Django CMS which makes it dynamic , How is using nginx in such a scenario beneficial ?
It is generally considered a best practice to have "static" content served by a traditional web server such as Nginx or Apache. By static content, I am referring to things like CSS, JavaScript, and Images. Since these files often don't require modification between requests, there is little sense in having Django serve them. And Nginx/Apache can be serving these files to the client's browser concurrently.
However, your dynamic content served by Django needs to be rendered within the context of one or more Django templates. Not to mention, content such as blog posts needs to be retrieved from the database. Fortunately, Django has a robust caching system that you can use to cache the rendered HTML output and decrease server load and response times.
How much of a performance boost you gain really depends on the situation. I can tell you from experience that caching a complex response has reduced the response time from 300ms down to 30ms in one scenario.
EDIT: Adding a great article on Scaling Django: Caching and Static Content.
My UI designer is making me a rich html and java script front-end which is supposed to work with my Django server. I have provided him a bunch of APIs to get different elements from the server. (i.e. an api to load the images, another api to load some text, another api to load bigger size image when somebody clicked on a small one, etc.) The problem with this is that every time a user visits the page, I will be getting some number of requests.
On the other hand, I could have used django template tagging to render the page once with all the elements needed. however,
I was wondering if there is a clear benefit in one of these options?
As #limelights said, try not to optimise too early. Because you don't really know what will be your bottleneck.
And when you'll have problems, the first thing to check is your queries to the database. Do you use selectect_related() and are your indexes optimal for your requests. This include the queries that javascript make via django (Try to load all the child node needed in one or two requests, as select_related() do).
After that the next optimisation should probably be cache. Here's the doc about the django's cache framwork.
After that there's adanced optimisation with database denormalization, apache or nginx optimisation, mysql or postgresql settings optimisation, load balancing and etc... Those optimizationg should be done because you have a problem of popularity, not just for a few users.
I have a handful of users on a server. After updating the site, they don't see the new pages. Is there a way to globally force their browsers and providers to display the new page? Maybe from settings.py? I see there are decorators that look like they do this on a function level.
Depends on browser and cache settings.
There may be no way to tell browsers to do so (as pages are cached, they are not even talking to server, so there is nothing You can do there).
Good trick is to set Vary: Cookie header, so You can always invalidate cache (by changing cookie somewhere) in case of need.
One way to force the browser to load a new page rather than loading the cached version is to change the file name. You could add a date/time to the file name and use a rewrite rule (assuming Apache web server here) to get the new page.
This site gives a quick explanation: http://www.askapache.com/htaccess/mod_rewrite-fix-for-caching-updated-files.html
and google will show many more.
you may also have to examine your cache control headers.
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.