PHP SimpleCache Redux

Published 11th May 2010

A while ago I posted a simple PHP class that I had created which allows you to cache API requests in a simple way. I’ve done some more work on the class and made it even simpler to cache your API calls.

PHP SimpleCache Class

So here is the SimpleCache class:

<?php class SimpleCache { //Path to cache folder (with trailing /) var $cache_path = 'cache/'; //Length of time to cache a file in seconds var $cache_time = 3600; //This is just a functionality wrapper function function get_data($label, $url) { if($data = $this->get_cache($label)){ return $data; } else { $data = $this->do_curl($url); $this->set_cache($label, $data); return $data; } } function set_cache($label, $data) { file_put_contents($this->cache_path . $this->safe_filename($label) .'.cache', $data); } function get_cache($label) { $filename = $this->cache_path . $this->safe_filename($label) .'.cache'; if(file_exists($filename) && (filemtime($filename) + $this->cache_time >= time())) { return file_get_contents($filename); } return false; } function is_cached($label) { $filename = $this->cache_path . $this->safe_filename($label) .'.cache'; if(file_exists($filename) && (filemtime($filename) + $this->cache_time >= time())) { return true; } return false; } //Helper function for retrieving data from url function do_curl($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1); $content = curl_exec($ch); curl_close($ch); return $content; } //Helper function to validate filenames function safe_filename($filename) { return preg_replace('/[^0-9a-z._-]/i','', strtolower($filename)); } } ?>

The main difference with the old code is that there are now checks in place to make sure the label you give the cache can be a valid filename and that there is now a wrapper method get_data() which does all the hard work for you.

How do I use it?

Well let’s use an example. Say I wanted to get my latest tweet from twitter using twitter search. This is how you would do it:

require('simpleCache.php'); $cache = new SimpleCache(); $latest_tweet = $cache->get_data('tweet', 'http://search.twitter.com/search.atom?q=from:gilbitron&rpp=1'); echo $latest_tweet;

Thats it. Simple yeah? The label can be anything you like (we used “tweet” in this example”) and will create the file “cache/tweet.cache” on your server. All the cache stuff is done automatically and you don’t have to worry about it.

You can also change the path to your cache folder and cache time by doing the following (default values shown):

require('simpleCache.php'); $cache = new SimpleCache(); $cache->cache_path = 'cache/'; //cache folder $cache->cache_time = 3600; //time in secs

There is also one more method that you can use called is_cache():

$cache->is_cached('tweet');

This just returns true if the given label is currently cached, and false if not.

Conclusion

You can see the demo from the original code to see what kind of difference some simple caching can do for your load times. Let me know if you find this useful and where you use it.

This class is now on GitHub at: http://github.com/gilbitron/PHP-SimpleCache. Fork Away.