xxcache
xxcache is a light framework for caching data in PHP. It is not aimed to be as advanced as most of the other php caches out there. Rather, it is designed to be a very simple pluggable cache framework. It allows different types of caches to all be accessed through the same abstract interface – so a script can use standard get and set methods to save and load data without knowing anything about how the underlying cache mechanism chosen actually works.
Usage
The main file to include is xxcache.php. This file contains the main factory getter function, GetXXCache($type). $type can either be a constant that is defined in xxcache.constants.php, or a string representing the type. The type constants and their string equivalents are:
XXCACHE_MEMCACHE, or “memcache”: The Memcache cache handler (requires php memcache extension)
XXCACHE_EACCELERATOR, or “eaccelerator”: The Eaccelerator cache handler
XXCACHE_FILECACHE, or “filecache”: The file-based disk cache handler
XXCACHE_NULL, or any other unknown string: The null cache
XXCACHE_AUTO: Autoselect
Autoselect will test to see what your system supports. If your system supports memcache then it will return a memcache handler, otherwise it will check for support for the eaccelerator cache, otherwise it will fall back to the filecache. Be careful with this though; it just checks for the existence of the memcache functions or the eaccelerator functions. If you have the memcache PHP extension installed it will return a memcache object, even though you may not actually have any memcache servers.
All cache handlers have the following methods standard:
GetCacheHits() – Returns the number of cache hits since instantiation.
GetCacheMisses() – Returns the number of cache misses since instantiation.
GetCacheType() – Returns the cache type as one of the constants defined in xxcache.constants.php.
GetCacheTypeString() – Returns the cache type as a normal human-readable string.
Open() – Set up the cache for use. This should always be called before attempting to get or set anything. Some caches do stuff in here (for example, memcache opens the connection to the server), some don’t, so to be safe it should always be called first.
Close() – Close the cache. Should always be called when you’re done using the cache, for the same reasons as Open().
Get($key) – Get cached item with string key $key. Returns null if item isn’t found.
Set($key, $val) – Set the given string key $key to contain the value $val.
Del($key) – Delete the cached item with string key $key.
Clear() – Clear out all cached data
Stats() – Returns an array containing some cache statistics. What statistics you get depends on the cache handler being used.
Although in general caches are transparent, certain caches may require calling special methods to set up before calling Open() if you require changing a setting from the default. These parameter setup methods are:
Filecache:
GetCacheDir()/SetCacheDir($dir) – These get/set the directory cache files will be stored in. $dir should be a string that ends with a directory separator (/). Default cache dir is “cache/”.
GetCacheExt()/SetCacheExt($ext) – These get/set the extension cache files will have. $ext should be a string that begins with a period “.”. Default cache extension is “.cache”.
Eaccelerator:
GetNamespace()/SetNamespace($ns) – These get/set the namespace that is prepended to all string keys stored in eaccelerator. The default namespace is “xxcache_”.
Memcache:
GetNamespace()/SetNamespace($ns) – These get/set the namespace that is prepended to all string keys stored in memcache. The default namespace is “xxcache_”. Note: as memcache is not necessarily a local service, namespaces are also automatically prepended with the server hostname when set. So the real internal namespace used is “hostname_xxcache_”, and calling SetNamespace(“something_”) will make the full internal namespace “hostname_something_”.
GetAddress()/SetAddress($addr) – These get/set the address of the memcache server as a string. If you need to change this from the default, make sure you do so before calling Open() to connect. Default is “127.0.0.1″.
GetPort()/SetPort($po) – These get/set the port of the memcache server, as a number. If you need to change this from the default, make sure you do so before calling Open() to connect. Default is 11211.
GetPersist()/SetPersist($p) – These get/set whether to use a persistent connection to the memcache server, as a boolean. If you want to change this from the default, make sure you do so before calling Open() to connect. Default is false.
GetCompressMin()/SetCompressMin($min) – These get/set the minimum compression size. Default is 20000.
GetCompressRatio()/SetCompressRatio($ratio) – These get/set the compression ratio. Default is 0.2.
Typical usage is as follows (obviously replacing XXCACHE_MEMCACHE with your chosen type):
include_once('xxcache/xxcache.php');
$cache = GetXXCache(XXCACHE_MEMCACHE);
if ($cache->GetCacheType() === XXCACHE_MEMCACHE) {
// Set up any address/port/dir settings here, before Open()
}
$cache->Open();
$data = $cache->Get("my_data_key");
if (!$data) {
$data = get_my_database_data()
$cache->Set("my_data_key", $data);
}
$output = $cache->Get("my_output_key");
if (!$output) {
$output = format_data_into_html($data);
$cache->Set("my_output_key", $output);
}
echo $output;
$cache->Close();
You get the idea.
The null cache is a special type of cache that can be used if you want to disable caching. It always returns null and doesn’t store anything. This provides an easy way to disable caching without having to rewrite your code or litter your code with extra checks to see if the $cache object exists.
The file that contains the abstract class definition, xxcache.cachebase.php, is commented to fully explain the interface, in case you want to write your own cache handler. You should be able to just write a new cache handler, add a constant for it in xxcache.constants.php, add support for it in the getter in xxcache.php, and you should be good to go. If you write a new cache handler you like feel free to send it to me and I’ll include it with the main release.
Requirements
To define the abstract interface and have the cache handlers inherit from it, I required more fully featured object oriented programming features. Therefore, xxcache requires PHP5 to run.
Changelog
- 20081024 – 0.0.1
- Initial release
Download