memcached Class Reference
[Cache]

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

List of all members.

Public Member Functions

 memcached ($args)
 Memcache initializer.
 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)
 Decriment 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).
 incr ($key, $amt=1)
 Increments $key (optionally) by $amt.
 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.
 _close_sock ($sock)
 Close the specified socket.
 _connect_sock (&$sock, $host)
 Connects $sock to $host, timing out after $timeout.
 _dead_sock ($sock)
 Marks a host as dead until 30-40 seconds in the future.
 get_sock ($key)
 get_sock
 _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.
 _set ($cmd, $key, $val, $exp)
 Performs the requested storage operation to the memcache server.
 sock_to_host ($host)
 Returns the socket for the host.
 _debugprint ($str)
 _safe_fwrite ($f, $buf, $len=false)
 Write to a stream, timing out after the correct amount of time.
 _flush_read_buffer ($f)
 Flush the read buffer of a stream.

Public Attributes

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


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 memcached(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, 'persistant' => true));

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

Author:
Ryan T. Dean <rtdean@cytherianage.net>
Version:
0.1.2 memcached client class implemented using (p)fsockopen()
Author:
Ryan T. Dean <rtdean@cytherianage.net>

Definition at line 73 of file memcached-client.php.


Member Function Documentation

memcached::_close_sock ( sock  ) 

Close the specified socket.

Parameters:
string $sock Socket to close
Access:
private

Definition at line 681 of file memcached-client.php.

Referenced by _load_items().

memcached::_connect_sock ( &$  sock,
host 
)

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

Parameters:
integer $sock Socket to connect
string $host Host:IP to connect to
Returns:
boolean
Access:
private

Definition at line 700 of file memcached-client.php.

References $i, $t, and _debugprint().

Referenced by sock_to_host().

memcached::_dead_sock ( sock  ) 

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

Parameters:
string $sock Socket to mark as dead
Access:
private

Definition at line 746 of file memcached-client.php.

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

memcached::_debugprint ( str  ) 

Definition at line 1027 of file memcached-client.php.

References print.

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

memcached::_flush_read_buffer ( f  ) 

Flush the read buffer of a stream.

Definition at line 1071 of file memcached-client.php.

References $n.

Referenced by get_sock().

memcached::_hashfunc ( key  ) 

Creates a hash integer based on the $key.

Parameters:
string $key Key to hash
Returns:
integer Hash value
Access:
private

Definition at line 821 of file memcached-client.php.

References $key.

Referenced by get_sock().

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

Perform increment/decriment on $key.

Parameters:
string $cmd Command to perform
string $key Key to perform it on
integer $amt Amount to adjust
Returns:
integer New value of $key
Access:
private

Definition at line 842 of file memcached-client.php.

References $cmd, $key, _dead_sock(), _safe_fwrite(), and get_sock().

Referenced by decr(), and incr().

memcached::_load_items ( sock,
&$  ret 
)

Load items into $ret from $sock.

Parameters:
resource $sock Socket to read from
array $ret Returned values
Access:
private

Definition at line 875 of file memcached-client.php.

References $flags, $n, $offset, _close_sock(), _debugprint(), COMPRESSED, and SERIALIZED.

Referenced by get(), and get_multi().

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

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

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

Definition at line 1059 of file memcached-client.php.

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

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

Performs the requested storage operation to the memcache server.

Parameters:
string $cmd Command to perform
string $key Key to act on
mixed $val What we need to store
integer $exp When it should expire
Returns:
boolean
Access:
private

Definition at line 941 of file memcached-client.php.

References $cmd, $flags, $key, _dead_sock(), _debugprint(), _safe_fwrite(), COMPRESSED, COMPRESSION_SAVINGS, get_sock(), and SERIALIZED.

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

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

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

Parameters:
string $key Key to set with data
mixed $val Value to store
integer $exp (optional) Time to expire data at
Returns:
boolean
Access:
public

Definition at line 282 of file memcached-client.php.

References $key, and _set().

memcached::decr ( key,
amt = 1 
)

Decriment a value stored on the memcache server.

Parameters:
string $key Key to decriment
integer $amt (optional) Amount to decriment
Returns:
mixed FALSE on failure, value on success
Access:
public

Definition at line 299 of file memcached-client.php.

References $key, and _incrdecr().

memcached::delete ( key,
time = 0 
)

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

Parameters:
string $key Key to delete
integer $time (optional) How long to wait before deleting
Returns:
boolean TRUE on success, FALSE on failure
Access:
public

Definition at line 316 of file memcached-client.php.

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

memcached::disconnect_all (  ) 

Disconnects all connected sockets.

Access:
public

Definition at line 352 of file memcached-client.php.

memcached::enable_compress ( enable  ) 

Enable / Disable compression.

Parameters:
boolean $enable TRUE to enable, FALSE to disable
Access:
public

Definition at line 370 of file memcached-client.php.

memcached::forget_dead_hosts (  ) 

Forget about all of the dead hosts.

Access:
public

Definition at line 383 of file memcached-client.php.

memcached::get ( key  ) 

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

Parameters:
string $key Key to retrieve
Returns:
mixed
Access:
public

Definition at line 399 of file memcached-client.php.

References $cmd, $fname, $key, _dead_sock(), _debugprint(), _load_items(), _safe_fwrite(), get_sock(), wfProfileIn(), and wfProfileOut().

memcached::get_multi ( keys  ) 

Get multiple keys from the server(s).

Parameters:
array $keys Keys to retrieve
Returns:
array
Access:
public

Definition at line 452 of file memcached-client.php.

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

memcached::get_sock ( key  ) 

get_sock

Parameters:
string $key Key to retrieve value for;
Returns:
mixed resource on success, false on failure
Access:
private

Definition at line 766 of file memcached-client.php.

References $i, $key, _flush_read_buffer(), _hashfunc(), and sock_to_host().

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

memcached::incr ( key,
amt = 1 
)

Increments $key (optionally) by $amt.

Parameters:
string $key Key to increment
integer $amt (optional) amount to increment
Returns:
integer New key value?
Access:
public

Definition at line 518 of file memcached-client.php.

References $key, and _incrdecr().

memcached::memcached ( args  ) 

Memcache initializer.

Parameters:
array $args Associative array of settings
Returns:
mixed
Access:
public

Definition at line 248 of file memcached-client.php.

References $args, and set_servers().

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

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

Parameters:
string $key Key to set value as
mixed $value Value to store
integer $exp (optional) Experiation time
Returns:
boolean
Access:
public

Definition at line 536 of file memcached-client.php.

References $key, and _set().

memcached::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:
resource $sock Socket to send command on
string $cmd Command to run
Returns:
array Output array
Access:
public

Definition at line 560 of file memcached-client.php.

References $cmd, $res, and _safe_fwrite().

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

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

Returns true if set successfully.

Parameters:
string $key Key to set value as
mixed $value Value to set
integer $exp (optional) Experiation time
Returns:
boolean TRUE on success
Access:
public

Definition at line 594 of file memcached-client.php.

References $key, and _set().

memcached::set_compress_threshold ( thresh  ) 

Sets the compression threshold.

Parameters:
integer $thresh Threshold to compress if larger than
Access:
public

Definition at line 609 of file memcached-client.php.

memcached::set_debug ( dbg  ) 

Sets the debug flag.

Parameters:
boolean $dbg TRUE for debugging, FALSE otherwise
Access:
public
See also:
memcahced::memcached

Definition at line 626 of file memcached-client.php.

memcached::set_servers ( list  ) 

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

Parameters:
array $list Array of servers to connect to
Access:
public
See also:
memcached::memcached()

Definition at line 643 of file memcached-client.php.

Referenced by memcached().

memcached::set_timeout ( seconds,
microseconds 
)

Sets the timeout for new connections.

Parameters:
integer $seconds Number of seconds
integer $microseconds Number of microseconds
Access:
public

Definition at line 663 of file memcached-client.php.

memcached::sock_to_host ( host  ) 

Returns the socket for the host.

Parameters:
string $host Host:IP to get socket for
Returns:
mixed IO Stream or false
Access:
private

Definition at line 1004 of file memcached-client.php.

References _connect_sock(), and _dead_sock().

Referenced by get_sock().


Member Data Documentation

memcached::$_active

Definition at line 206 of file memcached-client.php.

memcached::$_bucketcount

Definition at line 198 of file memcached-client.php.

memcached::$_buckets

Definition at line 190 of file memcached-client.php.

memcached::$_cache_sock

Definition at line 118 of file memcached-client.php.

memcached::$_compress_enable

Definition at line 150 of file memcached-client.php.

memcached::$_compress_threshold

Definition at line 158 of file memcached-client.php.

memcached::$_connect_attempts

Number of connection attempts for each server.

Definition at line 232 of file memcached-client.php.

memcached::$_connect_timeout

Connect timeout in seconds.

Definition at line 227 of file memcached-client.php.

memcached::$_debug

Definition at line 126 of file memcached-client.php.

memcached::$_have_zlib

Definition at line 142 of file memcached-client.php.

memcached::$_host_dead

Definition at line 134 of file memcached-client.php.

memcached::$_persistant

Definition at line 166 of file memcached-client.php.

memcached::$_servers

Definition at line 182 of file memcached-client.php.

memcached::$_single_sock

Definition at line 174 of file memcached-client.php.

memcached::$_timeout_microseconds

Definition at line 222 of file memcached-client.php.

memcached::$_timeout_seconds

Definition at line 214 of file memcached-client.php.

memcached::$stats

Definition at line 107 of file memcached-client.php.

Flag: indicates data is compressed.

Definition at line 89 of file memcached-client.php.

Referenced by _load_items(), and _set().

Minimum savings to store data compressed.

Definition at line 96 of file memcached-client.php.

Referenced by _set().

Flag: indicates data is serialized.

Definition at line 84 of file memcached-client.php.

Referenced by _load_items(), and _set().


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

Generated on Sat Sep 5 02:08:42 2009 for MediaWiki by  doxygen 1.5.9