phpCA client

Justin jmat@shutdown.net
Wed, 31 Dec 2003 15:44:04 -0500


I agree on the NULL idea -- look back in the archives, and we had a quick
convo about it.   The plan was to have the new API return a PEAR error
object when the value doesn't exist; but I don't know if that's still going
to happen or not. I think the other guys working on the PHP API have run out
of time to work on it... I'm completely swamped, or I would have already
patched it up.  

I wrote a wrapper around the memcached API that actually translates TRUE and
FALSE into the words "!TRUE!" and "!FALSE!" before sending them out.  When
it retrieves, it returns a NULL if it gets a FALSE, or a FALSE if it gets a
!FALSE!. 

It's not pretty, but it works; and I can control what gets returned to my
programs, no matter what the API returns.

It's a very good idea to write a wrapper around the API anyway, since it
makes it 100x easier to switch between different API's (plus you don't have
to make the memcache object global) I have functions called "mc_get" and
"mc_set", etc., so they can be called anywhere without having to globalize
things.  As long as you're using one of the many optimizers and caching
engines, there's no speed drawback to doing this.

Finally... The 0.1.2 API has some weird problems; it will sometimes freak
out and not be able to "parse response from server".  Make sure you go to
line 840 and change that printf to a trigger_error, or you're suddenly going
to get a ton of error messages thrown out to your webpages.  Russell Garrett
had a patch that fixed all of the problems, but he yanked it... I talked to
him a few days ago, and he's made several more changes, and he said he'd
release the patch soon...

-----Original Message-----
From: memcached-admin@lists.danga.com
[mailto:memcached-admin@lists.danga.com] On Behalf Of Tim Yardley
Sent: Tuesday, December 30, 2003 8:20 PM
To: memcached@lists.danga.com
Subject: phpCA client

First off, congratulations guys on making this into a nicely extensible
system.

I was looking at memcached-client-php-0.1.2.tar.gz, particularly at the get
function of said release.  It seems like instead of return'ing false in the
case that an object is not found, it would make sense to instead return the
php NULL constant.  The desire after fetching something from the cache is to
determine if it was returned by the cache, not to check its value.  So, with
that in mind... Here are the ways we can check that.

Currently returning FALSE when an item doesn't exist gives you the
following:

<?
$memcache->add('false', FALSE);
$memcache->add('true', TRUE);

// object is defined... So we want it to say yes on the first one
$foo = $memcache->get('false');

// this one prints
if (isset($foo)) {
  echo 'got it';
}

// this one does not print
if ($foo) {
  echo 'got it';
}

// this one does not print
if (isset($foo) && $foo) {
  echo 'got it';
}
unset($foo);

// object is defined and true... So we want it to say yes here.
$foo = $memcache->get('true');

// this one prints
if (isset($foo)) {
  echo 'got it';
}

// this one prints
if ($foo) {
  echo 'got it';
}

// this one prints
if (isset($foo) && $foo) {
  echo 'got it';
}
unset($foo);

// these should all fail
$foo = $memcache->get('idontexist');

// this one prints, incorrectly
if (isset($foo)) {
  echo 'got it';
}

// this one does not print
if ($foo) {
  echo 'got it';
}

// this one does not print
if (isset($foo) && $foo) {
  echo 'got it';
}
unset($foo);

?>

Returning NULL when it doesn't exist will give you the following:
<?
$memcache->add('false', FALSE);
$memcache->add('true', TRUE);

// object is defined and false... So we want it to say yes to the first one.
$foo = $memcache->get('false');

// this one prints
if (isset($foo)) {
  echo 'got it';
}

// this one does not print
if ($foo) {
  echo 'got it';
}

// this one does not print
if (isset($foo) && $foo) {
  echo 'got it';
}
unset($foo);

// object is defined and true.. So we want it to say yes to all these here.
$foo = $memcache->get('true');

// this one prints
if (isset($foo)) {
  echo 'got it';
}

// this one prints
if ($foo) {
  echo 'got it';
}

// this one prints
if (isset($foo) && $foo) {
  echo 'got it';
}
unset($foo);

// these should all fail
$foo = $memcache->get('idontexist');

// this one does not print
if (isset($foo)) {
  echo 'got it';
}

// this one does not print
if ($foo) {
  echo 'got it';
}

// this one does not print
if (isset($foo) && $foo) {
  echo 'got it';
}
unset($foo);

?>

Basically, it boils down to the fact that we want to check if the object was
returned and not check it's value... So that means that the proper way to
check that would be isset($foo), but... That will fail in the case that it
is not defined.  This may affect more than just the PHP client, thoughts?
Hopefully I didn't mistype my test cases as I wrote this.

Did you follow me on that?

/tmy