* double quotes to single quotes
[mediawiki.git] / includes / memcached-client.php
blob17d9ecbecd3d790ed888311f126b5b1a5fc9f046
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;
208 // }}}
209 // }}}
210 // {{{ methods
211 // {{{ public functions
212 // {{{ memcached()
215 * Memcache initializer
217 * @param array $args Associative array of settings
219 * @return mixed
220 * @access public
222 function memcached ($args)
224 $this->set_servers(@$args['servers']);
225 $this->_debug = @$args['debug'];
226 $this->stats = array();
227 $this->_compress_threshold = @$args['compress_threshold'];
228 $this->_persistant = array_key_exists('persistant', $args) ? (@$args['persistant']) : false;
229 $this->_compress_enable = true;
230 $this->_have_zlib = function_exists("gzcompress");
232 $this->_cache_sock = array();
233 $this->_host_dead = array();
236 // }}}
237 // {{{ add()
240 * Adds a key/value to the memcache server if one isn't already set with
241 * that key
243 * @param string $key Key to set with data
244 * @param mixed $val Value to store
245 * @param interger $exp (optional) Time to expire data at
247 * @return boolean
248 * @access public
250 function add ($key, $val, $exp = 0)
252 return $this->_set('add', $key, $val, $exp);
255 // }}}
256 // {{{ decr()
259 * Decriment a value stored on the memcache server
261 * @param string $key Key to decriment
262 * @param interger $amt (optional) Amount to decriment
264 * @return mixed FALSE on failure, value on success
265 * @access public
267 function decr ($key, $amt=1)
269 return $this->_incrdecr('decr', $key, $amt);
272 // }}}
273 // {{{ delete()
276 * Deletes a key from the server, optionally after $time
278 * @param string $key Key to delete
279 * @param interger $time (optional) How long to wait before deleting
281 * @return boolean TRUE on success, FALSE on failure
282 * @access public
284 function delete ($key, $time = 0)
286 if (!$this->_active)
287 return false;
289 $sock = $this->get_sock($key);
290 if (!is_resource($sock))
291 return false;
293 $key = is_array($key) ? $key[1] : $key;
295 @$this->stats['delete']++;
296 $cmd = "delete $key $time\r\n";
297 if(!fwrite($sock, $cmd, strlen($cmd)))
299 $this->_dead_sock($sock);
300 return false;
302 $res = trim(fgets($sock));
304 if ($this->_debug)
305 $this->_debugprint(sprintf("MemCache: delete %s (%s)\n", $key, $res));
307 if ($res == "DELETED")
308 return true;
309 return false;
312 // }}}
313 // {{{ disconnect_all()
316 * Disconnects all connected sockets
318 * @access public
320 function disconnect_all ()
322 foreach ($this->_cache_sock as $sock)
323 fclose($sock);
325 $this->_cache_sock = array();
328 // }}}
329 // {{{ enable_compress()
332 * Enable / Disable compression
334 * @param boolean $enable TRUE to enable, FALSE to disable
336 * @access public
338 function enable_compress ($enable)
340 $this->_compress_enable = $enable;
343 // }}}
344 // {{{ forget_dead_hosts()
347 * Forget about all of the dead hosts
349 * @access public
351 function forget_dead_hosts ()
353 $this->_host_dead = array();
356 // }}}
357 // {{{ get()
360 * Retrieves the value associated with the key from the memcache server
362 * @param string $key Key to retrieve
364 * @return mixed
365 * @access public
367 function get ($key)
369 if (!$this->_active)
370 return false;
372 $sock = $this->get_sock($key);
374 if (!is_resource($sock))
375 return false;
377 @$this->stats['get']++;
379 $cmd = "get $key\r\n";
380 if (!fwrite($sock, $cmd, strlen($cmd)))
382 $this->_dead_sock($sock);
383 return false;
386 $val = array();
387 $this->_load_items($sock, $val);
389 if ($this->_debug)
390 foreach ($val as $k => $v)
391 $this->_debugprint(@sprintf("MemCache: sock %s got %s => %s\r\n", serialize($sock), $k, $v));
393 return @$val[$key];
396 // }}}
397 // {{{ get_multi()
400 * Get multiple keys from the server(s)
402 * @param array $keys Keys to retrieve
404 * @return array
405 * @access public
407 function get_multi ($keys)
409 if (!$this->_active)
410 return false;
412 $this->stats['get_multi']++;
414 foreach ($keys as $key)
416 $sock = $this->get_sock($key);
417 if (!is_resource($sock)) continue;
418 $key = is_array($key) ? $key[1] : $key;
419 if (!isset($sock_keys[$sock]))
421 $sock_keys[$sock] = array();
422 $socks[] = $sock;
424 $sock_keys[$sock][] = $key;
427 // Send out the requests
428 foreach ($socks as $sock)
430 $cmd = "get";
431 foreach ($sock_keys[$sock] as $key)
433 $cmd .= " ". $key;
435 $cmd .= "\r\n";
437 if (fwrite($sock, $cmd, strlen($cmd)))
439 $gather[] = $sock;
440 } else
442 $this->_dead_sock($sock);
446 // Parse responses
447 $val = array();
448 foreach ($gather as $sock)
450 $this->_load_items($sock, $val);
453 if ($this->_debug)
454 foreach ($val as $k => $v)
455 $this->_debugprint(sprintf("MemCache: got %s => %s\r\n", $k, $v));
457 return $val;
460 // }}}
461 // {{{ incr()
464 * Increments $key (optionally) by $amt
466 * @param string $key Key to increment
467 * @param interger $amt (optional) amount to increment
469 * @return interger New key value?
470 * @access public
472 function incr ($key, $amt=1)
474 return $this->_incrdecr('incr', $key, $amt);
477 // }}}
478 // {{{ replace()
481 * Overwrites an existing value for key; only works if key is already set
483 * @param string $key Key to set value as
484 * @param mixed $value Value to store
485 * @param interger $exp (optional) Experiation time
487 * @return boolean
488 * @access public
490 function replace ($key, $value, $exp=0)
492 return $this->_set('replace', $key, $value, $exp);
495 // }}}
496 // {{{ run_command()
499 * Passes through $cmd to the memcache server connected by $sock; returns
500 * output as an array (null array if no output)
502 * NOTE: due to a possible bug in how PHP reads while using fgets(), each
503 * line may not be terminated by a \r\n. More specifically, my testing
504 * has shown that, on FreeBSD at least, each line is terminated only
505 * with a \n. This is with the PHP flag auto_detect_line_endings set
506 * to falase (the default).
508 * @param resource $sock Socket to send command on
509 * @param string $cmd Command to run
511 * @return array Output array
512 * @access public
514 function run_command ($sock, $cmd)
516 if (!is_resource($sock))
517 return array();
519 if (!fwrite($sock, $cmd, strlen($cmd)))
520 return array();
522 while (true)
524 $res = fgets($sock);
525 $ret[] = $res;
526 if (preg_match('/^END/', $res))
527 break;
528 if (strlen($res) == 0)
529 break;
531 return $ret;
534 // }}}
535 // {{{ set()
538 * Unconditionally sets a key to a given value in the memcache. Returns true
539 * if set successfully.
541 * @param string $key Key to set value as
542 * @param mixed $value Value to set
543 * @param interger $exp (optional) Experiation time
545 * @return boolean TRUE on success
546 * @access public
548 function set ($key, $value, $exp=0)
550 return $this->_set('set', $key, $value, $exp);
553 // }}}
554 // {{{ set_compress_threshold()
557 * Sets the compression threshold
559 * @param interger $thresh Threshold to compress if larger than
561 * @access public
563 function set_compress_threshold ($thresh)
565 $this->_compress_threshold = $thresh;
568 // }}}
569 // {{{ set_debug()
572 * Sets the debug flag
574 * @param boolean $dbg TRUE for debugging, FALSE otherwise
576 * @access public
578 * @see memcahced::memcached
580 function set_debug ($dbg)
582 $this->_debug = $dbg;
585 // }}}
586 // {{{ set_servers()
589 * Sets the server list to distribute key gets and puts between
591 * @param array $list Array of servers to connect to
593 * @access public
595 * @see memcached::memcached()
597 function set_servers ($list)
599 $this->_servers = $list;
600 $this->_active = count($list);
601 $this->_buckets = null;
602 $this->_bucketcount = 0;
604 $this->_single_sock = null;
605 if ($this->_active == 1)
606 $this->_single_sock = $this->_servers[0];
609 // }}}
610 // }}}
611 // {{{ private methods
612 // {{{ _close_sock()
615 * Close the specified socket
617 * @param string $sock Socket to close
619 * @access private
621 function _close_sock ($sock)
623 $host = array_search($sock, $this->_cache_sock);
624 fclose($this->_cache_sock[$host]);
625 unset($this->_cache_sock[$host]);
628 // }}}
629 // {{{ _connect_sock()
632 * Connects $sock to $host, timing out after $timeout
634 * @param interger $sock Socket to connect
635 * @param string $host Host:IP to connect to
636 * @param float $timeout (optional) Timeout value, defaults to 0.25s
638 * @return boolean
639 * @access private
641 function _connect_sock (&$sock, $host, $timeout = 0.25)
643 list ($ip, $port) = explode(":", $host);
644 if ($this->_persistant == 1)
646 $sock = @pfsockopen($ip, $port, $errno, $errstr, $timeout);
647 } else
649 $sock = @fsockopen($ip, $port, $errno, $errstr, $timeout);
652 if (!$sock)
653 return false;
654 return true;
657 // }}}
658 // {{{ _dead_sock()
661 * Marks a host as dead until 30-40 seconds in the future
663 * @param string $sock Socket to mark as dead
665 * @access private
667 function _dead_sock ($sock)
669 $host = array_search($sock, $this->_cache_sock);
670 @list ($ip, $port) = explode(":", $host);
671 $this->_host_dead[$ip] = time() + 30 + intval(rand(0, 10));
672 $this->_host_dead[$host] = $this->_host_dead[$ip];
673 unset($this->_cache_sock[$host]);
676 // }}}
677 // {{{ get_sock()
680 * get_sock
682 * @param string $key Key to retrieve value for;
684 * @return mixed resource on success, false on failure
685 * @access private
687 function get_sock ($key)
689 if (!$this->_active)
690 return false;
692 if ($this->_single_sock !== null)
693 return $this->sock_to_host($this->_single_sock);
695 $hv = is_array($key) ? intval($key[0]) : $this->_hashfunc($key);
697 if ($this->_buckets === null)
699 foreach ($this->_servers as $v)
701 if (is_array($v))
703 for ($i=0; $i<$v[1]; $i++)
704 $bu[] = $v[0];
705 } else
707 $bu[] = $v;
710 $this->_buckets = $bu;
711 $this->_bucketcount = count($bu);
714 $realkey = is_array($key) ? $key[1] : $key;
715 for ($tries = 0; $tries<20; $tries++)
717 $host = $this->_buckets[$hv % $this->_bucketcount];
718 $sock = $this->sock_to_host($host);
719 if (is_resource($sock))
720 return $sock;
721 $hv += $this->_hashfunc($tries . $realkey);
724 return false;
727 // }}}
728 // {{{ _hashfunc()
731 * Creates a hash interger based on the $key
733 * @param string $key Key to hash
735 * @return interger Hash value
736 * @access private
738 function _hashfunc ($key)
740 return crc32($key);
743 // }}}
744 // {{{ _incrdecr()
747 * Perform increment/decriment on $key
749 * @param string $cmd Command to perform
750 * @param string $key Key to perform it on
751 * @param interger $amt Amount to adjust
753 * @return interger New value of $key
754 * @access private
756 function _incrdecr ($cmd, $key, $amt=1)
758 if (!$this->_active)
759 return null;
761 $sock = $this->get_sock($key);
762 if (!is_resource($sock))
763 return null;
765 $key = is_array($key) ? $key[1] : $key;
766 $this->stats[$cmd]++;
767 if (!fwrite($sock, "$cmd $key $amt\r\n"))
768 return $this->_dead_sock($sock);
770 stream_set_timeout($sock, 1, 0);
771 $line = fgets($sock);
772 if (!preg_match('/^(\d+)/', $line, $match))
773 return null;
774 return $match[1];
777 // }}}
778 // {{{ _load_items()
781 * Load items into $ret from $sock
783 * @param resource $sock Socket to read from
784 * @param array $ret Returned values
786 * @access private
788 function _load_items ($sock, &$ret)
790 while (1)
792 $decl = fgets($sock);
793 if ($decl == "END\r\n")
795 return true;
796 } elseif (preg_match('/^VALUE (\S+) (\d+) (\d+)\r\n$/', $decl, $match))
798 list($rkey, $flags, $len) = array($match[1], $match[2], $match[3]);
799 $bneed = $len+2;
800 $offset = 0;
802 while ($bneed > 0)
804 $data = fread($sock, $bneed);
805 $n = strlen($data);
806 if ($n == 0)
807 break;
808 $offset += $n;
809 $bneed -= $n;
810 @$ret[$rkey] .= $data;
813 if ($offset != $len+2)
815 // Something is borked!
816 if ($this->_debug)
817 $this->_debugprint(sprintf("Something is borked! key %s expecting %d got %d length\n", $rkey, $len+2, $offset));
819 unset($ret[$rkey]);
820 $this->_close_sock($sock);
821 return false;
824 $ret[$rkey] = rtrim($ret[$rkey]);
826 if ($this->_have_zlib && $flags & MEMCACHE_COMPRESSED)
827 $ret[$rkey] = gzuncompress($ret[$rkey]);
829 if ($flags & MEMCACHE_SERIALIZED)
830 $ret[$rkey] = unserialize($ret[$rkey]);
832 } else
834 $this->_debugprint("Error parsing memcached response\n");
835 return 0;
840 // }}}
841 // {{{ _set()
844 * Performs the requested storage operation to the memcache server
846 * @param string $cmd Command to perform
847 * @param string $key Key to act on
848 * @param mixed $val What we need to store
849 * @param interger $exp When it should expire
851 * @return boolean
852 * @access private
854 function _set ($cmd, $key, $val, $exp)
856 if (!$this->_active)
857 return false;
859 $sock = $this->get_sock($key);
860 if (!is_resource($sock))
861 return false;
863 @$this->stats[$cmd]++;
865 $flags = 0;
867 if (!is_scalar($val))
869 $val = serialize($val);
870 $flags |= MEMCACHE_SERIALIZED;
871 if ($this->_debug)
872 $this->_debugprint(sprintf("client: serializing data as it is not scalar\n"));
875 $len = strlen($val);
877 if ($this->_have_zlib && $this->_compress_enable &&
878 $this->_compress_threshold && $len >= $this->_compress_threshold)
880 $c_val = gzcompress($val, 9);
881 $c_len = strlen($c_val);
883 if ($c_len < $len*(1 - COMPRESS_SAVINGS))
885 if ($this->_debug)
886 $this->_debugprint(sprintf("client: compressing data; was %d bytes is now %d bytes\n", $len, $c_len));
887 $val = $c_val;
888 $len = $c_len;
889 $flags |= MEMCACHE_COMPRESSED;
892 if (!fwrite($sock, "$cmd $key $flags $exp $len\r\n$val\r\n"))
893 return $this->_dead_sock($sock);
895 $line = trim(fgets($sock));
897 if ($this->_debug)
899 if ($flags & MEMCACHE_COMPRESSED)
900 $val = 'compressed data';
901 $this->_debugprint(sprintf("MemCache: %s %s => %s (%s)\n", $cmd, $key, $val, $line));
903 if ($line == "STORED")
904 return true;
905 return false;
908 // }}}
909 // {{{ sock_to_host()
912 * Returns the socket for the host
914 * @param string $host Host:IP to get socket for
916 * @return mixed IO Stream or false
917 * @access private
919 function sock_to_host ($host)
921 if (isset($this->_cache_sock[$host]))
922 return $this->_cache_sock[$host];
924 $now = time();
925 list ($ip, $port) = explode (":", $host);
926 if (isset($this->_host_dead[$host]) && $this->_host_dead[$host] > $now ||
927 isset($this->_host_dead[$ip]) && $this->_host_dead[$ip] > $now)
928 return null;
930 if (!$this->_connect_sock($sock, $host))
931 return $this->_dead_sock($host);
933 // Do not buffer writes
934 stream_set_write_buffer($sock, 0);
936 $this->_cache_sock[$host] = $sock;
938 return $this->_cache_sock[$host];
941 function _debugprint($str){
942 print($str);
945 // }}}
946 // }}}
947 // }}}
950 // }}}