PHP, MVC And Smarty Caching

Following on from previous posts on PHP and MVC and in particular on using Smarty caching in the MVC design pattern, I used Smarty to cache the output of an app at work this morning.
I agonised in my last post about where to put the caching, should it go into the viewer, or the model, or the controller?

I finally decided to make the viewer responsilble for deciding if the template is cached or not.

The controller then asks the viewer if the current page is cached, if it’s not, then it runs whatever functions the model needs to generate the page.

The beauty of this is that the model doesn’t need to know about the caching at all. The developer can concentrate on the business logic of his application without worrying about the underlying architecture. That’s one reason for keeping the cache key in the viewer too. Don’t lets confuse the developer!
Of course, this presumes the most simplistic caching system, but it would suffice for most cases!

One note about caching in your viewer: What if your application needs to be cached, but only for certain users? The cache key of $_SERVER[‘REQUEST_URI’] works fine for that, but you might want to add a $userID to the key. Another thing to think about is that you shouldn’t be caching it all if the user logged in is an administrator (how annoying to develop on a site where you have to wait 20 minutes to clear the cache) 🙂

The Golden Compass
First of a three part fantasy/sci-fi series. Some people hate it because of it's anti God message but it's a great read. I found it hard to put down. There's even a Snopes article about the film adaptation.