We've got to maintain a certain level of 'street-cred'.

Smart Caching in CakePHP

CakePHP's view caching can dramatically reduce the load on your server and decrease page load time. I have several requirements that has kept me from using Cake's caching - until now.

First, I have several pages that takes several seconds to build on a four core server and eight gigs of RAM. These pages could be cached but only a certain group of users should see them. CakePHP's caching has precluded access checking.

Second, I use Smarty as my page template language whenever possible for its MVC cleanliness. Smarty pages could not be cached by Cake as it really is outside the scope of the core.

These files are usable by all users, not just Smarty users. The names came from my Smarty use but I also have thtml/ctp files that have to be maintained and the caching was built with them in mind as well.

To use tags in Smarty files, you are limited to the same limited set of operations as Cake's ctp files. For most things Smarty files are cleaner and smaller but not when you are trying to show a dynamic field on a cached page. Here is a sample of showing a user's login name in a Smarty file. `

{{php}} $user=$this->tplvars'session'; if ($user) e('hello, '.$user['login_name']); {{/php}}

` Luckily, this is all I ever do on a cached page. If you find yourself doing much more then you probably don't want caching at the view level but should consider data caching instead.

The following code is a view Helper that should be used in place of Cake's when you need these features. You can use it even if you are not using Smarty but want the ability to do some simple access checking. The cached file will call your controller's beforeFilter() method before running your page. If your beforeFilter() returns false then the page will not render. It should run your code as though it was not cached. I perform a redirect in my beforeFilter() to wherever the user really belongs.

SmartyCacheHelper

The helper should be in your app/views/helpers directory. smarty_cache.php

SmartyView

The view should be in your app/views directory. This view is okay for Cake's ctp and thtml users but is meant for Smarty's tpl users. If your using Smarty then this is the view class you want! smarty.php