Hacker News new | past | comments | ask | show | jobs | submit login
Django tip: Caching and two-phased template rendering (holovaty.com)
58 points by mace on May 18, 2009 | hide | past | favorite | 6 comments



We do something like this -- we call it "partial currying" .. if you think of a partial (in Rails) as a function of a number of parameters, you can render and cache the intermediate rendering of a subset of those parameters (curry them.) Subsequent calls can then specify the values for the rest.

The mechanism we use for this is to do some ruby metaprogramming to kludge in some placeholder values for the uncurried values, and then gsub them in in the subsequent passes. (So, the first pass is the only pass that runs the HAML/ERB file, the rest are gsub's) This works for us, since things after the first pass are not changing layout, etc, and need to be high performance. The first pass is a heavyweight rendering at a coarse granularity (and cached), subsequent curries are lightweight gsubs at fine grain.


Is it possible to do the same thing in PHP? Authenticated users performance is one of the areas Drupal could be improved, and this looks like it could help a lot.


The quick-and-dirty way is to do it like Smarty does: compile what you want to cache and write that as a .php file that includes the user-dependent data to disk.

But, if I'm not mistaken, Drupal with users is slow because pretty much any function in the system is allowed to change its output depending on the logged in user, right? And that's pretty tricky to cache.


Yeah, you can do it in PHP, but the process for getting it to work includes banging your head on the table, getting drunk, and slitting your wrists.

I'll take Django, thanks.


While I've not used it yet myself, 'edge-side includes' seem really appropriate for such problems -- offloading the caching of the large, unchanging part to a very efficient raw HTTP cache, only passing through customization hits for a tiny subset of the page (and even allowing those to be cached and invalidated separately). See:

http://en.wikipedia.org/wiki/Edge_Side_Includes


caching in asp.net web controls is insanely easy. ironic that in mvc land (asp.net mvc included) it is so difficult.




Consider applying for YC's W25 batch! Applications are open till Nov 12.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: