0

I'm writing a blog application. All the pages (lists of posts, detail of the post) are really static, I can predict when the must be update (for example when I write a new post or a comment is added). I could use @cache_page to cache entire views.

The only problem is that in every page I have some data collected from Twitter that I want to update every 5 minutes.

Django offers template caching, per-view caching and the low level cache framework. With the low level framework I can avoid calculating most of what must be displayed on the page (like caching Post queries, comments, tags...).

What is the best approach to my problem? How to aggressively cache almost everything for a view / template but a few parts?

I want to avoid using iframes.

Thanks

pistacchio
  • 56,889
  • 107
  • 278
  • 420

2 Answers2

1

You can not exclude certain parts of a Django template for the cache not should this work in any other template engine I know of.

My advice would be to use JavaScript to asynchronously load you're ever changing content. It should be particularly easy with Twitter as the already offer a great API.

It that doesn't suit you, you can always use Django template caching, to cache only parts of your template.

codingjoe
  • 1,210
  • 15
  • 32
  • Thank you for you answer. The problem with template caching is that it avoids rebuilding the page, but generally most of the work is the server-side retrieval of the content to show, so this won't be very beneficial. – pistacchio Mar 17 '15 at 09:26
  • I'm fairly sure you can't really do this client-side anymore (you could in the past, however). Nowadays Twitter requires authentication on all API endpoints, and they've removed the RSS feeds, so you can't just fetch the feeds via AJAX. You can either use one of their embedded widgets that they provide, or do it server-side. – Matthew Daly Mar 17 '15 at 09:31
  • @pistacchio that's why you have the regular cache for. In general cache what's being slow (pep20). Caching the entire page is lazy and that's why on one really uses is. You should rather cache your API calls and very slow template loops. – codingjoe Mar 17 '15 at 09:50
0

One option might be to set up Varnish on the server. I'm not familiar with Varnish myself, but as I understand it you can use Edge Side Includes to cache only certain fragments of a page.

Obviously it may not suit your use case, but it sounds like a possibility.

Matthew Daly
  • 9,212
  • 2
  • 42
  • 83