MediaWiki  REL1_24
MWMemcached Class Reference

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

Inheritance diagram for MWMemcached:
Collaboration 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 ($text)
 _error_log ($text)
 _fgets ($sock)
 Read a line from a stream.
 _flush_read_buffer ($f)
 Flush the read buffer of a stream.
 _fread ($sock, $len)
 Read the specified number of bytes from a stream.
 _fwrite ($sock, $buf)
 Write to a stream.
 _handle_error ($sock, $msg)
 Handle an I/O error.
 _hashfunc ($key)
 Creates a hash integer based on the $key.
 _incrdecr ($cmd, $key, $amt=1)
 Perform increment/decriment on $key.
 _load_items ($sock, &$ret, &$casToken=null)
 Load items into $ret from $sock.
 _set ($cmd, $key, $val, $exp, $casToken=null)
 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.
 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.
 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, &$casToken=null)
 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

int $_active
 # of total servers we have
int $_bucketcount
 Total # of bit buckets we have.
array $_buckets
 Our bit buckets.
array $_cache_sock
 Cached Sockets that are connected.
bool $_compress_enable
 Do we want to use compression?
int $_compress_threshold
 At how many bytes should we compress?
 $_connect_attempts
 Number of connection attempts for each server.
 $_connect_timeout
 Connect timeout in seconds.
bool $_debug
 Current debug status; 0 - none to 9 - profiling.
bool $_have_zlib
 Is compression available?
array $_host_dead
 Dead hosts, assoc array, 'host'=>'unixtime when ok to check again'.
bool $_persistent
 Are we using persistent links?
array $_servers
 Array containing ip:port or array(ip:port, weight)
string $_single_sock
 If only using one server; contains ip:port to connect to.
int $_timeout_microseconds
 Stream timeout in microseconds.
int $_timeout_seconds
 Stream timeout in seconds.
array $stats
 Command statistics.
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 77 of file MemcachedClient.php.


Constructor & Destructor Documentation

Memcache initializer.

Parameters:
array$argsAssociative array of settings
Returns:
mixed

Definition at line 234 of file MemcachedClient.php.

References array(), and set_servers().


Member Function Documentation

Close the specified socket.

Parameters:
string$sockSocket to close
Access:
private

Definition at line 705 of file MemcachedClient.php.

MWMemcached::_connect_sock ( &$  sock,
host 
)

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

Parameters:
int$sockSocket to connect
string$hostHost:IP to connect to
Returns:
bool
Access:
private

Definition at line 723 of file MemcachedClient.php.

Parameters:
string$host

Definition at line 772 of file MemcachedClient.php.

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

Parameters:
string$sockSocket to mark as dead
Access:
private

Definition at line 764 of file MemcachedClient.php.

Parameters:
string$text

Definition at line 1097 of file MemcachedClient.php.

Parameters:
string$text

Definition at line 1104 of file MemcachedClient.php.

MWMemcached::_fgets ( sock)

Read a line from a stream.

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

Parameters:
Resource$sockThe socket
Returns:
string|bool The string on success, false on failure

Definition at line 1194 of file MemcachedClient.php.

Flush the read buffer of a stream.

Parameters:
Resource$f

Definition at line 1223 of file MemcachedClient.php.

References $result.

MWMemcached::_fread ( sock,
len 
)

Read the specified number of bytes from a stream.

If there is an error, mark the socket dead.

Parameters:
Resource$sockThe socket
int$lenThe number of bytes to read
Returns:
string|bool The string on success, false on failure.

Definition at line 1163 of file MemcachedClient.php.

MWMemcached::_fwrite ( sock,
buf 
)

Write to a stream.

If there is an error, mark the socket dead.

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

Definition at line 1115 of file MemcachedClient.php.

References wfDebugLog().

MWMemcached::_handle_error ( sock,
msg 
)

Handle an I/O error.

Mark the socket dead and log an error.

Parameters:
Resource$sock
string$msg

Definition at line 1142 of file MemcachedClient.php.

Creates a hash integer based on the $key.

Parameters:
string$keyKey to hash
Returns:
int Hash value
Access:
private

Definition at line 840 of file MemcachedClient.php.

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

Perform increment/decriment on $key.

Parameters:
string$cmdCommand to perform
string | array$keyKey to perform it on
int$amtAmount to adjust
Returns:
int New value of $key
Access:
private

Definition at line 860 of file MemcachedClient.php.

Referenced by delete().

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

Load items into $ret from $sock.

Parameters:
Resource$sockSocket to read from
array$retreturned values
float$casToken[optional]
Returns:
bool True for success, false for failure
Access:
private

Definition at line 901 of file MemcachedClient.php.

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
mixed$valWhat we need to store
int$exp(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")
float$casToken[optional]
Returns:
bool
Access:
private

Definition at line 993 of file MemcachedClient.php.

Referenced by decr(), and set().

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
mixed$valValue to store
int$exp(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:
bool

Definition at line 270 of file MemcachedClient.php.

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:
float$casTokenCurrent known value
string$keyKey to set value as
mixed$valueValue to set
int$exp(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:
bool True on success

Definition at line 630 of file MemcachedClient.php.

MWMemcached::decr ( key,
amt = 1 
)

Decrease a value stored on the memcache server.

Parameters:
string$keyKey to decrease
int$amt(optional) amount to decrease
Returns:
mixed False on failure, value on success

Definition at line 285 of file MemcachedClient.php.

References $key, and _set().

MWMemcached::delete ( key,
time = 0 
)

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

Parameters:
string$keyKey to delete
int$time(optional) how long to wait before deleting
Returns:
bool True on success, false on failure

Definition at line 300 of file MemcachedClient.php.

References $key, and _incrdecr().

Disconnects all connected sockets.

Definition at line 359 of file MemcachedClient.php.

Enable / Disable compression.

Parameters:
bool$enableTrue to enable, false to disable

Definition at line 375 of file MemcachedClient.php.

Forget about all of the dead hosts.

Definition at line 385 of file MemcachedClient.php.

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

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

Parameters:
array | string$keykey to retrieve
float$casToken[optional]
Returns:
mixed

Definition at line 400 of file MemcachedClient.php.

References array().

Get multiple keys from the server(s)

Parameters:
array$keysKeys to retrieve
Returns:
array

Definition at line 465 of file MemcachedClient.php.

get_sock

Parameters:
string$keyKey to retrieve value for;
Returns:
Resource|bool Resource on success, false on failure
Access:
private

Definition at line 791 of file MemcachedClient.php.

MWMemcached::incr ( key,
amt = 1 
)

Increments $key (optionally) by $amt.

Parameters:
string$keyKey to increment
int$amt(optional) amount to increment
Returns:
int|null 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 532 of file MemcachedClient.php.

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

Definition at line 339 of file MemcachedClient.php.

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
mixed$valueValue to store
int$exp(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:
bool

Definition at line 552 of file MemcachedClient.php.

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:
Resource$sockSocket to send command on
string$cmdCommand to run
Returns:
array Output array

Definition at line 568 of file MemcachedClient.php.

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
mixed$valueValue to set
int$exp(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:
bool True on success

Definition at line 608 of file MemcachedClient.php.

References $value, and _set().

Sets the compression threshold.

Parameters:
int$threshThreshold to compress if larger than

Definition at line 642 of file MemcachedClient.php.

Sets the debug flag.

Parameters:
bool$dbgTrue for debugging, false otherwise
See also:
MWMemcached::__construct

Definition at line 656 of file MemcachedClient.php.

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

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

Definition at line 670 of file MemcachedClient.php.

Referenced by __construct().

MWMemcached::set_timeout ( seconds,
microseconds 
)

Sets the timeout for new connections.

Parameters:
int$secondsNumber of seconds
int$microsecondsNumber of microseconds

Definition at line 688 of file MemcachedClient.php.

Returns the socket for the host.

Parameters:
string$hostHost:IP to get socket for
Returns:
Resource|bool IO Stream or false
Access:
private

Definition at line 1068 of file MemcachedClient.php.

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

Definition at line 348 of file MemcachedClient.php.


Member Data Documentation

int MWMemcached::$_active

# of total servers we have

Access:
private

Definition at line 195 of file MemcachedClient.php.

int MWMemcached::$_bucketcount

Total # of bit buckets we have.

Access:
private

Definition at line 188 of file MemcachedClient.php.

array MWMemcached::$_buckets

Our bit buckets.

Access:
private

Definition at line 181 of file MemcachedClient.php.

array MWMemcached::$_cache_sock

Cached Sockets that are connected.

Access:
private

Definition at line 118 of file MemcachedClient.php.

bool MWMemcached::$_compress_enable

Do we want to use compression?

Access:
private

Definition at line 146 of file MemcachedClient.php.

int MWMemcached::$_compress_threshold

At how many bytes should we compress?

Access:
private

Definition at line 153 of file MemcachedClient.php.

MWMemcached::$_connect_attempts

Number of connection attempts for each server.

Definition at line 219 of file MemcachedClient.php.

MWMemcached::$_connect_timeout

Connect timeout in seconds.

Definition at line 214 of file MemcachedClient.php.

bool MWMemcached::$_debug

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

Access:
private

Definition at line 125 of file MemcachedClient.php.

bool MWMemcached::$_have_zlib

Is compression available?

Access:
private

Definition at line 139 of file MemcachedClient.php.

array MWMemcached::$_host_dead

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

Access:
private

Definition at line 132 of file MemcachedClient.php.

bool MWMemcached::$_persistent

Are we using persistent links?

Access:
private

Definition at line 160 of file MemcachedClient.php.

array MWMemcached::$_servers

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

Access:
private

Definition at line 174 of file MemcachedClient.php.

string MWMemcached::$_single_sock

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

Access:
private

Definition at line 167 of file MemcachedClient.php.

int MWMemcached::$_timeout_microseconds

Stream timeout in microseconds.

Access:
private

Definition at line 209 of file MemcachedClient.php.

int MWMemcached::$_timeout_seconds

Stream timeout in seconds.

Applies for example to fread()

Access:
private

Definition at line 202 of file MemcachedClient.php.

array MWMemcached::$stats

Command statistics.

Access:
public

Definition at line 108 of file MemcachedClient.php.

Flag: indicates data is compressed.

Definition at line 92 of file MemcachedClient.php.

Minimum savings to store data compressed.

Definition at line 99 of file MemcachedClient.php.

Flag: indicates data is serialized.

Definition at line 87 of file MemcachedClient.php.


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