The arc90 lab is the playground of arc90 - a New York-based technology and strategic consulting firm. The lab is a place for us to share our ideas, tools and the occasional experiment in web technology. All ideas, experiments and tools are licensed under Creative Commons.

licensed under creative commons

This site is licensed under a Creative Commons Attribution 2.5 License.

Tools

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
A More Complex Example

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.

Digg Del.icio.us