PHP API 1.0.9
Justin Matlock
jmat@shutdown.net
Wed, 13 Aug 2003 20:44:24 -0400
This is a multi-part message in MIME format.
------=_NextPart_000_00A0_01C361DB.B4352620
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
I ran into a few more tiny problems in the PHP API. I couldn't figure out
why my server kept trying to open connections to "-1". :)
It was rebuilding the server bucket every single time it tried to make a
connection -- it didn't store the list of servers between calls. This, of
course, wasted some CPU cycles.
The hash function was returning odd values -- sometimes it would send back
negative numbers, which would cause get_sock to try to assign a negative
bucket position. So what was happening is about 1/2 the time, it was trying
to open a socket to "", and failing. I added some more debugging lines (I
pulled them back out), which showed what was happening:
[03Aug13 200514] (DBUG) <10.1.0.11> (u:1) get_sock(): hash
2.5527987333199E+15
[03Aug13 200514] (DBUG) <10.1.0.11> (u:1) host = buckets[2.5527987333199E+15
% 4]
[03Aug13 200514] (DBUG) <10.1.0.11> (u:1) bucket chosen = $this->bucket[-2]
[03Aug13 200514] (DBUG) <10.1.0.11> (u:1) HEY! that bucket doesn't exist!
This is going to fail!
[03Aug13 200514] (DBUG) <10.1.0.11> (u:1) get_sock(): calling sock_to_host
with ''
[03Aug13 200514] (DBUG) <10.1.0.11> (u:1) sock_to_host(): trying host ''
[03Aug13 200514] (DBUG) <10.1.0.11> (u:1) sock_to_host(): Host address was
not in the format of host:port
Don't ask me why the results of modulus hash was -2... I skipped the part
in math class where they taught scientific notation arithmetic. :)
I replaced the hash function with a simpler CRC32 (that I could
understood -- plus pregs in PHP aren't all that fast), passing it through
sprint("%u") to force it to always be positive.
This also seems to make the host selection much more evenly spread out among
the servers.
I also added the minimum compression piece and the set_compression(1|0)
component.
Ryan; check out these changes and see if they make sense to you.
And Brad... I think I've got this diff thing down. ;-)
J
----- Original Message -----
From: "Brad Fitzpatrick" <brad@danga.com>
To: <memcached@lists.danga.com>
Sent: Tuesday, August 12, 2003 8:04 PM
Subject: PHP API 1.0.9
> New PHP release is up, with the fix for the protocol parsing bug.
>
> http://www.danga.com/memcached/dist/php-memcached-1.0.9.tar.gz
>
> - Brad
>
>
------=_NextPart_000_00A0_01C361DB.B4352620
Content-Type: application/octet-stream;
name="php-memcached-1.0.9c.patch"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
filename="php-memcached-1.0.9c.patch"
diff -urN php-memcached-1.0.9/Documentation =
php-memcached-1.0.9c/Documentation=0A=
--- php-memcached-1.0.9/Documentation Tue Aug 12 20:09:53 2003=0A=
+++ php-memcached-1.0.9c/Documentation Wed Aug 13 20:38:00 2003=0A=
@@ -158,7 +158,8 @@=0A=
MC_ERR_LOADITEM_BYTES // _load_items bytes read larger than bytes =
available=0A=
MC_ERR_GET // failed to get value associated with key=0A=
=0A=
-=0A=
+// Turns compression on or off; 0=3Doff, 1=3Don=0A=
+MemCacheClient::set_compression($setting)=0A=
=0A=
EXAMPLE:=0A=
<?php=0A=
diff -urN php-memcached-1.0.9/MemCachedClient.inc.php =
php-memcached-1.0.9c/MemCachedClient.inc.php=0A=
--- php-memcached-1.0.9/MemCachedClient.inc.php Tue Aug 12 20:09:53 2003=0A=
+++ php-memcached-1.0.9c/MemCachedClient.inc.php Wed Aug 13 20:40:24 2003=0A=
@@ -90,6 +90,22 @@=0A=
* @var string=0A=
*/=0A=
var $errstr;=0A=
+ /**=0A=
+ * size of val to force compression; 0 turns off; defaults 1=0A=
+ * @ var int=0A=
+ */=0A=
+ var $compress =3D 1;=0A=
+ /**=0A=
+ * temp flag to turn compression on/off; defaults on=0A=
+ * @ var int=0A=
+ */=0A=
+ var $comp_active =3D 1;=0A=
+=0A=
+ /**=0A=
+ * array that contains parsed out buckets=0A=
+ * @ var array=0A=
+ */=0A=
+ var $bucket;=0A=
=0A=
=0A=
/**=0A=
@@ -116,6 +132,7 @@=0A=
{=0A=
$this->set_servers($options["servers"]);=0A=
$this->debug =3D $options["debug"];=0A=
+ $this->compress =3D $options["compress"];=0A=
$this->cache_sock =3D array();=0A=
}=0A=
=0A=
@@ -550,6 +567,23 @@=0A=
}=0A=
=0A=
=0A=
+ /**=0A=
+ * temporarily sets compression on or off=0A=
+ * turning it off, and then back on will result in the compression =
threshold going=0A=
+ * back to the original setting from $options=0A=
+ * @param int $setting setting of compression (0=3Doff|1=3Don)=0A=
+ */=0A=
+=0A=
+ function set_compression($setting=3D1) {=0A=
+ if ($setting !=3D 0) {=0A=
+ $this->comp_active =3D 1;=0A=
+ } else {=0A=
+ $this->comp_active =3D 0;=0A=
+ }=0A=
+ }=0A=
+=0A=
+=0A=
+=0A=
/*=0A=
* PRIVATE FUNCTIONS=0A=
*/=0A=
@@ -637,8 +671,6 @@=0A=
*/=0A=
function get_sock($key)=0A=
{=0A=
- $buckets =3D 0;=0A=
-=0A=
if(!$this->active)=0A=
{=0A=
$this->errno =3D MC_ERR_NOT_ACTIVE;=0A=
@@ -652,9 +684,9 @@=0A=
=0A=
$hv =3D is_array($key) ? intval($key[0]) : $this->_hashfunc($key);=0A=
=0A=
- if(!$buckets)=0A=
+ if(!$this->buckets)=0A=
{=0A=
- $bu =3D $buckets =3D array();=0A=
+ $bu =3D $this->buckets =3D array();=0A=
=0A=
foreach($this->servers as $v)=0A=
{=0A=
@@ -667,14 +699,14 @@=0A=
$bu[] =3D $v;=0A=
}=0A=
=0A=
- $buckets =3D $bu;=0A=
+ $this->buckets =3D $bu;=0A=
}=0A=
=0A=
$real_key =3D is_array($key) ? $key[1] : $key;=0A=
$tries =3D 0;=0A=
while($tries < 20)=0A=
{=0A=
- $host =3D @$buckets[$hv % count($buckets)];=0A=
+ $host =3D @$this->buckets[$hv % count($this->buckets)];=0A=
$sock =3D $this->sock_to_host($host);=0A=
=0A=
if(is_resource($sock))=0A=
@@ -783,7 +815,6 @@=0A=
return trim($retval);=0A=
}=0A=
=0A=
-=0A=
/**=0A=
* sends the command to the server=0A=
* Possible errors set are:=0A=
@@ -837,6 +868,17 @@=0A=
$flags |=3D 1;=0A=
}=0A=
=0A=
+ if (($this->compress_active) && ($this->compress > 0) && =
(strlen($val) > $this->compress)) {=0A=
+ $this->_debug("_set(): compressing data. size in:".strlen($val));=0A=
+ $cval=3Dgzcompress($val);=0A=
+ $this->_debug("_set(): done compressing data. size =
out:".strlen($cval));=0A=
+ if ((strlen($cval) < strlen($val)) && (strlen($val) - strlen($cval) =
> 2048)){=0A=
+ $flags |=3D 2;=0A=
+ $val=3D$cval;=0A=
+ }=0A=
+ unset($cval);=0A=
+ }=0A=
+=0A=
$len =3D strlen($val);=0A=
if (!is_int($exptime))=0A=
$exptime =3D 0;=0A=
@@ -1034,6 +1076,8 @@=0A=
=0A=
if(strlen($val[$sk]) !=3D $len_array[$sk])=0A=
continue;=0A=
+ if(@$flags_array[$sk] & 2)=0A=
+ $val[$sk] =3D gzuncompress($val[$sk]);=0A=
=0A=
if(@$flags_array[$sk] & 1)=0A=
$val[$sk] =3D unserialize($val[$sk]);=0A=
@@ -1078,12 +1122,7 @@=0A=
*/=0A=
function _hashfunc($num)=0A=
{=0A=
- $hash =3D 0;=0A=
-=0A=
- foreach(preg_split('//', $num, -1, PREG_SPLIT_NO_EMPTY) as $v)=0A=
- {=0A=
- $hash =3D $hash * 33 + ord($v);=0A=
- }=0A=
+ $hash =3D sprintf("%u",crc32($num));=0A=
=0A=
return $hash;=0A=
}=0A=
------=_NextPart_000_00A0_01C361DB.B4352620--