MediaWiki  REL1_19
MWMemcached Class Reference

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

Inheritance diagram for MWMemcached:

List of all members.

Public Member Functions

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

Public Attributes

 $_active
 $_bucketcount
 $_buckets
 $_cache_sock
 $_compress_enable
 $_compress_threshold
 $_connect_attempts
 Number of connection attempts for each server.
 $_connect_timeout
 Connect timeout in seconds.
 $_debug
 $_have_zlib
 $_host_dead
 $_persistent
 $_servers
 $_single_sock
 $_timeout_microseconds
 $_timeout_seconds
 $stats
const COMPRESSED = 2
 Flag: indicates data is compressed.
const COMPRESSION_SAVINGS = 0.20
 Minimum savings to store data compressed.
const SERIALIZED = 1
 Flag: indicates data is serialized.

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 <[email protected]>
Version:
0.1.2 memcached client class implemented using (p)fsockopen()
Author:
Ryan T. Dean <[email protected]>

Definition at line 74 of file MemcachedClient.php.


Constructor & Destructor Documentation

Memcache initializer.

Parameters:
$argsArray Associative array of settings
Returns:
mixed

Definition at line 247 of file MemcachedClient.php.

References set_servers().

Here is the call graph for this function:


Member Function Documentation

Close the specified socket.

Parameters:
$sockString: socket to close
Access:
private

Definition at line 687 of file MemcachedClient.php.

Referenced by _load_items().

Here is the caller graph for this function:

MWMemcached::_connect_sock ( &$  sock,
host 
)

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

Parameters:
$sockInteger: socket to connect
$hostString: Host:IP to connect to
Returns:
boolean
Access:
private

Definition at line 705 of file MemcachedClient.php.

References _debugprint(), wfRestoreWarnings(), and wfSuppressWarnings().

Referenced by sock_to_host().

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 747 of file MemcachedClient.php.

Referenced by _dead_sock(), and sock_to_host().

Here is the caller graph for this function:

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

Parameters:
$sockString: socket to mark as dead
Access:
private

Definition at line 742 of file MemcachedClient.php.

References _dead_host().

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

Here is the call graph for this function:

Here is the caller graph for this function:

Reimplemented in MemCachedClientforWiki.

Definition at line 1051 of file MemcachedClient.php.

References print.

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

Here is the caller graph for this function:

Flush the read buffer of a stream.

Definition at line 1095 of file MemcachedClient.php.

References $f, and $n.

Referenced by get_sock().

Here is the caller graph for this function:

Creates a hash integer based on the $key.

Parameters:
$keyString: key to hash
Returns:
Integer: hash value
Access:
private

Definition at line 817 of file MemcachedClient.php.

Referenced by get_sock().

Here is the caller graph for this function:

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

Perform increment/decriment on $key.

Parameters:
$cmdString command to perform
$keyString|array key to perform it on
$amtInteger amount to adjust
Returns:
Integer: new value of $key
Access:
private

Definition at line 837 of file MemcachedClient.php.

References _dead_sock(), _safe_fwrite(), and get_sock().

Referenced by decr(), and incr().

Here is the call graph for this function:

Here is the caller graph for this function:

MWMemcached::_load_items ( sock,
&$  ret 
)

Load items into $ret from $sock.

Parameters:
$sockResource: socket to read from
$retArray: returned values
Access:
private

Definition at line 876 of file MemcachedClient.php.

References $n, _close_sock(), and _debugprint().

Referenced by get(), and get_multi().

Here is the call graph for this function:

Here is the caller graph for this function:

MWMemcached::_safe_fwrite ( f,
buf,
len = false 
)

Write to a stream, timing out after the correct amount of time.

Returns:
Boolean: false on failure, true on success Original behaviour

Definition at line 1083 of file MemcachedClient.php.

References $f.

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

Here is the caller graph for this function:

MWMemcached::_set ( cmd,
key,
val,
exp 
)

Performs the requested storage operation to the memcache server.

Parameters:
$cmdString: command to perform
$keyString: key 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")
Returns:
Boolean
Access:
private

Definition at line 947 of file MemcachedClient.php.

References _dead_sock(), _debugprint(), _safe_fwrite(), COMPRESSED, get_sock(), and SERIALIZED.

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

Here is the call graph for this function:

Here is the caller graph for this function:

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

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

Parameters:
$keyString: key 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 exipration eg: strtotime("+3 hour")
Returns:
Boolean

Definition at line 283 of file MemcachedClient.php.

References _set().

Here is the call graph for this function:

MWMemcached::decr ( key,
amt = 1 
)

Decrease a value stored on the memcache server.

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

Definition at line 298 of file MemcachedClient.php.

References _incrdecr().

Here is the call graph for this function:

MWMemcached::delete ( key,
time = 0 
)

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

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

Definition at line 313 of file MemcachedClient.php.

References $res, _dead_sock(), _debugprint(), _safe_fwrite(), and get_sock().

Here is the call graph for this function:

Disconnects all connected sockets.

Definition at line 363 of file MemcachedClient.php.

Enable / Disable compression.

Parameters:
$enableBoolean: TRUE to enable, FALSE to disable

Definition at line 379 of file MemcachedClient.php.

Forget about all of the dead hosts.

Definition at line 389 of file MemcachedClient.php.

MWMemcached::get ( key)

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

Parameters:
$keyarray|string key to retrieve
Returns:
Mixed

Definition at line 403 of file MemcachedClient.php.

References _dead_sock(), _debugprint(), _load_items(), _safe_fwrite(), get_sock(), wfProfileIn(), and wfProfileOut().

Here is the call graph for this function:

Get multiple keys from the server(s)

Parameters:
$keysArray: keys to retrieve
Returns:
Array

Definition at line 463 of file MemcachedClient.php.

References $keys, _dead_sock(), _debugprint(), _load_items(), _safe_fwrite(), and get_sock().

Here is the call graph for this function:

get_sock

Parameters:
$keyString: key to retrieve value for;
Returns:
Mixed: resource on success, false on failure
Access:
private

Definition at line 766 of file MemcachedClient.php.

References _flush_read_buffer(), _hashfunc(), and sock_to_host().

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

Here is the call graph for this function:

Here is the caller graph for this function:

MWMemcached::incr ( key,
amt = 1 
)

Increments $key (optionally) by $amt.

Parameters:
$keyString: key 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 531 of file MemcachedClient.php.

References _incrdecr().

Here is the call graph for this function:

MWMemcached::lock ( key,
timeout = 0 
)

Definition at line 347 of file MemcachedClient.php.

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

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

Parameters:
$keyString: key 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 551 of file MemcachedClient.php.

References _set().

Here is the call graph for this function:

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)

NOTE: due to a possible bug in how PHP reads while using fgets(), each line may not be terminated by a
. More specifically, my testing has shown that, on FreeBSD at least, each line is terminated only with a
. This is with the PHP flag auto_detect_line_endings set to falase (the default).

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

Definition at line 573 of file MemcachedClient.php.

References $res, and _safe_fwrite().

Here is the call graph for this function:

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

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

Returns true if set successfully.

Parameters:
$keyString: key 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 612 of file MemcachedClient.php.

References _set().

Here is the call graph for this function:

Sets the compression threshold.

Parameters:
$threshInteger: threshold to compress if larger than

Definition at line 624 of file MemcachedClient.php.

Sets the debug flag.

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

Definition at line 638 of file MemcachedClient.php.

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

Parameters:
$listArray of servers to connect to
See also:
MWMemcached::__construct()

Definition at line 652 of file MemcachedClient.php.

Referenced by __construct().

Here is the caller graph for this function:

MWMemcached::set_timeout ( seconds,
microseconds 
)

Sets the timeout for new connections.

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

Definition at line 670 of file MemcachedClient.php.

Returns the socket for the host.

Parameters:
$hostString: Host:IP to get socket for
Returns:
Mixed: IO Stream or false
Access:
private

Definition at line 1025 of file MemcachedClient.php.

References _connect_sock(), and _dead_host().

Referenced by get_sock().

Here is the call graph for this function:

Here is the caller graph for this function:

MWMemcached::unlock ( key)

Definition at line 352 of file MemcachedClient.php.


Member Data Documentation

MWMemcached::$_active

Definition at line 206 of file MemcachedClient.php.

MWMemcached::$_bucketcount

Definition at line 198 of file MemcachedClient.php.

MWMemcached::$_buckets

Definition at line 190 of file MemcachedClient.php.

MWMemcached::$_cache_sock

Definition at line 118 of file MemcachedClient.php.

MWMemcached::$_compress_enable

Definition at line 150 of file MemcachedClient.php.

MWMemcached::$_compress_threshold

Definition at line 158 of file MemcachedClient.php.

MWMemcached::$_connect_attempts

Number of connection attempts for each server.

Definition at line 232 of file MemcachedClient.php.

MWMemcached::$_connect_timeout

Connect timeout in seconds.

Definition at line 227 of file MemcachedClient.php.

MWMemcached::$_debug

Definition at line 126 of file MemcachedClient.php.

MWMemcached::$_have_zlib

Definition at line 142 of file MemcachedClient.php.

MWMemcached::$_host_dead

Definition at line 134 of file MemcachedClient.php.

MWMemcached::$_persistent

Definition at line 166 of file MemcachedClient.php.

MWMemcached::$_servers

Definition at line 182 of file MemcachedClient.php.

MWMemcached::$_single_sock

Definition at line 174 of file MemcachedClient.php.

MWMemcached::$_timeout_microseconds

Definition at line 222 of file MemcachedClient.php.

MWMemcached::$_timeout_seconds

Definition at line 214 of file MemcachedClient.php.

MWMemcached::$stats

Definition at line 107 of file MemcachedClient.php.

Flag: indicates data is compressed.

Definition at line 89 of file MemcachedClient.php.

Referenced by _set().

Minimum savings to store data compressed.

Definition at line 96 of file MemcachedClient.php.

Flag: indicates data is serialized.

Definition at line 84 of file MemcachedClient.php.

Referenced by _set().


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