Update memcached client to 1.0.10
authorBrion Vibber <brion@users.mediawiki.org>
Thu, 21 Aug 2003 07:03:10 +0000 (07:03 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Thu, 21 Aug 2003 07:03:10 +0000 (07:03 +0000)
docs/memcached.doc
docs/php-memcached/ChangeLog
docs/php-memcached/Documentation
includes/MemCachedClient.inc.php

index c79865d..85a74a0 100644 (file)
@@ -72,7 +72,7 @@ usage evenly), make its entry a subarray:
 
 == PHP client for memcached ==
 
-As of this writing, MediaWiki includes version 1.0.8 of the PHP
+As of this writing, MediaWiki includes version 1.0.10 of the PHP
 memcached client by Ryan Gilfether <hotrodder@rocketmail.com>.
 You'll find some documentation for it in the 'php-memcached'
 subdirectory under the present one.
@@ -88,15 +88,23 @@ disable itself fairly smoothly.
 == Keys used ==
 
 User:
-       key: $wgDBname:user:user_id:$sId
-       ex: wikidb:user:user_id:51
+       key: $wgDBname:user:id:$sId
+       ex: wikidb:user:id:51
        stores: instance of class User
        set in: User::loadFromSession()
-       cleared by: User::saveSettings()
+       cleared by: User::saveSettings(), UserTalkUpdate::doUpdate()
+       
+Newtalk:
+       key: $wgDBname:newtalk:ip:$ip
+       ex: wikidb:newtalk:ip:123.45.67.89
+       stores: integer, 0 or 1
+       set in: User::loadFromDatabase()
+       cleared by: User::saveSettings() # ?
+       expiry set to 30 minutes
 
 LinkCache:
-       key: $wgDBname:linkcache:title:$title
-       ex: wikidb:linkcache:title:Wikipedia:Welcome,_Newcomers!
+       key: $wgDBname:lc:title:$title
+       ex: wikidb:lc:title:Wikipedia:Welcome,_Newcomers!
        stores: cur_id of page, or 0 if page does not exist
        set in: LinkCache::addLink()
        cleared by: LinkCache::clearBadLink()
index bfabb49..86792f6 100644 (file)
@@ -1,3 +1,12 @@
+Release 1.0.10
+--------------
+* bug fix: changes hashing function to crc32, sprintf %u
+* feature: optional compression
+
+Release 1.0.9
+-------------
+* protocol parsing bug
+
 Release 1.0.8
 -------------
 * whitespace/punctuation/wording cleanups
index 0d09d17..4782807 100644 (file)
@@ -158,7 +158,8 @@ MC_ERR_LOADITEM_END         // _load_items failed to receive END response
 MC_ERR_LOADITEM_BYTES  // _load_items bytes read larger than bytes available
 MC_ERR_GET                             // failed to get value associated with key
 
-
+// Turns compression on or off; 0=off, 1=on
+MemCacheClient::set_compression($setting)
 
 EXAMPLE:
 <?php
index e77e8f5..a737e5d 100644 (file)
@@ -19,7 +19,7 @@
 /**
  * version string
  */
-define("MC_VERSION", "1.0.9");
+define("MC_VERSION", "1.0.10");
 /**
  * int, buffer size used for sending and receiving
  * data from sockets
@@ -51,7 +51,7 @@ define("MC_ERR_LOADITEM_BYTES", 1012);        // _load_items bytes read larger than byt
  * @author Ryan Gilfether <ryan@gilfether.com>
  * @package MemCachedClient
  * @access public
- * @version 1.0.7
+ * @version 1.0.10
  */
 class MemCachedClient
 {
@@ -90,6 +90,22 @@ class MemCachedClient
      * @var string
      */
     var $errstr;
+    /**
+     * size of val to force compression; 0 turns off; defaults 1
+     * @ var int
+     */
+    var $compress = 1;
+    /**
+     * temp flag to turn compression on/off; defaults on
+     * @ var int
+     */
+    var $comp_active = 1;
+
+    /**
+     * array that contains parsed out buckets
+     * @ var array
+     */
+    var $bucket;
 
 
     /**
@@ -116,6 +132,7 @@ class MemCachedClient
                {
                        $this->set_servers($options["servers"]);
                        $this->debug = $options["debug"];
+                       $this->compress = $options["compress"];
                        $this->cache_sock = array();
                }
 
@@ -550,6 +567,23 @@ class MemCachedClient
        }
 
 
+       /**
+       *       temporarily sets compression on or off
+       *       turning it off, and then back on will result in the compression threshold going
+       *       back to the original setting from $options
+       *       @param int $setting setting of compression (0=off|1=on)
+       */
+
+       function set_compression($setting=1) {
+               if ($setting != 0) {
+                       $this->comp_active = 1;
+               } else {
+                       $this->comp_active = 0;
+               }
+       }
+
+
+
        /*
         * PRIVATE FUNCTIONS
         */
@@ -637,8 +671,6 @@ class MemCachedClient
         */
        function get_sock($key)
        {
-               $buckets = 0;
-
                if(!$this->active)
                {
                        $this->errno = MC_ERR_NOT_ACTIVE;
@@ -652,9 +684,9 @@ class MemCachedClient
 
                $hv = is_array($key) ? intval($key[0]) : $this->_hashfunc($key);
 
-               if(!$buckets)
+               if(!$this->buckets)
                {
-                       $bu = $buckets = array();
+                       $bu = $this->buckets = array();
 
                        foreach($this->servers as $v)
                        {
@@ -667,14 +699,14 @@ class MemCachedClient
                                        $bu[] = $v;
                        }
 
-                       $buckets = $bu;
+                       $this->buckets = $bu;
                }
 
                $real_key = is_array($key) ? $key[1] : $key;
                $tries = 0;
                while($tries < 20)
                {
-                       $host = @$buckets[$hv % count($buckets)];
+                       $host = @$this->buckets[$hv % count($this->buckets)];
                        $sock = $this->sock_to_host($host);
 
                        if(is_resource($sock))
@@ -783,7 +815,6 @@ class MemCachedClient
                return trim($retval);
        }
 
-
        /**
         * sends the command to the server
         * Possible errors set are:
@@ -837,6 +868,17 @@ class MemCachedClient
                        $flags |= 1;
                }
 
+               if (($this->compress_active) && ($this->compress > 0) && (strlen($val) > $this->compress)) {
+                       $this->_debug("_set(): compressing data. size in:".strlen($val));
+                       $cval=gzcompress($val);
+                       $this->_debug("_set(): done compressing data. size out:".strlen($cval));
+                       if ((strlen($cval) < strlen($val)) && (strlen($val) - strlen($cval) > 2048)){
+                               $flags |= 2;
+                               $val=$cval;
+                       }
+                       unset($cval);
+               }
+
                $len = strlen($val);
                if (!is_int($exptime))
                        $exptime = 0;
@@ -1034,6 +1076,8 @@ class MemCachedClient
 
                                                if(strlen($val[$sk]) != $len_array[$sk])
                                                        continue;
+                                               if(@$flags_array[$sk] & 2)
+                                                       $val[$sk] = gzuncompress($val[$sk]);
 
                                                if(@$flags_array[$sk] & 1)
                                                        $val[$sk] = unserialize($val[$sk]);
@@ -1078,12 +1122,7 @@ class MemCachedClient
         */
        function _hashfunc($num)
        {
-               $hash = 0;
-
-               foreach(preg_split('//', $num, -1, PREG_SPLIT_NO_EMPTY) as $v)
-               {
-                       $hash = $hash * 33 + ord($v);
-               }
+               $hash = sprintf("%u",crc32($num));
 
                return $hash;
        }