Caching Paginated Results
I simplified the previous example somewhat so as not to get lost in myriad unrelated details. However, now that you understand how the general caching process works, let’s dive a bit deeper into how I go about caching paginated data. Because each page within the paginated list is of course unique, repeatedly caching the data using the
games
token isn’t acceptable. Instead, I use the page number to create a dynamic token, like this:$cacheToken = $this->platform->name."_".$this->page;
Therefore, every time the page changes, the cache token will be updated accordingly, creating a new cache file (e.g.
xbox_1
, xbox_2
, xbox_3
…). This token is then passed into the load()
method just as before:... if ( ($gamesPaginator = $cache->load($cacheToken)) === false) { ...
Caching Resources
If you’re building a website that will receive any significant amount of traffic, then caching is one of the most effective and easiest ways to boost performance and keep your users happy. Be sure to check out the following resources for more information:
- Zend_Cache: The Zend_Cache documentation
- The Alternative PHP Cache (APC): A PECL extension useful for caching PHP data
- Memcached: A powerful open source caching system used by many of the world’s largest websites, among them Craigslist, Twitter, and Wikipedia
About the Author
Jason Gilmore is the founder of the publishing and consulting firm WJGilmore.com. He also is the author of several popular books, including “Easy PHP Websites with the Zend Framework”, “Easy PayPal with PHP”, and “Beginning PHP and MySQL, Fourth Edition”. Follow him on Twitter at @wjgilmore.