How to cache a dynamic web page in django

220 views
Skip to first unread message

Chen Xu

unread,
Aug 7, 2014, 2:23:01 AM8/7/14
to django...@googlegroups.com
Hi Everyone,
I have a dynamic page which generates some data from the database, but the content in the database might be the same for everyone or for a long time, so I wonder what is the best way to cache the page so that it won't be talking to the database every time?


Thanks



--
⚡ Chen Xu

Avraham Serour

unread,
Aug 7, 2014, 5:17:05 AM8/7/14
to django...@googlegroups.com
you can use http headers to cache the whole page


--
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...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CACac-qZUVO289z3Z9s7DziMOhbwYES_Jtv_7tpjH8xMp9-oEEw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Erik Cederstrand

unread,
Aug 7, 2014, 6:15:25 AM8/7/14
to Django Users
Den 07/08/2014 kl. 08.22 skrev Chen Xu <xuch...@gmail.com>:

> Hi Everyone,
> I have a dynamic page which generates some data from the database, but the content in the database might be the same for everyone or for a long time, so I wonder what is the best way to cache the page so that it won't be talking to the database every time?

The answer depends on what kind of changes to the data you will encounter. You can cache either client-side or server-side.

If you know in advance when the next update is, or that it at least won't change (or you can accept serving stale data) for the next x seconds (hours, days, weeks), then you can use standard HTTP cache headers to tell the client to cache the content locally.

If you don't know when the next change is, then you can do one of two things;

a) Find a cheap way of detecting data changes by looking at e.g. a timestamp in the database. Then, render the page from scratch if there was a change, or serve a cached version using the Django cache framework if there was no change.

b) Put the fully rendered page in cache server-side using the Django cache framework (or Varnish), and add a hook into the code that updates the database, which invalidates the cache on updates. You can either update the cache eagerly (force a cache insertion after the invalidation) or lazily (when a user requests the page), depending on your needs.


The performance gain will naturally depend on the rate of database updates vs. page requests.


Erik

Collin Anderson

unread,
Aug 7, 2014, 8:45:16 AM8/7/14
to django...@googlegroups.com
a) Find a cheap way of detecting data changes by looking at e.g. a timestamp in the database. Then, render the page from scratch if there was a change, or serve a cached version using the Django cache framework if there was no change.
You could, for example, have any change in the admin clear the cache

b) Put the fully rendered page in cache server-side using the Django cache framework (or Varnish), and add a hook into the code that updates the database, which invalidates the cache on updates. You can either update the cache eagerly (force a cache insertion after the invalidation) or lazily (when a user requests the page), depending on your needs.
I personally use nginx's proxy_cache for this.

Chen Xu

unread,
Aug 7, 2014, 12:02:49 PM8/7/14
to django...@googlegroups.com
Actually, in my case, my dynamic page includes a template where the template is what I want to cache, can I just cache that template I want to include?

Thanks


--
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...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.



--
⚡ Chen Xu

Collin Anderson

unread,
Aug 7, 2014, 12:22:52 PM8/7/14
to django...@googlegroups.com
You can put {% cache %} template tags around the included template.
Reply all
Reply to author
Forward
0 new messages