Overview  Package  Tree cmsdAm 0.8

cmsdAm

Class cmsdam_memcache

cmsdAm.cmsdam_memcache

class cmsdam_memcache

This is the cmsdAm variables caching class.

CmsdAm uses it to drastically increase performance. Please keep in mind that this tool must be used with security in mind, because if you can modify cache values, you'll be able to modify variables of cmsdam. This can be generally used for our scopes without problems.

Class instance: $cms->memcache

Author:
dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>

Field Summary
public var$conf

You should not use this variable from your section.

public array$stats

Command statistics

private array$_cache_sock

Cached Sockets that are connected

private str$_memcache_cmsdam_sid

Private cmsdam cache SID number.

private boolean$_debug

Current debug status; 0 - none to 9 - profiling

private array$_host_dead

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

private boolean$_have_zlib

Is compression available?

private boolean$_compress_enable

Do we want to use compression?

private interger$_compress_threshold

At how many bytes should we compress?

private boolean$_persistant

Are we using persistant links?

private string$_single_sock

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

private array$_servers

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

private array$_buckets

Our bit buckets

private interger$_bucketcount

Total # of bit buckets we have

private interger$_active

# of total servers we have

Method Summary
public mixedcmsdam_memcache

Memcache initializer

public booleanadd

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

public mixeddecr

Decriment a value stored on the memcache server

public booleandelete

Deletes a key from the server, optionally after $time

public voiddisconnect_all

Disconnects all connected sockets

public voidenable_compress

Enable / Disable compression

public voidforget_dead_hosts

Forget about all of the dead hosts

public mixedget

Retrieves the value associated with the key from the memcache server

public arrayget_multi

Get multiple keys from the server(s)

public intergerincr

Increments $key (optionally) by $amt

public booleanreplace

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

public arrayrun_command

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 \r\n.

public booleanset

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

public voidset_compress_threshold

Sets the compression threshold

public voidset_debug

Sets the debug flag

public voidset_servers

Sets the server list to distribute key gets and puts between

private void_close_sock

Close the specified socket

private boolean_connect_sock

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

private void_dead_sock

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

private mixedget_sock

get_sock

private interger_hashfunc

Creates a hash interger based on the $key

private interger_incrdecr

Perform increment/decriment on $key

private void_load_items

Load items into $ret from $sock

private boolean_set

Performs the requested storage operation to the memcache server

private mixedsock_to_host

Returns the socket for the host

Field Detail

conf

public var $conf

You should not use this variable from your section.


stats

public array $stats

Command statistics


_cache_sock

private array $_cache_sock

Cached Sockets that are connected


_memcache_cmsdam_sid

private str $_memcache_cmsdam_sid

Private cmsdam cache SID number.

This is to avoid cache pollution if you have another cmsdAm site hosted in your web server.


_debug

private boolean $_debug

Current debug status; 0 - none to 9 - profiling


_host_dead

private array $_host_dead

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


_have_zlib

private boolean $_have_zlib

Is compression available?


_compress_enable

private boolean $_compress_enable

Do we want to use compression?


_compress_threshold

private interger $_compress_threshold

At how many bytes should we compress?


_persistant

private boolean $_persistant

Are we using persistant links?


_single_sock

private string $_single_sock

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


_servers

private array $_servers

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


_buckets

private array $_buckets

Our bit buckets


_bucketcount

private interger $_bucketcount

Total # of bit buckets we have


_active

private interger $_active

# of total servers we have

Method Detail

cmsdam_memcache()

public mixed cmsdam_memcache(array args = "")

Memcache initializer

Parameters:
args - Associative array of settings

add()

public boolean add(string key,
                   mixed val,
                   interger exp = 0)

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

Parameters:
key - Key to set with data
val - Value to store
exp - (optional) Time to expire data at

decr()

public mixed decr(string key,
                  interger amt = 1)

Decriment a value stored on the memcache server

Parameters:
key - Key to decriment
amt - (optional) Amount to decriment
Returns:
FALSE on failure, value on success

delete()

public boolean delete(string key,
                      interger time = 0)

Deletes a key from the server, optionally after $time

Parameters:
key - Key to delete
time - (optional) How long to wait before deleting
Returns:
TRUE on success, FALSE on failure

disconnect_all()

public void disconnect_all()

Disconnects all connected sockets


enable_compress()

public void enable_compress(boolean enable)

Enable / Disable compression

Parameters:
enable - TRUE to enable, FALSE to disable

forget_dead_hosts()

public void forget_dead_hosts()

Forget about all of the dead hosts


get()

public mixed get(string key)

Retrieves the value associated with the key from the memcache server

Parameters:
key - Key to retrieve

get_multi()

public array get_multi(array keys)

Get multiple keys from the server(s)

Parameters:
keys - Keys to retrieve

incr()

public interger incr(string key,
                     interger amt = 1)

Increments $key (optionally) by $amt

Parameters:
key - Key to increment
amt - (optional) amount to increment
Returns:
New key value?

replace()

public boolean replace(string key,
                       mixed value,
                       interger exp = 0)

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

Parameters:
key - Key to set value as
value - Value to store
exp - (optional) Experiation time

run_command()

public array run_command(resource sock,
                         string 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 \r\n.

More specifically, my testing has shown that, on FreeBSD at least, each line is terminated only with a \n. This is with the PHP flag auto_detect_line_endings set to falase (the default).

Parameters:
sock - Socket to send command on
cmd - Command to run
Returns:
Output array

set()

public boolean set(string key,
                   mixed value,
                   interger exp = 0)

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

Returns true if set successfully.

Parameters:
key - Key to set value as
value - Value to set
exp - (optional) Experiation time
Returns:
TRUE on success

set_compress_threshold()

public void set_compress_threshold(interger thresh)

Sets the compression threshold

Parameters:
thresh - Threshold to compress if larger than

set_debug()

public void set_debug(boolean dbg)

Sets the debug flag

Parameters:
dbg - TRUE for debugging, FALSE otherwise
See Also:
"memcahced::memcached"

set_servers()

public void set_servers(array list)

Sets the server list to distribute key gets and puts between

Parameters:
list - Array of servers to connect to
See Also:
"memcached::memcached"

_close_sock()

private void _close_sock(string sock)

Close the specified socket

Parameters:
sock - Socket to close

_connect_sock()

private boolean _connect_sock(interger sock,
                              string host,
                              float timeout = 0.25)

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

Parameters:
sock - Socket to connect
host - Host:IP to connect to
timeout - (optional) Timeout value, defaults to 0.25s

_dead_sock()

private void _dead_sock(string sock)

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

Parameters:
sock - Socket to mark as dead

get_sock()

private mixed get_sock(string key)

get_sock

Parameters:
key - Key to retrieve value for;
Returns:
resource on success, false on failure

_hashfunc()

private interger _hashfunc(string key)

Creates a hash interger based on the $key

Parameters:
key - Key to hash
Returns:
Hash value

_incrdecr()

private interger _incrdecr(string cmd,
                           string key,
                           interger amt = 1)

Perform increment/decriment on $key

Parameters:
cmd - Command to perform
key - Key to perform it on
amt - Amount to adjust
Returns:
New value of $key

_load_items()

private void _load_items(resource sock,
                         array ret)

Load items into $ret from $sock

Parameters:
sock - Socket to read from
ret - Returned values

_set()

private boolean _set(string cmd,
                     string key,
                     mixed val,
                     interger exp)

Performs the requested storage operation to the memcache server

Parameters:
cmd - Command to perform
key - Key to act on
val - What we need to store
exp - When it should expire

sock_to_host()

private mixed sock_to_host(string host)

Returns the socket for the host

Parameters:
host - Host:IP to get socket for
Returns:
IO Stream or false

Overview  Package  Tree cmsdAm 0.8