Quick PHP Page Cache Script

This is a free script written in PHP to create a local Page Cache system for your web application.

There may be times that you have a wide collection of objects that need to be accessed several times throughout your page load – in essence they need to be cached.  For complexity reasons these are objects that you cannot easily specify in advance – such as unknown record names coming from a wide array fo database tables.

Normally in this type of situation you may want to look at solutions such as Memcache for providing a cache layer in front of your Database.  Or if you wanted closer cache you could look at something such as APC to be installed on your web server.  However these use cases still don’t cover the scenario where an object may need to be accessed multiple times and even changed along the code path as it is being accessed.  For this type of use case a Page Cache system may make sense.

Please keep in mind that the Page Cache script is not intended to replace Memcache or APC style caching – rather this Page Cache script is designed to provide an alternative style caching as appropriate.  This script is not intended to be used with a very large number of objects as this script ensures that the object gets held in memory for the life of the script.  if you put thousands of large objects into memory – you will most likely crash that script and/or slow the script down.

A good use case for this script is pulling a collection of several records out of different tables of a database – or even pulling a collection of records out of Memcache or APC.  Instead of having to make multiple calls to the Memcache or Database layer – you can temporarily store the objects in the page cache.  If you are manipulating these objects then you may not want to store them in APC as all other scripts on that server will get the manipulated objects as well (there may be times you DO want that – but it depends on your use case).

Another use case for this script is the example of when you may make multiple saves to an object and to limit the number of times you do it per page load.  Let’s assume you have a Database record that may be recording 20 different save actions through the life of your script.  You can use this class to save and retrieve the object throughout the script and save it at the very end.  This also allows a very easy back-out procedure since if you hit a worrisome error on save number 15 out of 20, you could choose to either force the save command to the data store layer at that point or kill the script and abandon all of the previous 15 actions if they may then be considered suspect.  Again it really just depends on your use case.

I encourage your thoughts and comments on this script and it’s use in the comments section below.

Enjoy!

<?

/**
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT
 * HOLDERS MAKE NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY OR
 * FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF THE SOFTWARE
 * OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY PATENTS,
 * COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.COPYRIGHT HOLDERS WILL NOT
 * BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL OR CONSEQUENTIAL
 * DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE OR DOCUMENTATION.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://gnu.org/licenses/>.
 *
 * URL: http://www.benhallbenhall.com
 */

class CommonCache{

	public static $cachedObjects=array();
	public static $enableErrorLogs=true;

	/*
	 * Retrieves an object from the page cache for re-use later in the script.
	 *
	 * @param	string $id	A unique id to designate the object
	 * @param	string $type	A grouping mechanism to provide multiple realms for unique ID's.
	 *
	 * @return 	object The cached object, false if object not found
	 */

	public static function getObject($id, $type=''){
		if(array_key_exists($type,self::$cachedObjects) && array_key_exists($id,self::$cachedObjects[$type])){
			self::l("Common Object retrieved from Cache: Type(".$type.") ID(".$id.")");
			return self::$cachedObjects[$name];
		}

		return false;
	}

	/*
	 * Saves an object into the page cache for re-use later in the script
	 *
	 * @param 	string $id		A unique id to designate the object
	 * @param	string $type		A grouping mechanism to provide multiple realms for unique ID's.
	 * @param 	mixed $object	 	The object to store into the page cache
	 * @param	boolean $override	If true will save the passed value into the page cache regardless if it already
	 *
	 * @return false on error
	 */
	public static function setObject($id, $type, $object, $override=true){
		if(!$override){
			if(isset(self::$cachedObjects[$type][$id])){
				//Cache object exists - don't override it...
				self::l("Cached object exists - skipping the overwrite: Type(".$type.") ID(".$id.")");
				return false;	//Remove this line if you don't want it to error out on an override based skip
			}
		}
		self::$cachedObjects[$type][$id]=$object;
	}

	/*
	 * An error log function for this class
	 *
	 * @param	string $msg		The message to send to the error log
	 *
	 */
	public static function l($msg){
		if(self::$enableErrorLogs){
			Logger::debug($msg);
		}
	}

}
Posted in , and tagged , .

Leave a Reply

Your email address will not be published. Required fields are marked *