phpCA client

Justin jmat@shutdown.net
Wed, 31 Dec 2003 16:36:18 -0500


Yeah yeah yeah... But I don't see myself ever wanting to do that. 

Here's another solution; these are regular functions, not enclosed in a
class.  Use get_multi.  I forgot I had written this a few weeks ago,
removing that !FALSE!/!TRUE! code.  I think the API should ultimately return
a NULL if the value doesn't exist, but this is a good way around it until we
can all finalize on a single API that works.  :)

----

include_once("memcached-client.php");

// logger() is a custom logging function, it just writes to
/web/logs/photozero.log

function memcache_init()
{
	global $memc;
	$params = array('servers' => array('10.1.2.1:11223'),
						'debug'   => false,
						'compress_threshold' =>
2048,
						'persistant' => true);
	if (!$memc = new memcached($params)) {
		trigger_error("Unable to initialize
memcached!",E_USER_ERROR);
	}
	return true;
}

function memcache_set($key,$value,$expires=3600)
{
 	global $memc;
 	if (!isset($memc)) memcache_init();
	$return = $memc->set($key,$value,$expires);
	if (!$return) trigger_error("memcache_set: unable to set $key to
$value",E_USER_WARNING);
	if (DEBUG) logger("memcache_set: set $key ($expires) to
\"$value\"",7);
	return $return;
}

function memcache_get($key)
{
	global $memc;
	if (!isset($memc)) memcache_init();
	if (is_array($key))
	{
		$return = $memc->get_multi($key);
	}
	else
	{
		$return = $memc->get_multi(array($key));
		if (!isset($return[$key]))
		{
			logger("memcache_get: $key failed: key does not
exist!",7);
			return NULL;
		}
		$return = $return[$key];
	}
	if (DEBUG) logger("memcache_get: retrieved $key: \"$return\"",7);
	return $return;
}