LogoPhly, boy, phly
the weblog and site of Matthew Weier O'Phinney

Tuesday, November 8. 2005

Simple Caching for PHP

I ran across an article on "How to build a simple caching system, with PHP" on PHPit today. Overall, it's a fairly decent article, and uses some good principles (using the output buffer to capture content, using a callback to grab the captured content). There are a few minor improvements I'd make, however.

There are some definite areas I'd change. First off, die() doesn't make sense to me as a way to abort execution if content is found. exit(0) makes more sense -- it indicates that execution was successful.

Also, it's often useful to have the ability to clear the cache for a given page. Adding a check for a $_GET or $SERVER['PATH_INFO'] element could accomodate that.

The article says to include the file with the caching functions on every page. I have a couple issues with that:

  1. Too easy to forget to include it.
  2. Namespacing -- using function in the global area could conflict with other user defined functions.

To solve (1), use an auto_prepend_file directive. You can do this in a .htaccess file ('php_value "auto_prepend_file" "/path/to/cache.php"), in the httpd.conf, or directly in your php.ini; in either case, it only needs to be defined once, and you never have to worry about it in your scripts. If there are scripts you never want to cache, you can override the value in individual .htaccess directives, or setup a hash-lookup in the caching routines to skip such scripts.

To solve (2), wrap the functions into a class. You could still call items statically (Cache::get_url(), etc.). This would also allow you to define the hash lookup as noted above -- simply place it in a static property.

Finally, this has all been done before. PEAR::Cache_Lite offers all of this functionality (minus the routines to create unique identifiers per page), and a little more -- efficiently, even. The only difference I've seen in practice is that when using PEAR::Cache_Lite, I had to do an auto_append_file as well to stop the output buffering.

Overall, a nice article and introduction to page caching.

Posted by Matthew Weier O'Phinney in PHP at 14:32 | Comments (2) | Trackbacks (0)

Trackbacks
Trackback specific URI for this entry

No Trackbacks

Comments
Display comments as (Linear | Threaded)

Thanks for the link you provided. Reading the article helped me, but your own suggested improvements were more helpful. Can you post your own version of "How to build a simple caching system, with PHP" and put your two cents to it?
#1 Wong PoK (Link) on 2005-11-09 00:26 (Reply)
I'll work on that, and update this node to point to it once I have.
#1.1 Matthew Weier O'Phinney (Link) on 2005-11-09 10:01 (Reply)

Add Comment

Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

 
 
  • Home
  • Resume
  • Blog
  • Phly PEAR Channel
  • Contact Me
  • About this site

ZCE

Zend Education Advisory Board Member

Add to Technorati Favorites

Calendar

Back October '08
Mon Tue Wed Thu Fri Sat Sun
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    

Quicksearch

Links

  • PHLY - PHp LibrarY
  • Paul M. Jones
  • Mike Naberezny
  • Shahar Evron
  • Planet PHP
  • Zend Where I now work
  • Garden.org Where I once worked

Archives

October 2008
September 2008
August 2008
Recent...
Older...

Categories

XML Linux
XML Personal
XML Aikido
XML Family
XML Programming
XML Dojo
XML Perl
XML PHP

All categories

Syndicate This Blog

XML RSS 0.91 feed
XML RSS 1.0 feed
XML RSS 2.0 feed
ATOM/XML ATOM 0.3 feed
ATOM/XML ATOM 1.0 feed
XML RSS 2.0 Comments

Show tagged entries

xml best practices
xml books
xml conferences
xml dojo
xml dpc08
xml file_fortune
xml linux
xml mvc
xml oop
xml pear
xml personal
xml php
xml phpworks08
xml programming
xml ubuntu
xml webinar
xml zendcon
xml zendcon08
xml zend framework
© 2004 - present, Matthew Weier O'Phinney
matthew-web <at> weierophinney.net