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.

class mvcViewer
{
  function mvcViewer()
  {
    $this->cacheKey = md5( $_SERVER[ 'REQUEST_URI' ] );
  }

  function display()
  {
    // modified to use the cacheKey.
    $this->assign();
    $this->smarty->display( $this->formTplName . ".tpl", $this->cacheKey );
  }

  function is_cached()
  {
    return $this->smarty->is_cached( $this->formTplName . ".tpl", $this->cacheKey );
  }
}

class mvcController
{
  function main()
  {
    switch( $this->page )
    {
    default:
    $this->model = new mvcModel( $form );
    $this->view = new mvcView( $this->model, "mvc-Index" );
    if( $this->view->is_cached() == false )
    {
      $this->model->doSomethingHere();
    }
    break;
    }
  }
}

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!

5 thoughts on “PHP, MVC And Smarty Caching

  1. 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) 🙂

    Just a few notes …

  2. Caching isn’t really a difficult issue once the right key is chosen. Obviously the example above is really simplistic. The cache key for WPMU that runs this site goes something along the following lines..
    if( count( $posts ) == 1 )
    {
    $smartyKey = md5( $comment_author_url.$comment_author.$comment_author_email.$user_login.$_SERVER[ 'REQUEST_URI' ] );
    }
    else
    {
    $smartyKey = md5( $user_login.$_SERVER[ 'REQUEST_URI' ] );
    }

    If you feed the root URL with the parameter, “clear=1”, that’ll clear out the cache too. Implementation details really!

Leave a Reply

%d bloggers like this:

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close