MediaWiki  1.23.15
MWMemcached Class Reference

This is the PHP client for memcached - a distributed memory cache daemon. More...

Inheritance diagram for MWMemcached:
Collaboration diagram for MWMemcached:

Public Member Functions

 __construct ( $args)
 Memcache initializer. More...
 
 _close_sock ( $sock)
 Close the specified socket. More...
 
 _connect_sock (&$sock, $host)
 Connects $sock to $host, timing out after $timeout. More...
 
 _dead_host ( $host)
 
 _dead_sock ( $sock)
 Marks a host as dead until 30-40 seconds in the future. More...
 
 _debugprint ( $text)
 
 _error_log ( $text)
 
 _fgets ( $sock)
 Read a line from a stream. More...
 
 _flush_read_buffer ( $f)
 Flush the read buffer of a stream. More...
 
 _fread ( $sock, $len)
 Read the specified number of bytes from a stream. More...
 
 _fwrite ( $sock, $buf)
 Write to a stream. More...
 
 _handle_error ( $sock, $msg)
 Handle an I/O error. More...
 
 _hashfunc ( $key)
 Creates a hash integer based on the $key. More...
 
 _incrdecr ( $cmd, $key, $amt=1)
 Perform increment/decriment on $key. More...
 
 _load_items ( $sock, &$ret, &$casToken=null)
 Load items into $ret from $sock. More...
 
 _set ( $cmd, $key, $val, $exp, $casToken=null)
 Performs the requested storage operation to the memcache server. More...
 
 add ( $key, $val, $exp=0)
 Adds a key/value to the memcache server if one isn't already set with that key. More...
 
 cas ( $casToken, $key, $value, $exp=0)
 Sets a key to a given value in the memcache if the current value still corresponds to a known, given value. More...
 
 decr ( $key, $amt=1)
 Decrease a value stored on the memcache server. More...
 
 delete ( $key, $time=0)
 Deletes a key from the server, optionally after $time. More...
 
 disconnect_all ()
 Disconnects all connected sockets. More...
 
 enable_compress ( $enable)
 Enable / Disable compression. More...
 
 forget_dead_hosts ()
 Forget about all of the dead hosts. More...
 
 get ( $key, &$casToken=null)
 Retrieves the value associated with the key from the memcache server. More...
 
 get_multi ( $keys)
 Get multiple keys from the server(s) More...
 
 get_sock ( $key)
 get_sock More...
 
 incr ( $key, $amt=1)
 Increments $key (optionally) by $amt. More...
 
 lock ( $key, $timeout=0)
 
 replace ( $key, $value, $exp=0)
 Overwrites an existing value for key; only works if key is already set. More...
 
 run_command ( $sock, $cmd)
 Passes through $cmd to the memcache server connected by $sock; returns output as an array (null array if no output) More...
 
 set ( $key, $value, $exp=0)
 Unconditionally sets a key to a given value in the memcache. More...
 
 set_compress_threshold ( $thresh)
 Sets the compression threshold. More...
 
 set_debug ( $dbg)
 Sets the debug flag. More...
 
 set_servers ( $list)
 Sets the server list to distribute key gets and puts between. More...
 
 set_timeout ( $seconds, $microseconds)
 Sets the timeout for new connections. More...
 
 sock_to_host ( $host)
 Returns the socket for the host. More...
 
 unlock ( $key)
 

Public Attributes

integer $_active
 
integer $_bucketcount
 Total # of bit buckets we have. More...
 
array $_buckets
 Our bit buckets. More...
 
array $_cache_sock
 Cached Sockets that are connected. More...
 
boolean $_compress_enable
 Do we want to use compression? More...
 
integer $_compress_threshold
 At how many bytes should we compress? More...
 
 $_connect_attempts
 Number of connection attempts for each server. More...
 
 $_connect_timeout
 Connect timeout in seconds. More...
 
boolean $_debug
 Current debug status; 0 - none to 9 - profiling. More...
 
boolean $_have_zlib
 Is compression available? More...
 
array $_host_dead
 Dead hosts, assoc array, 'host'=>'unixtime when ok to check again'. More...
 
boolean $_persistent
 Are we using persistent links? More...
 
array $_servers
 Array containing ip:port or array(ip:port, weight) More...
 
string $_single_sock
 If only using one server; contains ip:port to connect to. More...
 
integer $_timeout_microseconds
 Stream timeout in microseconds. More...
 
integer $_timeout_seconds
 Stream timeout in seconds. More...
 
array $stats
 Command statistics. More...
 
const COMPRESSED = 2
 Flag: indicates data is compressed. More...
 
const COMPRESSION_SAVINGS = 0.20
 Minimum savings to store data compressed. More...
 
const SERIALIZED = 1
 Flag: indicates data is serialized. More...
 

Detailed Description

This is the PHP client for memcached - a distributed memory cache daemon.

More information is available at http://www.danga.com/memcached/

Usage example:

require_once 'memcached.php';

$mc = new MWMemcached(array( 'servers' => array('127.0.0.1:10000', array('192.0.0.1:10010', 2), '127.0.0.1:10020'), 'debug' => false, 'compress_threshold' => 10240, 'persistent' => true));

$mc->add( 'key', array( 'some', 'array' ) ); $mc->replace( 'key', 'some random string' ); $val = $mc->get( 'key' );

Author
Ryan T. Dean rtdea.nosp@m.n@cy.nosp@m.theri.nosp@m.anag.nosp@m.e.net
Version
0.1.2 memcached client class implemented using (p)fsockopen()
Author
Ryan T. Dean rtdea.nosp@m.n@cy.nosp@m.theri.nosp@m.anag.nosp@m.e.net

Definition at line 76 of file MemcachedClient.php.

Constructor & Destructor Documentation

◆ __construct()

MWMemcached::__construct (   $args)

Memcache initializer.

Parameters
array$argsAssociative array of settings
Returns
mixed

Definition at line 233 of file MemcachedClient.php.

References $args, array(), and set_servers().

Member Function Documentation

◆ _close_sock()

MWMemcached::_close_sock (   $sock)

Close the specified socket.

Parameters
string$socksocket to close
Access:\n private

Definition at line 698 of file MemcachedClient.php.

◆ _connect_sock()

MWMemcached::_connect_sock ( $sock,
  $host 
)

Connects $sock to $host, timing out after $timeout.

Parameters
$sockInteger: socket to connect
string$hostHost:IP to connect to
Returns
boolean
Access:\n private

Definition at line 716 of file MemcachedClient.php.

References $_connect_attempts, $_connect_timeout, _dead_host(), _error_log(), _flush_read_buffer(), list, wfRestoreWarnings(), and wfSuppressWarnings().

Referenced by sock_to_host().

◆ _dead_host()

MWMemcached::_dead_host (   $host)
Parameters
$host

Definition at line 765 of file MemcachedClient.php.

Referenced by _connect_sock(), and _dead_sock().

◆ _dead_sock()

MWMemcached::_dead_sock (   $sock)

Marks a host as dead until 30-40 seconds in the future.

Parameters
string$socksocket to mark as dead
Access:\n private

Definition at line 757 of file MemcachedClient.php.

References _dead_host().

Referenced by _handle_error().

◆ _debugprint()

MWMemcached::_debugprint (   $text)
Parameters
$textstring

Definition at line 1090 of file MemcachedClient.php.

References wfDebugLog().

Referenced by _set(), delete(), get(), and get_multi().

◆ _error_log()

MWMemcached::_error_log (   $text)
Parameters
$textstring

Definition at line 1097 of file MemcachedClient.php.

References wfDebugLog().

Referenced by _connect_sock(), and _handle_error().

◆ _fgets()

MWMemcached::_fgets (   $sock)

Read a line from a stream.

If there is an error, mark the socket dead. The \r
line ending is stripped from the response.

Parameters
$sockThe socket
Returns
The string on success, false on failure

Definition at line 1184 of file MemcachedClient.php.

References _handle_error().

Referenced by _incrdecr(), _load_items(), _set(), delete(), and run_command().

◆ _flush_read_buffer()

MWMemcached::_flush_read_buffer (   $f)

Flush the read buffer of a stream.

Parameters
$fResource

Definition at line 1213 of file MemcachedClient.php.

References $e, $f, $n, and array().

Referenced by _connect_sock().

◆ _fread()

MWMemcached::_fread (   $sock,
  $len 
)

Read the specified number of bytes from a stream.

If there is an error, mark the socket dead.

Parameters
$sockThe socket
$lenThe number of bytes to read
Returns
The string on success, false on failure.

Definition at line 1153 of file MemcachedClient.php.

References _handle_error().

Referenced by _load_items().

◆ _fwrite()

MWMemcached::_fwrite (   $sock,
  $buf 
)

Write to a stream.

If there is an error, mark the socket dead.

Parameters
$sockThe socket
$bufThe string to write
Returns
bool True on success, false on failure

Definition at line 1108 of file MemcachedClient.php.

References _handle_error().

Referenced by _incrdecr(), _set(), delete(), get(), get_multi(), and run_command().

◆ _handle_error()

MWMemcached::_handle_error (   $sock,
  $msg 
)

Handle an I/O error.

Mark the socket dead and log an error.

Definition at line 1132 of file MemcachedClient.php.

References _dead_sock(), and _error_log().

Referenced by _fgets(), _fread(), _fwrite(), and _load_items().

◆ _hashfunc()

MWMemcached::_hashfunc (   $key)

Creates a hash integer based on the $key.

Parameters
string$keykey to hash
Returns
Integer: hash value
Access:\n private

Definition at line 833 of file MemcachedClient.php.

Referenced by get_sock().

◆ _incrdecr()

MWMemcached::_incrdecr (   $cmd,
  $key,
  $amt = 1 
)

Perform increment/decriment on $key.

Parameters
string$cmdcommand to perform
string | array$keykey to perform it on
$amtInteger amount to adjust
Returns
Integer: new value of $key
Access:\n private

Definition at line 853 of file MemcachedClient.php.

References $line, _fgets(), _fwrite(), array(), and get_sock().

Referenced by decr(), and incr().

◆ _load_items()

MWMemcached::_load_items (   $sock,
$ret,
$casToken = null 
)

Load items into $ret from $sock.

Parameters
$sockResource: socket to read from
array$retreturned values
$casToken[optional]Float
Returns
boolean True for success, false for failure
Access:\n private

Definition at line 894 of file MemcachedClient.php.

References $flags, $ret, $vars, _fgets(), _fread(), _handle_error(), array(), as, and list.

Referenced by get(), and get_multi().

◆ _set()

MWMemcached::_set (   $cmd,
  $key,
  $val,
  $exp,
  $casToken = null 
)

Performs the requested storage operation to the memcache server.

Parameters
string$cmdcommand to perform
string$keykey to act on
$valMixed: what we need to store
$expInteger: (optional) Expiration time. This can be a number of seconds to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or longer must be the timestamp of the time at which the mapping should expire. It is safe to use timestamps in all cases, regardless of exipration eg: strtotime("+3 hour")
$casToken[optional]Float
Returns
Boolean
Access:\n private

Definition at line 986 of file MemcachedClient.php.

References $command, $flags, $line, _debugprint(), _fgets(), _fwrite(), COMPRESSED, get_sock(), and SERIALIZED.

Referenced by add(), cas(), replace(), and set().

◆ add()

MWMemcached::add (   $key,
  $val,
  $exp = 0 
)

Adds a key/value to the memcache server if one isn't already set with that key.

Parameters
string$keykey to set with data
$valMixed: value to store
$expInteger: (optional) Expiration time. This can be a number of seconds to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or longer must be the timestamp of the time at which the mapping should expire. It is safe to use timestamps in all cases, regardless of expiration eg: strtotime("+3 hour")
Returns
Boolean

Definition at line 269 of file MemcachedClient.php.

References _set().

◆ cas()

MWMemcached::cas (   $casToken,
  $key,
  $value,
  $exp = 0 
)

Sets a key to a given value in the memcache if the current value still corresponds to a known, given value.

Returns true if set successfully.

Parameters
$casTokenFloat: current known value
string$keykey to set value as
$valueMixed: value to set
$expInteger: (optional) Expiration time. This can be a number of seconds to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or longer must be the timestamp of the time at which the mapping should expire. It is safe to use timestamps in all cases, regardless of exipration eg: strtotime("+3 hour")
Returns
Boolean: TRUE on success

Definition at line 623 of file MemcachedClient.php.

References $value, and _set().

◆ decr()

MWMemcached::decr (   $key,
  $amt = 1 
)

Decrease a value stored on the memcache server.

Parameters
string$keykey to decrease
$amtInteger: (optional) amount to decrease
Returns
Mixed: FALSE on failure, value on success

Definition at line 284 of file MemcachedClient.php.

References _incrdecr().

◆ delete()

MWMemcached::delete (   $key,
  $time = 0 
)

Deletes a key from the server, optionally after $time.

Parameters
string$keykey to delete
$timeInteger: (optional) how long to wait before deleting
Returns
Boolean: TRUE on success, FALSE on failure

Definition at line 299 of file MemcachedClient.php.

References $res, _debugprint(), _fgets(), _fwrite(), and get_sock().

◆ disconnect_all()

MWMemcached::disconnect_all ( )

Disconnects all connected sockets.

Definition at line 358 of file MemcachedClient.php.

References array(), and as.

◆ enable_compress()

MWMemcached::enable_compress (   $enable)

Enable / Disable compression.

Parameters
$enableBoolean: TRUE to enable, FALSE to disable

Definition at line 374 of file MemcachedClient.php.

◆ forget_dead_hosts()

MWMemcached::forget_dead_hosts ( )

Forget about all of the dead hosts.

Definition at line 384 of file MemcachedClient.php.

References array().

◆ get()

MWMemcached::get (   $key,
$casToken = null 
)

Retrieves the value associated with the key from the memcache server.

Parameters
array | string$keykey to retrieve
$casToken[optional]Float
Returns
Mixed

Definition at line 399 of file MemcachedClient.php.

References $value, _debugprint(), _fwrite(), _load_items(), array(), as, get_sock(), wfProfileIn(), and wfProfileOut().

◆ get_multi()

MWMemcached::get_multi (   $keys)

Get multiple keys from the server(s)

Parameters
array$keyskeys to retrieve
Returns
Array

Definition at line 458 of file MemcachedClient.php.

References $keys, _debugprint(), _fwrite(), _load_items(), array(), as, and get_sock().

◆ get_sock()

MWMemcached::get_sock (   $key)

get_sock

Parameters
string$keykey to retrieve value for;
Returns
Mixed: resource on success, false on failure
Access:\n private

Definition at line 784 of file MemcachedClient.php.

References $_bucketcount, _hashfunc(), array(), as, and sock_to_host().

Referenced by _incrdecr(), _set(), delete(), get(), and get_multi().

◆ incr()

MWMemcached::incr (   $key,
  $amt = 1 
)

Increments $key (optionally) by $amt.

Parameters
string$keykey to increment
$amtInteger: (optional) amount to increment
Returns
Integer: null if the key does not exist yet (this does NOT create new mappings if the key does not exist). If the key does exist, this returns the new value for that key.

Definition at line 525 of file MemcachedClient.php.

References _incrdecr().

◆ lock()

MWMemcached::lock (   $key,
  $timeout = 0 
)
Parameters
$key
$timeoutint
Returns
bool

Definition at line 338 of file MemcachedClient.php.

◆ replace()

MWMemcached::replace (   $key,
  $value,
  $exp = 0 
)

Overwrites an existing value for key; only works if key is already set.

Parameters
string$keykey to set value as
$valueMixed: value to store
$expInteger: (optional) Expiration time. This can be a number of seconds to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or longer must be the timestamp of the time at which the mapping should expire. It is safe to use timestamps in all cases, regardless of exipration eg: strtotime("+3 hour")
Returns
Boolean

Definition at line 545 of file MemcachedClient.php.

References $value, and _set().

◆ run_command()

MWMemcached::run_command (   $sock,
  $cmd 
)

Passes through $cmd to the memcache server connected by $sock; returns output as an array (null array if no output)

Parameters
$sockResource: socket to send command on
string$cmdcommand to run
Returns
Array: output array

Definition at line 561 of file MemcachedClient.php.

References $res, $ret, _fgets(), _fwrite(), and array().

◆ set()

MWMemcached::set (   $key,
  $value,
  $exp = 0 
)

Unconditionally sets a key to a given value in the memcache.

Returns true if set successfully.

Parameters
string$keykey to set value as
$valueMixed: value to set
$expInteger: (optional) Expiration time. This can be a number of seconds to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or longer must be the timestamp of the time at which the mapping should expire. It is safe to use timestamps in all cases, regardless of exipration eg: strtotime("+3 hour")
Returns
Boolean: TRUE on success

Definition at line 601 of file MemcachedClient.php.

References $value, and _set().

◆ set_compress_threshold()

MWMemcached::set_compress_threshold (   $thresh)

Sets the compression threshold.

Parameters
$threshInteger: threshold to compress if larger than

Definition at line 635 of file MemcachedClient.php.

◆ set_debug()

MWMemcached::set_debug (   $dbg)

Sets the debug flag.

Parameters
$dbgBoolean: TRUE for debugging, FALSE otherwise
See also
MWMemcached::__construct

Definition at line 649 of file MemcachedClient.php.

◆ set_servers()

MWMemcached::set_servers (   $list)

Sets the server list to distribute key gets and puts between.

Parameters
array$listof servers to connect to
See also
MWMemcached::__construct()

Definition at line 663 of file MemcachedClient.php.

Referenced by __construct().

◆ set_timeout()

MWMemcached::set_timeout (   $seconds,
  $microseconds 
)

Sets the timeout for new connections.

Parameters
$secondsInteger: number of seconds
$microsecondsInteger: number of microseconds

Definition at line 681 of file MemcachedClient.php.

◆ sock_to_host()

MWMemcached::sock_to_host (   $host)

Returns the socket for the host.

Parameters
string$hostHost:IP to get socket for
Returns
Mixed: IO Stream or false
Access:\n private

Definition at line 1061 of file MemcachedClient.php.

References _connect_sock(), and list.

Referenced by get_sock().

◆ unlock()

MWMemcached::unlock (   $key)
Parameters
$key
Returns
bool

Definition at line 347 of file MemcachedClient.php.

Member Data Documentation

◆ $_active

integer MWMemcached::$_active

of total servers we have

Access:\n private

Definition at line 194 of file MemcachedClient.php.

◆ $_bucketcount

integer MWMemcached::$_bucketcount

Total # of bit buckets we have.

Access:\n private

Definition at line 187 of file MemcachedClient.php.

Referenced by get_sock().

◆ $_buckets

array MWMemcached::$_buckets

Our bit buckets.

Access:\n private

Definition at line 180 of file MemcachedClient.php.

◆ $_cache_sock

array MWMemcached::$_cache_sock

Cached Sockets that are connected.

Access:\n private

Definition at line 117 of file MemcachedClient.php.

◆ $_compress_enable

boolean MWMemcached::$_compress_enable

Do we want to use compression?

Access:\n private

Definition at line 145 of file MemcachedClient.php.

◆ $_compress_threshold

integer MWMemcached::$_compress_threshold

At how many bytes should we compress?

Access:\n private

Definition at line 152 of file MemcachedClient.php.

◆ $_connect_attempts

MWMemcached::$_connect_attempts

Number of connection attempts for each server.

Definition at line 218 of file MemcachedClient.php.

Referenced by _connect_sock().

◆ $_connect_timeout

MWMemcached::$_connect_timeout

Connect timeout in seconds.

Definition at line 213 of file MemcachedClient.php.

Referenced by _connect_sock().

◆ $_debug

boolean MWMemcached::$_debug

Current debug status; 0 - none to 9 - profiling.

Access:\n private

Definition at line 124 of file MemcachedClient.php.

◆ $_have_zlib

boolean MWMemcached::$_have_zlib

Is compression available?

Access:\n private

Definition at line 138 of file MemcachedClient.php.

◆ $_host_dead

array MWMemcached::$_host_dead

Dead hosts, assoc array, 'host'=>'unixtime when ok to check again'.

Access:\n private

Definition at line 131 of file MemcachedClient.php.

◆ $_persistent

boolean MWMemcached::$_persistent

Are we using persistent links?

Access:\n private

Definition at line 159 of file MemcachedClient.php.

◆ $_servers

array MWMemcached::$_servers

Array containing ip:port or array(ip:port, weight)

Access:\n private

Definition at line 173 of file MemcachedClient.php.

◆ $_single_sock

string MWMemcached::$_single_sock

If only using one server; contains ip:port to connect to.

Access:\n private

Definition at line 166 of file MemcachedClient.php.

◆ $_timeout_microseconds

integer MWMemcached::$_timeout_microseconds

Stream timeout in microseconds.

Access:\n private

Definition at line 208 of file MemcachedClient.php.

◆ $_timeout_seconds

integer MWMemcached::$_timeout_seconds

Stream timeout in seconds.

Applies for example to fread()

Access:\n private

Definition at line 201 of file MemcachedClient.php.

◆ $stats

array MWMemcached::$stats

Command statistics.

Access:\n public

Definition at line 107 of file MemcachedClient.php.

◆ COMPRESSED

const MWMemcached::COMPRESSED = 2

Flag: indicates data is compressed.

Definition at line 91 of file MemcachedClient.php.

Referenced by _set().

◆ COMPRESSION_SAVINGS

const MWMemcached::COMPRESSION_SAVINGS = 0.20

Minimum savings to store data compressed.

Definition at line 98 of file MemcachedClient.php.

◆ SERIALIZED

const MWMemcached::SERIALIZED = 1

Flag: indicates data is serialized.

Definition at line 86 of file MemcachedClient.php.

Referenced by _set().


The documentation for this class was generated from the following file: