Tuesday, June 19th, 2007

AppCache for PHP5

Speed up your apps and have a convenient application-level scope with AppCache by Chris Dary, the seventh tool from the arc90 lab.


What is this?

AppCache is an implementation of an Application scope in PHP. If you’ve ever worked with Coldfusion (or ASP), you’ll know that Application-level variables are visible to an entire group of files. They’re one level above a session scope, and one level below server level variables.

As you can probably guess, this is hugely useful for all sorts of things – things like caching data, common variables, etc.

To do this, AppCache expands upon a fantastic caching system called memcached. Memcached’s intent is to speed up web applications by alleviating database load – it was originally created to enhance the speed of LiveJournal.com.

As a caveat, if you have not used an Application scope or a caching mechanism before: This data is not persistent – it’s stored in RAM, not on disk, and will be lost if the daemon is restarted or the cache expires, and therefore you should only use it for run-time stuff, which can be recreated or retrieved every time the app starts.

Why is this better than using Memcache directly?

A few reasons:

  1. In your code, usage is very clean – as simple as $app->var;
  2. Unlike Memcache itself, you have access to the list of variables defined within your app – $app->getKeys(); returns an array of all the currently defined variables. Use isset($app->var) to determine if a variable is set.
  3. No namespace worries. A key prefix is created from whatever you name the app – so you don’t have to worry about collisions with other memcache keys.
  4. The Memcache backend is abstracted – so if you end up implementing a different backend (database, shared memory, etc), this should be very simple.

Examples

Here are a few examples of what AppCache looks like in action:

A Simple Example
<?php
require 'Application.php';
// Initialize our application
$app = new Application('Sample Application Name');
// Check if our sample value is set
if( !isset( $app->sampleValue ) )
{
// If it is not set, initialize it, with the date, so that we can
// see that it is persistent amongst requests.
$app->sampleValue = "This sample value was set on " . date('r');
}
// Print our sample value
print_r($app->sampleValue);
// To remove sampleValue from our app, we would use:
// unset($app->sampleValue);
// To flush all values from the app, we would use:
// $app->clear();
// To access $app within functions and other classes, use either
// $GLOBALS['app'] or global $app;, then $app.
?>
A More Complex Example
<?php
require 'Application.php';
// Here, we pass our Application constructor an array of values for configuration.
$app = new Application(
array(
'key'				=>	'A complex Application example',
'memcached_servers'	=>	array('127.0.0.1:11211','anotherHost.com:22122'),
'memcached_prefix'	=>	'appvar_'
)
);
// Set up a cached_page variable that will expire.
if( !isset( $app->cached_page ) )
{
$page_content = array();
$page_content['header']	= '<html><head>' .
'<title>Page cached on ' . date('r') . '</title>' .
'</head><body>' .
'<h1>This is a cached header</h1>';
$page_content['body']	= '<p>This is some cached body content ' .
'with a random number: ' . rand() . '</p>';
$page_content['footer']	= '<h5>This is a cached footer</h5>' .
'</body></html>';
// This content will expire and be regenerated every 15 seconds
$app->setWithExpire('cached_page', $page_content, 15);
}
echo $app->cached_page['header'];
echo '<h4>The date is ' . date('r') . '. This is not cached.</h4>';
echo $app->cached_page['body'];
echo $app->cached_page['footer'];
// To see a list of all keys in our app,
// and their expiration dates (if they have one), use:
// print_r($app->getKeys());
// To remove cached_page from our app, we would use:
// unset($app->cached_page);
// To flush all values from the app, we would use:
// $app->clear();
?>

Download The Code

To use AppCache on your site, download it from one of the following sources:

Source File with Examples

This is a zip file with Application.php as well as the two examples above.

Download

Raw Source File

This is just Application.php as a PHP source file.

Download

Prerequisites

To run AppCache, you need the following:

  • PHP 5. AppCache has been tested with PHP 5.2.0, but it will likely work with other versions of PHP 5 as well.
  • An instance of memcached running somewhere. You can download it at http://www.danga.com/memcached.
  • The PHP library for memcache – installed easily with PECL. In many cases it’s as simple as running pecl install memcache. More information is available at http://us2.php.net/memcache.

Licensing

This arc90 tool is licensed under the Creative Commons Attribution 3.0 license.

Discuss AppCache

You can send feedback on AppCache at the arc90 blog.

Comments are closed.