3 * Memcached client for PHP.
5 * +---------------------------------------------------------------------------+
6 * | memcached client, PHP |
7 * +---------------------------------------------------------------------------+
8 * | Copyright (c) 2003 Ryan T. Dean <rtdean@cytherianage.net> |
9 * | All rights reserved. |
11 * | Redistribution and use in source and binary forms, with or without |
12 * | modification, are permitted provided that the following conditions |
15 * | 1. Redistributions of source code must retain the above copyright |
16 * | notice, this list of conditions and the following disclaimer. |
17 * | 2. Redistributions in binary form must reproduce the above copyright |
18 * | notice, this list of conditions and the following disclaimer in the |
19 * | documentation and/or other materials provided with the distribution. |
21 * | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
22 * | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
23 * | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
24 * | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
25 * | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
26 * | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
27 * | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
28 * | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
29 * | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
30 * | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
31 * +---------------------------------------------------------------------------+
32 * | Author: Ryan T. Dean <rtdean@cytherianage.net> |
33 * | Heavily influenced by the Perl memcached client by Brad Fitzpatrick. |
34 * | Permission granted by Brad Fitzpatrick for relicense of ported Perl |
35 * | client logic under 2-clause BSD license. |
36 * +---------------------------------------------------------------------------+
43 * This is the PHP client for memcached - a distributed memory cache daemon.
44 * More information is available at http://www.danga.com/memcached/
48 * require_once 'memcached.php';
50 * $mc = new MWMemcached(array(
51 * 'servers' => array('127.0.0.1:10000',
52 * array('192.0.0.1:10010', 2),
55 * 'compress_threshold' => 10240,
56 * 'persistent' => true));
58 * $mc->add('key', array('some', 'array'));
59 * $mc->replace('key', 'some random string');
60 * $val = $mc->get('key');
62 * @author Ryan T. Dean <rtdean@cytherianage.net>
69 // {{{ class MWMemcached
71 * memcached client class implemented using (p)fsockopen()
73 * @author Ryan T. Dean <rtdean@cytherianage.net>
84 * Flag: indicates data is serialized
89 * Flag: indicates data is compressed
96 * Minimum savings to store data compressed
98 const COMPRESSION_SAVINGS
= 0.20;
115 * Cached Sockets that are connected
123 * Current debug status; 0 - none to 9 - profiling
131 * Dead hosts, assoc array, 'host'=>'unixtime when ok to check again'
139 * Is compression available?
147 * Do we want to use compression?
152 var $_compress_enable;
155 * At how many bytes should we compress?
160 var $_compress_threshold;
163 * Are we using persistent links?
171 * If only using one server; contains ip:port to connect to
179 * Array containing ip:port or array(ip:port, weight)
195 * Total # of bit buckets we have
203 * # of total servers we have
211 * Stream timeout in seconds. Applies for example to fread()
216 var $_timeout_seconds;
219 * Stream timeout in microseconds
224 var $_timeout_microseconds;
227 * Connect timeout in seconds
229 var $_connect_timeout;
232 * Number of connection attempts for each server
234 var $_connect_attempts;
239 // {{{ public functions
243 * Memcache initializer
245 * @param $args Array Associative array of settings
249 public function __construct( $args ) {
250 $this->set_servers( isset( $args['servers'] ) ?
$args['servers'] : array() );
251 $this->_debug
= isset( $args['debug'] ) ?
$args['debug'] : false;
252 $this->stats
= array();
253 $this->_compress_threshold
= isset( $args['compress_threshold'] ) ?
$args['compress_threshold'] : 0;
254 $this->_persistent
= isset( $args['persistent'] ) ?
$args['persistent'] : false;
255 $this->_compress_enable
= true;
256 $this->_have_zlib
= function_exists( 'gzcompress' );
258 $this->_cache_sock
= array();
259 $this->_host_dead
= array();
261 $this->_timeout_seconds
= 0;
262 $this->_timeout_microseconds
= isset( $args['timeout'] ) ?
$args['timeout'] : 100000;
264 $this->_connect_timeout
= isset( $args['connect_timeout'] ) ?
$args['connect_timeout'] : 0.1;
265 $this->_connect_attempts
= 2;
272 * Adds a key/value to the memcache server if one isn't already set with
275 * @param $key String: key to set with data
276 * @param $val Mixed: value to store
277 * @param $exp Integer: (optional) Expiration time. This can be a number of seconds
278 * to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
279 * longer must be the timestamp of the time at which the mapping should expire. It
280 * is safe to use timestamps in all cases, regardless of exipration
281 * eg: strtotime("+3 hour")
285 public function add( $key, $val, $exp = 0 ) {
286 return $this->_set( 'add', $key, $val, $exp );
293 * Decrease a value stored on the memcache server
295 * @param $key String: key to decrease
296 * @param $amt Integer: (optional) amount to decrease
298 * @return Mixed: FALSE on failure, value on success
300 public function decr( $key, $amt = 1 ) {
301 return $this->_incrdecr( 'decr', $key, $amt );
308 * Deletes a key from the server, optionally after $time
310 * @param $key String: key to delete
311 * @param $time Integer: (optional) how long to wait before deleting
313 * @return Boolean: TRUE on success, FALSE on failure
315 public function delete( $key, $time = 0 ) {
316 if ( !$this->_active
) {
320 $sock = $this->get_sock( $key );
321 if ( !is_resource( $sock ) ) {
325 $key = is_array( $key ) ?
$key[1] : $key;
327 if ( isset( $this->stats
['delete'] ) ) {
328 $this->stats
['delete']++
;
330 $this->stats
['delete'] = 1;
332 $cmd = "delete $key $time\r\n";
333 if( !$this->_safe_fwrite( $sock, $cmd, strlen( $cmd ) ) ) {
334 $this->_dead_sock( $sock );
337 $res = trim( fgets( $sock ) );
339 if ( $this->_debug
) {
340 $this->_debugprint( sprintf( "MemCache: delete %s (%s)\n", $key, $res ) );
343 if ( $res == "DELETED" ||
$res == "NOT_FOUND" ) {
352 * @param $timeout int
355 public function lock( $key, $timeout = 0 ) {
364 public function unlock( $key ) {
370 // {{{ disconnect_all()
373 * Disconnects all connected sockets
375 public function disconnect_all() {
376 foreach ( $this->_cache_sock
as $sock ) {
380 $this->_cache_sock
= array();
384 // {{{ enable_compress()
387 * Enable / Disable compression
389 * @param $enable Boolean: TRUE to enable, FALSE to disable
391 public function enable_compress( $enable ) {
392 $this->_compress_enable
= $enable;
396 // {{{ forget_dead_hosts()
399 * Forget about all of the dead hosts
401 public function forget_dead_hosts() {
402 $this->_host_dead
= array();
409 * Retrieves the value associated with the key from the memcache server
411 * @param $key array|string key to retrieve
415 public function get( $key ) {
416 wfProfileIn( __METHOD__
);
418 if ( $this->_debug
) {
419 $this->_debugprint( "get($key)\n" );
422 if ( !$this->_active
) {
423 wfProfileOut( __METHOD__
);
427 $sock = $this->get_sock( $key );
429 if ( !is_resource( $sock ) ) {
430 wfProfileOut( __METHOD__
);
434 $key = is_array( $key ) ?
$key[1] : $key;
435 if ( isset( $this->stats
['get'] ) ) {
436 $this->stats
['get']++
;
438 $this->stats
['get'] = 1;
441 $cmd = "get $key\r\n";
442 if ( !$this->_safe_fwrite( $sock, $cmd, strlen( $cmd ) ) ) {
443 $this->_dead_sock( $sock );
444 wfProfileOut( __METHOD__
);
449 $this->_load_items( $sock, $val );
451 if ( $this->_debug
) {
452 foreach ( $val as $k => $v ) {
453 $this->_debugprint( sprintf( "MemCache: sock %s got %s\n", serialize( $sock ), $k ) );
458 if ( isset( $val[$key] ) ) {
461 wfProfileOut( __METHOD__
);
469 * Get multiple keys from the server(s)
471 * @param $keys Array: keys to retrieve
475 public function get_multi( $keys ) {
476 if ( !$this->_active
) {
480 if ( isset( $this->stats
['get_multi'] ) ) {
481 $this->stats
['get_multi']++
;
483 $this->stats
['get_multi'] = 1;
485 $sock_keys = array();
487 foreach ( $keys as $key ) {
488 $sock = $this->get_sock( $key );
489 if ( !is_resource( $sock ) ) {
492 $key = is_array( $key ) ?
$key[1] : $key;
493 if ( !isset( $sock_keys[$sock] ) ) {
494 $sock_keys[$sock] = array();
497 $sock_keys[$sock][] = $key;
501 // Send out the requests
502 foreach ( $socks as $sock ) {
504 foreach ( $sock_keys[$sock] as $key ) {
509 if ( $this->_safe_fwrite( $sock, $cmd, strlen( $cmd ) ) ) {
512 $this->_dead_sock( $sock );
518 foreach ( $gather as $sock ) {
519 $this->_load_items( $sock, $val );
522 if ( $this->_debug
) {
523 foreach ( $val as $k => $v ) {
524 $this->_debugprint( sprintf( "MemCache: got %s\n", $k ) );
535 * Increments $key (optionally) by $amt
537 * @param $key String: key to increment
538 * @param $amt Integer: (optional) amount to increment
540 * @return Integer: null if the key does not exist yet (this does NOT
541 * create new mappings if the key does not exist). If the key does
542 * exist, this returns the new value for that key.
544 public function incr( $key, $amt = 1 ) {
545 return $this->_incrdecr( 'incr', $key, $amt );
552 * Overwrites an existing value for key; only works if key is already set
554 * @param $key String: key to set value as
555 * @param $value Mixed: value to store
556 * @param $exp Integer: (optional) Expiration time. This can be a number of seconds
557 * to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
558 * longer must be the timestamp of the time at which the mapping should expire. It
559 * is safe to use timestamps in all cases, regardless of exipration
560 * eg: strtotime("+3 hour")
564 public function replace( $key, $value, $exp = 0 ) {
565 return $this->_set( 'replace', $key, $value, $exp );
572 * Passes through $cmd to the memcache server connected by $sock; returns
573 * output as an array (null array if no output)
575 * NOTE: due to a possible bug in how PHP reads while using fgets(), each
576 * line may not be terminated by a \r\n. More specifically, my testing
577 * has shown that, on FreeBSD at least, each line is terminated only
578 * with a \n. This is with the PHP flag auto_detect_line_endings set
579 * to falase (the default).
581 * @param $sock Resource: socket to send command on
582 * @param $cmd String: command to run
584 * @return Array: output array
586 public function run_command( $sock, $cmd ) {
587 if ( !is_resource( $sock ) ) {
591 if ( !$this->_safe_fwrite( $sock, $cmd, strlen( $cmd ) ) ) {
597 $res = fgets( $sock );
599 if ( preg_match( '/^END/', $res ) ) {
602 if ( strlen( $res ) == 0 ) {
613 * Unconditionally sets a key to a given value in the memcache. Returns true
614 * if set successfully.
616 * @param $key String: key to set value as
617 * @param $value Mixed: value to set
618 * @param $exp Integer: (optional) Expiration time. This can be a number of seconds
619 * to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
620 * longer must be the timestamp of the time at which the mapping should expire. It
621 * is safe to use timestamps in all cases, regardless of exipration
622 * eg: strtotime("+3 hour")
624 * @return Boolean: TRUE on success
626 public function set( $key, $value, $exp = 0 ) {
627 return $this->_set( 'set', $key, $value, $exp );
631 // {{{ set_compress_threshold()
634 * Sets the compression threshold
636 * @param $thresh Integer: threshold to compress if larger than
638 public function set_compress_threshold( $thresh ) {
639 $this->_compress_threshold
= $thresh;
646 * Sets the debug flag
648 * @param $dbg Boolean: TRUE for debugging, FALSE otherwise
650 * @see MWMemcached::__construct
652 public function set_debug( $dbg ) {
653 $this->_debug
= $dbg;
660 * Sets the server list to distribute key gets and puts between
662 * @param $list Array of servers to connect to
664 * @see MWMemcached::__construct()
666 public function set_servers( $list ) {
667 $this->_servers
= $list;
668 $this->_active
= count( $list );
669 $this->_buckets
= null;
670 $this->_bucketcount
= 0;
672 $this->_single_sock
= null;
673 if ( $this->_active
== 1 ) {
674 $this->_single_sock
= $this->_servers
[0];
679 * Sets the timeout for new connections
681 * @param $seconds Integer: number of seconds
682 * @param $microseconds Integer: number of microseconds
684 public function set_timeout( $seconds, $microseconds ) {
685 $this->_timeout_seconds
= $seconds;
686 $this->_timeout_microseconds
= $microseconds;
691 // {{{ private methods
695 * Close the specified socket
697 * @param $sock String: socket to close
701 function _close_sock( $sock ) {
702 $host = array_search( $sock, $this->_cache_sock
);
703 fclose( $this->_cache_sock
[$host] );
704 unset( $this->_cache_sock
[$host] );
708 // {{{ _connect_sock()
711 * Connects $sock to $host, timing out after $timeout
713 * @param $sock Integer: socket to connect
714 * @param $host String: Host:IP to connect to
719 function _connect_sock( &$sock, $host ) {
720 list( $ip, $port ) = explode( ':', $host );
722 $timeout = $this->_connect_timeout
;
723 $errno = $errstr = null;
724 for( $i = 0; !$sock && $i < $this->_connect_attempts
; $i++
) {
725 wfSuppressWarnings();
726 if ( $this->_persistent
== 1 ) {
727 $sock = pfsockopen( $ip, $port, $errno, $errstr, $timeout );
729 $sock = fsockopen( $ip, $port, $errno, $errstr, $timeout );
734 if ( $this->_debug
) {
735 $this->_debugprint( "Error connecting to $host: $errstr\n" );
740 // Initialise timeout
741 stream_set_timeout( $sock, $this->_timeout_seconds
, $this->_timeout_microseconds
);
750 * Marks a host as dead until 30-40 seconds in the future
752 * @param $sock String: socket to mark as dead
756 function _dead_sock( $sock ) {
757 $host = array_search( $sock, $this->_cache_sock
);
758 $this->_dead_host( $host );
764 function _dead_host( $host ) {
765 $parts = explode( ':', $host );
767 $this->_host_dead
[$ip] = time() +
30 +
intval( rand( 0, 10 ) );
768 $this->_host_dead
[$host] = $this->_host_dead
[$ip];
769 unset( $this->_cache_sock
[$host] );
778 * @param $key String: key to retrieve value for;
780 * @return Mixed: resource on success, false on failure
783 function get_sock( $key ) {
784 if ( !$this->_active
) {
788 if ( $this->_single_sock
!== null ) {
789 $this->_flush_read_buffer( $this->_single_sock
);
790 return $this->sock_to_host( $this->_single_sock
);
793 $hv = is_array( $key ) ?
intval( $key[0] ) : $this->_hashfunc( $key );
794 if ( $this->_buckets
=== null ) {
796 foreach ( $this->_servers
as $v ) {
797 if ( is_array( $v ) ) {
798 for( $i = 0; $i < $v[1]; $i++
) {
805 $this->_buckets
= $bu;
806 $this->_bucketcount
= count( $bu );
809 $realkey = is_array( $key ) ?
$key[1] : $key;
810 for( $tries = 0; $tries < 20; $tries++
) {
811 $host = $this->_buckets
[$hv %
$this->_bucketcount
];
812 $sock = $this->sock_to_host( $host );
813 if ( is_resource( $sock ) ) {
814 $this->_flush_read_buffer( $sock );
817 $hv = $this->_hashfunc( $hv . $realkey );
827 * Creates a hash integer based on the $key
829 * @param $key String: key to hash
831 * @return Integer: hash value
834 function _hashfunc( $key ) {
835 # Hash function must be in [0,0x7ffffff]
836 # We take the first 31 bits of the MD5 hash, which unlike the hash
837 # function used in a previous version of this client, works
838 return hexdec( substr( md5( $key ), 0, 8 ) ) & 0x7fffffff;
845 * Perform increment/decriment on $key
847 * @param $cmd String command to perform
848 * @param $key String|array key to perform it on
849 * @param $amt Integer amount to adjust
851 * @return Integer: new value of $key
854 function _incrdecr( $cmd, $key, $amt = 1 ) {
855 if ( !$this->_active
) {
859 $sock = $this->get_sock( $key );
860 if ( !is_resource( $sock ) ) {
864 $key = is_array( $key ) ?
$key[1] : $key;
865 if ( isset( $this->stats
[$cmd] ) ) {
866 $this->stats
[$cmd]++
;
868 $this->stats
[$cmd] = 1;
870 if ( !$this->_safe_fwrite( $sock, "$cmd $key $amt\r\n" ) ) {
871 $this->_dead_sock( $sock );
875 $line = fgets( $sock );
877 if ( !preg_match( '/^(\d+)/', $line, $match ) ) {
887 * Load items into $ret from $sock
889 * @param $sock Resource: socket to read from
890 * @param $ret Array: returned values
895 function _load_items( $sock, &$ret ) {
897 $decl = fgets( $sock );
898 if ( $decl == "END\r\n" ) {
900 } elseif ( preg_match( '/^VALUE (\S+) (\d+) (\d+)\r\n$/', $decl, $match ) ) {
901 list( $rkey, $flags, $len ) = array( $match[1], $match[2], $match[3] );
905 while ( $bneed > 0 ) {
906 $data = fread( $sock, $bneed );
907 $n = strlen( $data );
913 if ( isset( $ret[$rkey] ) ) {
914 $ret[$rkey] .= $data;
920 if ( $offset != $len +
2 ) {
921 // Something is borked!
922 if ( $this->_debug
) {
923 $this->_debugprint( sprintf( "Something is borked! key %s expecting %d got %d length\n", $rkey, $len +
2, $offset ) );
926 unset( $ret[$rkey] );
927 $this->_close_sock( $sock );
931 if ( $this->_have_zlib
&& $flags & self
::COMPRESSED
) {
932 $ret[$rkey] = gzuncompress( $ret[$rkey] );
935 $ret[$rkey] = rtrim( $ret[$rkey] );
937 if ( $flags & self
::SERIALIZED
) {
938 $ret[$rkey] = unserialize( $ret[$rkey] );
942 $this->_debugprint( "Error parsing memcached response\n" );
952 * Performs the requested storage operation to the memcache server
954 * @param $cmd String: command to perform
955 * @param $key String: key to act on
956 * @param $val Mixed: what we need to store
957 * @param $exp Integer: (optional) Expiration time. This can be a number of seconds
958 * to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
959 * longer must be the timestamp of the time at which the mapping should expire. It
960 * is safe to use timestamps in all cases, regardless of exipration
961 * eg: strtotime("+3 hour")
966 function _set( $cmd, $key, $val, $exp ) {
967 if ( !$this->_active
) {
971 $sock = $this->get_sock( $key );
972 if ( !is_resource( $sock ) ) {
976 if ( isset( $this->stats
[$cmd] ) ) {
977 $this->stats
[$cmd]++
;
979 $this->stats
[$cmd] = 1;
984 if ( !is_scalar( $val ) ) {
985 $val = serialize( $val );
986 $flags |
= self
::SERIALIZED
;
987 if ( $this->_debug
) {
988 $this->_debugprint( sprintf( "client: serializing data as it is not scalar\n" ) );
992 $len = strlen( $val );
994 if ( $this->_have_zlib
&& $this->_compress_enable
&&
995 $this->_compress_threshold
&& $len >= $this->_compress_threshold
)
997 $c_val = gzcompress( $val, 9 );
998 $c_len = strlen( $c_val );
1000 if ( $c_len < $len * ( 1 - self
::COMPRESSION_SAVINGS
) ) {
1001 if ( $this->_debug
) {
1002 $this->_debugprint( sprintf( "client: compressing data; was %d bytes is now %d bytes\n", $len, $c_len ) );
1006 $flags |
= self
::COMPRESSED
;
1009 if ( !$this->_safe_fwrite( $sock, "$cmd $key $flags $exp $len\r\n$val\r\n" ) ) {
1010 $this->_dead_sock( $sock );
1014 $line = trim( fgets( $sock ) );
1016 if ( $this->_debug
) {
1017 $this->_debugprint( sprintf( "%s %s (%s)\n", $cmd, $key, $line ) );
1019 if ( $line == "STORED" ) {
1026 // {{{ sock_to_host()
1029 * Returns the socket for the host
1031 * @param $host String: Host:IP to get socket for
1033 * @return Mixed: IO Stream or false
1036 function sock_to_host( $host ) {
1037 if ( isset( $this->_cache_sock
[$host] ) ) {
1038 return $this->_cache_sock
[$host];
1043 list( $ip, /* $port */) = explode( ':', $host );
1044 if ( isset( $this->_host_dead
[$host] ) && $this->_host_dead
[$host] > $now ||
1045 isset( $this->_host_dead
[$ip] ) && $this->_host_dead
[$ip] > $now
1050 if ( !$this->_connect_sock( $sock, $host ) ) {
1051 $this->_dead_host( $host );
1055 // Do not buffer writes
1056 stream_set_write_buffer( $sock, 0 );
1058 $this->_cache_sock
[$host] = $sock;
1060 return $this->_cache_sock
[$host];
1064 * @param $str string
1066 function _debugprint( $str ) {
1071 * Write to a stream, timing out after the correct amount of time
1073 * @return Boolean: false on failure, true on success
1076 function _safe_fwrite( $f, $buf, $len = false ) {
1077 stream_set_blocking( $f, 0 );
1079 if ( $len === false ) {
1080 wfDebug( "Writing " . strlen( $buf ) . " bytes\n" );
1081 $bytesWritten = fwrite( $f, $buf );
1083 wfDebug( "Writing $len bytes\n" );
1084 $bytesWritten = fwrite( $f, $buf, $len );
1086 $n = stream_select( $r = null, $w = array( $f ), $e = null, 10, 0 );
1087 # $this->_timeout_seconds, $this->_timeout_microseconds );
1089 wfDebug( "stream_select returned $n\n" );
1090 stream_set_blocking( $f, 1 );
1092 return $bytesWritten;
1096 * Original behaviour
1102 function _safe_fwrite( $f, $buf, $len = false ) {
1103 if ( $len === false ) {
1104 $bytesWritten = fwrite( $f, $buf );
1106 $bytesWritten = fwrite( $f, $buf, $len );
1108 return $bytesWritten;
1112 * Flush the read buffer of a stream
1113 * @param $f Resource
1115 function _flush_read_buffer( $f ) {
1116 if ( !is_resource( $f ) ) {
1119 $n = stream_select( $r = array( $f ), $w = null, $e = null, 0, 0 );
1120 while ( $n == 1 && !feof( $f ) ) {
1122 $n = stream_select( $r = array( $f ), $w = null, $e = null, 0, 0 );
1131 // vim: sts=3 sw=3 et
1135 class MemCachedClientforWiki
extends MWMemcached
{
1137 function _debugprint( $text ) {
1138 global $wgDebugLogGroups;
1139 if( !isset( $wgDebugLogGroups['memcached'] ) ) {
1140 # Prefix message since it will end up in main debug log file
1141 $text = "memcached: $text";
1143 wfDebugLog( 'memcached', $text );