#3364: pt_br name typo
[mediawiki.git] / includes / memcached-client.php
blob4ff0da91ced1cae282aae8bbcb12e9902c7fa6a7
1 <?php
2 //
3 // +---------------------------------------------------------------------------+
4 // | memcached client, PHP |
5 // +---------------------------------------------------------------------------+
6 // | Copyright (c) 2003 Ryan T. Dean <rtdean@cytherianage.net> |
7 // | All rights reserved. |
8 // | |
9 // | Redistribution and use in source and binary forms, with or without |
10 // | modification, are permitted provided that the following conditions |
11 // | are met: |
12 // | |
13 // | 1. Redistributions of source code must retain the above copyright |
14 // | notice, this list of conditions and the following disclaimer. |
15 // | 2. Redistributions in binary form must reproduce the above copyright |
16 // | notice, this list of conditions and the following disclaimer in the |
17 // | documentation and/or other materials provided with the distribution. |
18 // | |
19 // | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
20 // | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
21 // | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
22 // | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
23 // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
24 // | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 // | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 // | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 // | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
28 // | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 // +---------------------------------------------------------------------------+
30 // | Author: Ryan T. Dean <rtdean@cytherianage.net> |
31 // | Heavily influenced by the Perl memcached client by Brad Fitzpatrick. |
32 // | Permission granted by Brad Fitzpatrick for relicense of ported Perl |
33 // | client logic under 2-clause BSD license. |
34 // +---------------------------------------------------------------------------+
36 // $TCAnet$
39 /**
40 * This is the PHP client for memcached - a distributed memory cache daemon.
41 * More information is available at http://www.danga.com/memcached/
43 * Usage example:
45 * require_once 'memcached.php';
47 * $mc = new memcached(array(
48 * 'servers' => array('127.0.0.1:10000',
49 * array('192.0.0.1:10010', 2),
50 * '127.0.0.1:10020'),
51 * 'debug' => false,
52 * 'compress_threshold' => 10240,
53 * 'persistant' => true));
55 * $mc->add('key', array('some', 'array'));
56 * $mc->replace('key', 'some random string');
57 * $val = $mc->get('key');
59 * @author Ryan T. Dean <rtdean@cytherianage.net>
60 * @package memcached-client
61 * @version 0.1.2
64 // {{{ requirements
65 // }}}
67 // {{{ constants
68 // {{{ flags
70 /**
71 * Flag: indicates data is serialized
73 define("MEMCACHE_SERIALIZED", 1<<0);
75 /**
76 * Flag: indicates data is compressed
78 define("MEMCACHE_COMPRESSED", 1<<1);
80 // }}}
82 /**
83 * Minimum savings to store data compressed
85 define("COMPRESSION_SAVINGS", 0.20);
87 // }}}
89 // {{{ class memcached
90 /**
91 * memcached client class implemented using (p)fsockopen()
93 * @author Ryan T. Dean <rtdean@cytherianage.net>
94 * @package memcached-client
96 class memcached
98 // {{{ properties
99 // {{{ public
102 * Command statistics
104 * @var array
105 * @access public
107 var $stats;
109 // }}}
110 // {{{ private
113 * Cached Sockets that are connected
115 * @var array
116 * @access private
118 var $_cache_sock;
121 * Current debug status; 0 - none to 9 - profiling
123 * @var boolean
124 * @access private
126 var $_debug;
129 * Dead hosts, assoc array, 'host'=>'unixtime when ok to check again'
131 * @var array
132 * @access private
134 var $_host_dead;
137 * Is compression available?
139 * @var boolean
140 * @access private
142 var $_have_zlib;
145 * Do we want to use compression?
147 * @var boolean
148 * @access private
150 var $_compress_enable;
153 * At how many bytes should we compress?
155 * @var interger
156 * @access private
158 var $_compress_threshold;
161 * Are we using persistant links?
163 * @var boolean
164 * @access private
166 var $_persistant;
169 * If only using one server; contains ip:port to connect to
171 * @var string
172 * @access private
174 var $_single_sock;
177 * Array containing ip:port or array(ip:port, weight)
179 * @var array
180 * @access private
182 var $_servers;
185 * Our bit buckets
187 * @var array
188 * @access private
190 var $_buckets;
193 * Total # of bit buckets we have
195 * @var interger
196 * @access private
198 var $_bucketcount;
201 * # of total servers we have
203 * @var interger
204 * @access private
206 var $_active;
209 * Stream timeout in seconds. Applies for example to fread()
211 * @var integer
212 * @access private
214 var $_timeout_seconds;
217 * Stream timeout in microseconds
219 * @var integer
220 * @access private
222 var $_timeout_microseconds;
224 // }}}
225 // }}}
226 // {{{ methods
227 // {{{ public functions
228 // {{{ memcached()
231 * Memcache initializer
233 * @param array $args Associative array of settings
235 * @return mixed
236 * @access public
238 function memcached ($args)
240 $this->set_servers(@$args['servers']);
241 $this->_debug = @$args['debug'];
242 $this->stats = array();
243 $this->_compress_threshold = @$args['compress_threshold'];
244 $this->_persistant = array_key_exists('persistant', $args) ? (@$args['persistant']) : false;
245 $this->_compress_enable = true;
246 $this->_have_zlib = function_exists("gzcompress");
248 $this->_cache_sock = array();
249 $this->_host_dead = array();
251 $this->_timeout_seconds = 1;
252 $this->_timeout_microseconds = 0;
255 // }}}
256 // {{{ add()
259 * Adds a key/value to the memcache server if one isn't already set with
260 * that key
262 * @param string $key Key to set with data
263 * @param mixed $val Value to store
264 * @param interger $exp (optional) Time to expire data at
266 * @return boolean
267 * @access public
269 function add ($key, $val, $exp = 0)
271 return $this->_set('add', $key, $val, $exp);
274 // }}}
275 // {{{ decr()
278 * Decriment a value stored on the memcache server
280 * @param string $key Key to decriment
281 * @param interger $amt (optional) Amount to decriment
283 * @return mixed FALSE on failure, value on success
284 * @access public
286 function decr ($key, $amt=1)
288 return $this->_incrdecr('decr', $key, $amt);
291 // }}}
292 // {{{ delete()
295 * Deletes a key from the server, optionally after $time
297 * @param string $key Key to delete
298 * @param interger $time (optional) How long to wait before deleting
300 * @return boolean TRUE on success, FALSE on failure
301 * @access public
303 function delete ($key, $time = 0)
305 if (!$this->_active)
306 return false;
308 $sock = $this->get_sock($key);
309 if (!is_resource($sock))
310 return false;
312 $key = is_array($key) ? $key[1] : $key;
314 @$this->stats['delete']++;
315 $cmd = "delete $key $time\r\n";
316 if(!fwrite($sock, $cmd, strlen($cmd)))
318 $this->_dead_sock($sock);
319 return false;
321 $res = trim(fgets($sock));
323 if ($this->_debug)
324 $this->_debugprint(sprintf("MemCache: delete %s (%s)\n", $key, $res));
326 if ($res == "DELETED")
327 return true;
328 return false;
331 // }}}
332 // {{{ disconnect_all()
335 * Disconnects all connected sockets
337 * @access public
339 function disconnect_all ()
341 foreach ($this->_cache_sock as $sock)
342 fclose($sock);
344 $this->_cache_sock = array();
347 // }}}
348 // {{{ enable_compress()
351 * Enable / Disable compression
353 * @param boolean $enable TRUE to enable, FALSE to disable
355 * @access public
357 function enable_compress ($enable)
359 $this->_compress_enable = $enable;
362 // }}}
363 // {{{ forget_dead_hosts()
366 * Forget about all of the dead hosts
368 * @access public
370 function forget_dead_hosts ()
372 $this->_host_dead = array();
375 // }}}
376 // {{{ get()
379 * Retrieves the value associated with the key from the memcache server
381 * @param string $key Key to retrieve
383 * @return mixed
384 * @access public
386 function get ($key)
388 if (!$this->_active)
389 return false;
391 $sock = $this->get_sock($key);
393 if (!is_resource($sock))
394 return false;
396 @$this->stats['get']++;
398 $cmd = "get $key\r\n";
399 if (!fwrite($sock, $cmd, strlen($cmd)))
401 $this->_dead_sock($sock);
402 return false;
405 $val = array();
406 $this->_load_items($sock, $val);
408 if ($this->_debug)
409 foreach ($val as $k => $v)
410 $this->_debugprint(@sprintf("MemCache: sock %s got %s => %s\r\n", serialize($sock), $k, $v));
412 return @$val[$key];
415 // }}}
416 // {{{ get_multi()
419 * Get multiple keys from the server(s)
421 * @param array $keys Keys to retrieve
423 * @return array
424 * @access public
426 function get_multi ($keys)
428 if (!$this->_active)
429 return false;
431 $this->stats['get_multi']++;
433 foreach ($keys as $key)
435 $sock = $this->get_sock($key);
436 if (!is_resource($sock)) continue;
437 $key = is_array($key) ? $key[1] : $key;
438 if (!isset($sock_keys[$sock]))
440 $sock_keys[$sock] = array();
441 $socks[] = $sock;
443 $sock_keys[$sock][] = $key;
446 // Send out the requests
447 foreach ($socks as $sock)
449 $cmd = "get";
450 foreach ($sock_keys[$sock] as $key)
452 $cmd .= " ". $key;
454 $cmd .= "\r\n";
456 if (fwrite($sock, $cmd, strlen($cmd)))
458 $gather[] = $sock;
459 } else
461 $this->_dead_sock($sock);
465 // Parse responses
466 $val = array();
467 foreach ($gather as $sock)
469 $this->_load_items($sock, $val);
472 if ($this->_debug)
473 foreach ($val as $k => $v)
474 $this->_debugprint(sprintf("MemCache: got %s => %s\r\n", $k, $v));
476 return $val;
479 // }}}
480 // {{{ incr()
483 * Increments $key (optionally) by $amt
485 * @param string $key Key to increment
486 * @param interger $amt (optional) amount to increment
488 * @return interger New key value?
489 * @access public
491 function incr ($key, $amt=1)
493 return $this->_incrdecr('incr', $key, $amt);
496 // }}}
497 // {{{ replace()
500 * Overwrites an existing value for key; only works if key is already set
502 * @param string $key Key to set value as
503 * @param mixed $value Value to store
504 * @param interger $exp (optional) Experiation time
506 * @return boolean
507 * @access public
509 function replace ($key, $value, $exp=0)
511 return $this->_set('replace', $key, $value, $exp);
514 // }}}
515 // {{{ run_command()
518 * Passes through $cmd to the memcache server connected by $sock; returns
519 * output as an array (null array if no output)
521 * NOTE: due to a possible bug in how PHP reads while using fgets(), each
522 * line may not be terminated by a \r\n. More specifically, my testing
523 * has shown that, on FreeBSD at least, each line is terminated only
524 * with a \n. This is with the PHP flag auto_detect_line_endings set
525 * to falase (the default).
527 * @param resource $sock Socket to send command on
528 * @param string $cmd Command to run
530 * @return array Output array
531 * @access public
533 function run_command ($sock, $cmd)
535 if (!is_resource($sock))
536 return array();
538 if (!fwrite($sock, $cmd, strlen($cmd)))
539 return array();
541 while (true)
543 $res = fgets($sock);
544 $ret[] = $res;
545 if (preg_match('/^END/', $res))
546 break;
547 if (strlen($res) == 0)
548 break;
550 return $ret;
553 // }}}
554 // {{{ set()
557 * Unconditionally sets a key to a given value in the memcache. Returns true
558 * if set successfully.
560 * @param string $key Key to set value as
561 * @param mixed $value Value to set
562 * @param interger $exp (optional) Experiation time
564 * @return boolean TRUE on success
565 * @access public
567 function set ($key, $value, $exp=0)
569 return $this->_set('set', $key, $value, $exp);
572 // }}}
573 // {{{ set_compress_threshold()
576 * Sets the compression threshold
578 * @param interger $thresh Threshold to compress if larger than
580 * @access public
582 function set_compress_threshold ($thresh)
584 $this->_compress_threshold = $thresh;
587 // }}}
588 // {{{ set_debug()
591 * Sets the debug flag
593 * @param boolean $dbg TRUE for debugging, FALSE otherwise
595 * @access public
597 * @see memcahced::memcached
599 function set_debug ($dbg)
601 $this->_debug = $dbg;
604 // }}}
605 // {{{ set_servers()
608 * Sets the server list to distribute key gets and puts between
610 * @param array $list Array of servers to connect to
612 * @access public
614 * @see memcached::memcached()
616 function set_servers ($list)
618 $this->_servers = $list;
619 $this->_active = count($list);
620 $this->_buckets = null;
621 $this->_bucketcount = 0;
623 $this->_single_sock = null;
624 if ($this->_active == 1)
625 $this->_single_sock = $this->_servers[0];
629 * Sets the timeout for new connections
631 * @param integer $seconds Number of seconds
632 * @param integer $microseconds Number of microseconds
634 * @access public
636 function set_timeout ($seconds, $microseconds)
638 $this->_timeout_seconds = $seconds;
639 $this->_timeout_microseconds = $microseconds;
642 // }}}
643 // }}}
644 // {{{ private methods
645 // {{{ _close_sock()
648 * Close the specified socket
650 * @param string $sock Socket to close
652 * @access private
654 function _close_sock ($sock)
656 $host = array_search($sock, $this->_cache_sock);
657 fclose($this->_cache_sock[$host]);
658 unset($this->_cache_sock[$host]);
661 // }}}
662 // {{{ _connect_sock()
665 * Connects $sock to $host, timing out after $timeout
667 * @param interger $sock Socket to connect
668 * @param string $host Host:IP to connect to
669 * @param float $timeout (optional) Timeout value, defaults to 0.25s
671 * @return boolean
672 * @access private
674 function _connect_sock (&$sock, $host, $timeout = 0.25)
676 list ($ip, $port) = explode(":", $host);
677 if ($this->_persistant == 1)
679 $sock = @pfsockopen($ip, $port, $errno, $errstr, $timeout);
680 } else
682 $sock = @fsockopen($ip, $port, $errno, $errstr, $timeout);
685 if (!$sock) {
686 if ($this->_debug)
687 $this->_debugprint( "Error connecting to $host: $errstr\n" );
688 return false;
691 // Initialise timeout
692 stream_set_timeout($sock, $this->_timeout_seconds, $this->_timeout_microseconds);
694 return true;
697 // }}}
698 // {{{ _dead_sock()
701 * Marks a host as dead until 30-40 seconds in the future
703 * @param string $sock Socket to mark as dead
705 * @access private
707 function _dead_sock ($sock)
709 $host = array_search($sock, $this->_cache_sock);
710 @list ($ip, $port) = explode(":", $host);
711 $this->_host_dead[$ip] = time() + 30 + intval(rand(0, 10));
712 $this->_host_dead[$host] = $this->_host_dead[$ip];
713 unset($this->_cache_sock[$host]);
716 // }}}
717 // {{{ get_sock()
720 * get_sock
722 * @param string $key Key to retrieve value for;
724 * @return mixed resource on success, false on failure
725 * @access private
727 function get_sock ($key)
729 if (!$this->_active)
730 return false;
732 if ($this->_single_sock !== null)
733 return $this->sock_to_host($this->_single_sock);
735 $hv = is_array($key) ? intval($key[0]) : $this->_hashfunc($key);
737 if ($this->_buckets === null)
739 foreach ($this->_servers as $v)
741 if (is_array($v))
743 for ($i=0; $i<$v[1]; $i++)
744 $bu[] = $v[0];
745 } else
747 $bu[] = $v;
750 $this->_buckets = $bu;
751 $this->_bucketcount = count($bu);
754 $realkey = is_array($key) ? $key[1] : $key;
755 for ($tries = 0; $tries<20; $tries++)
757 $host = $this->_buckets[$hv % $this->_bucketcount];
758 $sock = $this->sock_to_host($host);
759 if (is_resource($sock))
760 return $sock;
761 $hv += $this->_hashfunc($tries . $realkey);
764 return false;
767 // }}}
768 // {{{ _hashfunc()
771 * Creates a hash interger based on the $key
773 * @param string $key Key to hash
775 * @return interger Hash value
776 * @access private
778 function _hashfunc ($key)
780 # Hash function must on [0,0x7ffffff]
781 # We take the first 31 bits of the MD5 hash, which unlike the hash
782 # function used in a previous version of this client, works
783 return hexdec(substr(md5($key),0,8)) & 0x7fffffff;
786 // }}}
787 // {{{ _incrdecr()
790 * Perform increment/decriment on $key
792 * @param string $cmd Command to perform
793 * @param string $key Key to perform it on
794 * @param interger $amt Amount to adjust
796 * @return interger New value of $key
797 * @access private
799 function _incrdecr ($cmd, $key, $amt=1)
801 if (!$this->_active)
802 return null;
804 $sock = $this->get_sock($key);
805 if (!is_resource($sock))
806 return null;
808 $key = is_array($key) ? $key[1] : $key;
809 @$this->stats[$cmd]++;
810 if (!fwrite($sock, "$cmd $key $amt\r\n"))
811 return $this->_dead_sock($sock);
813 stream_set_timeout($sock, 1, 0);
814 $line = fgets($sock);
815 if (!preg_match('/^(\d+)/', $line, $match))
816 return null;
817 return $match[1];
820 // }}}
821 // {{{ _load_items()
824 * Load items into $ret from $sock
826 * @param resource $sock Socket to read from
827 * @param array $ret Returned values
829 * @access private
831 function _load_items ($sock, &$ret)
833 while (1)
835 $decl = fgets($sock);
836 if ($decl == "END\r\n")
838 return true;
839 } elseif (preg_match('/^VALUE (\S+) (\d+) (\d+)\r\n$/', $decl, $match))
841 list($rkey, $flags, $len) = array($match[1], $match[2], $match[3]);
842 $bneed = $len+2;
843 $offset = 0;
845 while ($bneed > 0)
847 $data = fread($sock, $bneed);
848 $n = strlen($data);
849 if ($n == 0)
850 break;
851 $offset += $n;
852 $bneed -= $n;
853 @$ret[$rkey] .= $data;
856 if ($offset != $len+2)
858 // Something is borked!
859 if ($this->_debug)
860 $this->_debugprint(sprintf("Something is borked! key %s expecting %d got %d length\n", $rkey, $len+2, $offset));
862 unset($ret[$rkey]);
863 $this->_close_sock($sock);
864 return false;
867 if ($this->_have_zlib && $flags & MEMCACHE_COMPRESSED)
868 $ret[$rkey] = gzuncompress($ret[$rkey]);
870 $ret[$rkey] = rtrim($ret[$rkey]);
872 if ($flags & MEMCACHE_SERIALIZED)
873 $ret[$rkey] = unserialize($ret[$rkey]);
875 } else
877 $this->_debugprint("Error parsing memcached response\n");
878 return 0;
883 // }}}
884 // {{{ _set()
887 * Performs the requested storage operation to the memcache server
889 * @param string $cmd Command to perform
890 * @param string $key Key to act on
891 * @param mixed $val What we need to store
892 * @param interger $exp When it should expire
894 * @return boolean
895 * @access private
897 function _set ($cmd, $key, $val, $exp)
899 if (!$this->_active)
900 return false;
902 $sock = $this->get_sock($key);
903 if (!is_resource($sock))
904 return false;
906 @$this->stats[$cmd]++;
908 $flags = 0;
910 if (!is_scalar($val))
912 $val = serialize($val);
913 $flags |= MEMCACHE_SERIALIZED;
914 if ($this->_debug)
915 $this->_debugprint(sprintf("client: serializing data as it is not scalar\n"));
918 $len = strlen($val);
920 if ($this->_have_zlib && $this->_compress_enable &&
921 $this->_compress_threshold && $len >= $this->_compress_threshold)
923 $c_val = gzcompress($val, 9);
924 $c_len = strlen($c_val);
926 if ($c_len < $len*(1 - COMPRESSION_SAVINGS))
928 if ($this->_debug)
929 $this->_debugprint(sprintf("client: compressing data; was %d bytes is now %d bytes\n", $len, $c_len));
930 $val = $c_val;
931 $len = $c_len;
932 $flags |= MEMCACHE_COMPRESSED;
935 if (!fwrite($sock, "$cmd $key $flags $exp $len\r\n$val\r\n"))
936 return $this->_dead_sock($sock);
938 $line = trim(fgets($sock));
940 if ($this->_debug)
942 if ($flags & MEMCACHE_COMPRESSED)
943 $val = 'compressed data';
944 $this->_debugprint(sprintf("MemCache: %s %s => %s (%s)\n", $cmd, $key, $val, $line));
946 if ($line == "STORED")
947 return true;
948 return false;
951 // }}}
952 // {{{ sock_to_host()
955 * Returns the socket for the host
957 * @param string $host Host:IP to get socket for
959 * @return mixed IO Stream or false
960 * @access private
962 function sock_to_host ($host)
964 if (isset($this->_cache_sock[$host]))
965 return $this->_cache_sock[$host];
967 $now = time();
968 list ($ip, $port) = explode (":", $host);
969 if (isset($this->_host_dead[$host]) && $this->_host_dead[$host] > $now ||
970 isset($this->_host_dead[$ip]) && $this->_host_dead[$ip] > $now)
971 return null;
973 if (!$this->_connect_sock($sock, $host))
974 return $this->_dead_sock($host);
976 // Do not buffer writes
977 stream_set_write_buffer($sock, 0);
979 $this->_cache_sock[$host] = $sock;
981 return $this->_cache_sock[$host];
984 function _debugprint($str){
985 print($str);
988 // }}}
989 // }}}
990 // }}}
993 // }}}