This was a simple home-grown caching system for queries but it can really be used to store anything in cache. This will reduce server load on common queries, etc. In the code below, you will notice references to the following classes/properties, they are explained below what they hold (this class is integrated into a much larger entity).
$controller_config->cacheDir = This is the writeable directory path where the cache files are stored.
$controller_config->useCache = This is a boolean true/false as to whether the site uses cache or not. This is helpful during new development or troubleshooting.
$controller_config->cacheLifetime = This is the lifespan of the cache file (in seconds)
The way I use this is that I have to generate a unique "file id" that uniquely identifies the request. This is usually the classnamemethodName plus a hash of all the supplied arguments. For this example I'll keep it simple and show how I integrated the cache system with the event calendar.
The method that generates the month hash for the event calendar (entire month, days, events, etc) is named "viewCalendar_month" and accepts the arguments $month and $year; therefor:
$cache = new cache();
$fileID = "eventCal_monthHash_" . $month . "-" . $year;
if ($cache->hasCache($fileID) === true) {
$hash = $cache->getCache($fileID, true);
}
else {
$hash = $this->generateCalendarHash($month, $year, true);
$cache->writeCache($fileID, $hash, true);
}
It checks for a valid cache file with the supplied "file id". If it exists, it just grabs the contents of that file, if a valid cache file does not exist, it just re-generates the data as if the cache system never existed and then creates a new (or updated) cache file for next time.
The getCache and writeCache methods have a boolean argument of $serialized. This tells the cache system if the data needs to be serialized or unserialized before writing/reading the data (respectively). This is handy for storing arrays or objects to cache. An example where you would not need the data serialized is if you were storing a pre-JSON encoded result.
On a side note, I have my personal system set to let the cache files be valid for 24 hours (data doesn't change often). I do have a separate cleanup job that runs once a week and cleans out any .txt files in the cache directory that are older than 24 hours.
<?php /** * This is the cache management class * @package Skyward_Landing_Page * @subpackage Cache * @filesource * @author Matt Ford * @version 1.0 */ //if (!defined("parentFile")) {die("Direct Access Not Allowed");} else {} class cache { /** * This method determines if the supplied file id has a valid cached version or not * @access public * @author Matt Ford * @param string $fileID the ID of the cache file * @return boolean true false result */ public function hasCache($fileID) { global $controller_config; $file = $controller_config->cacheDir . $fileID . ".txt"; $diff = ($ctime - $mtime); if ($diff >= $controller_config->cacheLifetime) { //syslog(LOG_INFO, "Cache for this data ($fileID) is expired"); return false; } else { //syslog(LOG_INFO, "This data ($fileID) is cached"); return true; } } else { //syslog(LOG_INFO, "Cache does not contain this data ($fileID)"); return false; } } /** * This method deletes any existing cache file for the supplied fileID and writes a new cache file * @access public * @author Matt Ford * @param string $fileID the ID of the cache file * @param mixed $data string, array, object, whatever the data is * @param boolean $serialized whether or not to serialize the data before writing to cache file */ public function writeCache($fileID, $data, $serialized = false) { global $controller_config; if ($controller_config->useCache === true) { $file = $controller_config->cacheDir . $fileID . ".txt"; //syslog(LOG_INFO, "Writing cache file $fileID"); @file_put_contents($file, $data); } else {} } /** * This method retrieves the data from the cache file * @access public * @author Matt Ford * @param string $fileID the ID of the cache file * @param boolean $serialized whether or not to unserialize the data before before returning it * @return mixed string, array, object, whatever the data is */ public function getCache($fileID, $serialized = false) { global $controller_config; $file = $controller_config->cacheDir . $fileID . ".txt"; //syslog(LOG_INFO, "Cache utilized and retrieved for $fileID"); return $data; } else { //syslog(LOG_INFO, "Cache does not contain this data ($fileID)"); return ""; } } } ?>
Comments
Subscribe to comments
You need to login to post a comment.

Hello,
You can also use the Zend Framework : http://framework.zend.com/manual/fr/zend.cache.html
A+