I recently posted this to the Template::Toolkit mailing list, but
the moderator rejected it for some reason. (Probably because <jon-perl-list-templates AT jrock.us> is the address that's
subscribed, not <jon AT jrock.us>.)
Anyway, here it is. I will expand on this further with actual measurements, but this will have to do for now.
Thank you, you raise a good point that I had not fully considered. My goal is to display a dynamically generated page, but where most of the "dynamic" content is actually static for a period amount of time. (Take a news page, for example, where the list of news stories updates once an hour.)
I have a similar problem with my blogging software. Most of the content is static, and it's kind of a waste to regenerate everything every time someone views the page. My solution to this problem is to use a Cache::FastMmap object to store the formatted HTML content of each post, keyed on the source material's MD5sum (and filetype, for reasons not relevant to this discussion :)
This results in a nice speedup, since certain HTML filters are slow (OO.org document -> HTML, for example).
I also cache full pages, after they've been assembled by TT, keyed on the md5sum of each included article and some metadata (comment count, tags, etc.) The end result is a 5-20x speed up. With Catalyst's embedded server, I can do about 5 requests per second without cache, but it goes up to about 40-50 requests/second with the cache. So obviously a speedup. (And I'm still not doing a very good job, this was just a 20 minute hack!)
For the gory details, check out the code here:
http://www.jrock.us/trac/blog_software/browser/lib/Blog/Controller/Root.pm#L105 (page cache)
and: http://www.jrock.us/trac/blog_software/browser/lib/Blog/Model/Filesystem/Item.pm#L355 (object cache)
Another caching strategy is to set dynamic pages to be cachable for say, 10 seconds, and then letting Apache's mod_cache (experimental in 2.2.3, but it works for me) do the hard work. Your application gets hit once every ten seconds to refresh Apache's cache, but all the end-user requests are handled by serving from Apache's memory cache. With mpm_worker, it's *super fast*, and a 10 second update delay isn't bad.
If your application is really slow, this will let you increase your slashdot resistance factor (as I call it) enormously. A quick ab against some cached content gave me a requests/per second rate of 1638... much better than whatever your app would do by itself.
And you don't really lose much by taking this approach, other than a 10 second delay with your articles going live... but 10 seconds is nothing if you're getting 1500 requests per second and handling it with an old PIII :)
If this info is useful to anyone else, let me know (off-list) and I'll write up a more detailed article about it :)
As a programmer, you probably don't spend a lot of time thinking about the similarities between programming and quantum physics. Fortunately for us Perl users, some people do spend a lot of time thinking about physics, and they even wrap up the results in CPAN modules for us!
One such module is Quantum::Superpositions, which let me easily add some nice functionality to my blog software the other day.
As you can see (if you're looking at this article on my blog, not in an RSS reader), I have a list of tags to the left of my main content. If you click one, it shows you a list of articles that have been tagged with that tag. If you select more than one tag, you get this intersection of the two sets (tag1 AND tag2).
That's nice, but I like to use the left navigation bar to show the user where he is in addition to letting him move somewhere else. To do this, I bold the current location, and remove the link. That way, the user won't try to visit the page he's already looking at. This is pretty simple to implement for things like the "home page" or the "archives". You just check the current page, and if the link you're about to print out matches, make it bold instead. That logic looks something like this:
[% FOREACH category = categories %]
[% IF category == current_category %]
<b>[% category %]</b>
[% ELSE %]
[% category %]
[% END %]
[% END %]
Simple, and it provides excellent visual cues as to what the user is looking at.
The problem with tags, though, is that an article can have lots of them. The technique above doesn't work there; you need to do a search for each tag. Not a show stopper, but do you really want to do array searches inside the Template Toolkit? No!
And this is where Quantum::Superpositions comes to the rescue. In quantum physics, almost nothing is "certain" -- you can't tell with 100% certainty both where a particle is and what its velocity is. The state of an individual particle is in sort of a flux until you observe, and when you observe, a tangible state comes into existence.
How does this help with tags? By thinking of the set of all tags as a cloud of particles, we can apply the same principle. We don't care what all the tags are until we look at them.
So how do we do this in perl? Let's see an example:
In my
Tags.pm
Controller, I use the any method from Quantum::Superpositions to
collapse the list of tags into a scalar:
$c->stash->{article_tags} = any(@tags);
Then inside my Template Toolkit View, I "observe" the state as
necessary (with the == operator):
[% FOREACH tag = all_tags %]
[% IF tag == article_tags %]
<b>[% tag %]</b>
[% ELSE %]
[% tag %]
[% END %]
[% END %]
As you can see, this is exactly the same as what we did when we only had one possible value. If you think about it, it's really the same thing. You just needed to think more about physics :)
Anyway, take a look at this in action:
http://blog.jrock.us/tags/%E3%83%A2%E3%83%BC%E5%A8%98%E3%80%82/music
And then add some cool navigation features to your Catalyst app!