Creates a Memcached instance representing the connection to the memcache servers.
MemcachedManager
__construct
([string $persistentid = ''], [ $prefix = ''])
-
string
$persistentid: By default the Memcached instances are destroyed at the end of the request. To create an instance that persists between requests, use persistent_id to specify a unique ID for the instance. All instances created with the same persistent_id will share the same connection.
-
$prefix
Add an item under a new key.
Similar to set, but the operation fails if the key already exists.
<?php
$mcd = new MemcacheManager();
$mcd->add('mykey1', $myarray1, 30);
?>
bool
add
(string $key, mixed $value, [int $expiration = 0])
-
string
$key: The key under which to store the value.
-
mixed
$value: The value to store.
-
int
$expiration: Some storage commands involve sending an expiration value (relative to an item or to an operation requested by the client) to the server. In all such cases, the actual value sent may either be Unix time (number of seconds since January 1, 1970, as an integer), or a number of seconds starting from current time. In the latter case, this number of seconds may not exceed 60*60*24*30 (number of seconds in 30 days); if the expiration value is larger than that, the server will consider it to be real Unix time value rather than an offset from current time. If the expiration value is 0 (the default), the item never expires (although it may be deleted from the server to make place for other items).
Add an item under a new key on a specific server.
Functionally equivalent to add, except that the free-form server_key can be used to map the key to a specific server. This is useful if you need to keep a bunch of related keys on a certain server.
<?php
$mcd = new MemcacheManager();
$mcd->addByKey('myserverkey', 'mykey1', $myarray1, 30);
?>
bool
addByKey
(string $server_key, string $key, mixed $value, [int $expiration = 0])
-
string
$server_key: The key identifying the server to store the value on.
-
string
$key: The key under which to store the value.
-
mixed
$value: The value to store.
-
int
$expiration: The expiration time, defaults to 0. SEE: add() $expiration for more info.
Add Tags to a Key.
For best results, you should namespace your tags. Start all your tags with something consistent, like: 'tag_', and then you can keep that patters going with tags like 'tag_user_', or 'tag_object_' etc. This will allow you to find things the easiest by _searchTags() to find all tags in that namespace. So tags like 'tag_user_peter', 'tag_user_janet', 'tag_user_john' can be searched with _searchTags('_user_') and all your users will be found.
<?php
$mcd = new MemcacheManager();
$mcd->addKeyTags('foo', array('tag1', 'tag2', 'tag3'));
?>
void
addKeyTags
(string $key, string $tags)
-
string
$key: Memcached Key
-
string
$tags: Memcached Tag
Add Tag to a Multiple Keys.
<?php
$mcd = new MemcacheManager();
$mcd->set('foo','value1');
$mcd->set('bar','value2');
?>
void
addMultiKeyTag
(array $keys, string $tag)
-
array
$keys: Memcached Keys
-
string
$tag: Memcached Tag
Add a server to the server pool.
Adds the specified server to the server pool. No connection is established to the server at this time, but if you are using consistent key distribution option, some of the internal data structures will have to be updated. Thus, if you need to add multiple servers, it is better to use addServers as the update then happens only once. The same server may appear multiple times in the server pool, because no ( duplication checks are made. This is not advisable; instead, use the weight option to increase the selection weighting of this server.
<?php
$mcd = new MemcacheManager();
$mcd->addServer('mem1.domain.com', 11211, 33);
?>
bool
addServer
(string $host, int $port, int $weight)
-
string
$host: The hostname of the memcache server. If the hostname is invalid, data-related operations will set RES_HOST_LOOKUP_FAILURE result code.
-
int
$port: The port on which memcache is running. Usually, this is 11211.
-
int
$weight: The weight of the server relative to the total weight of all the servers in the pool. This controls the probability of the server being selected for operations. This is used only with consistent distribution option and usually corresponds to the amount of memory available to memcache on that server.
Add multiple servers to the server pool.
Adds servers to the server pool. Each entry in servers is supposed to an array containing hostname, port, and, optionally, weight of the server. No connection is established to the servers at this time. The same server may appear multiple times in the server pool, because no duplication checks are made. This is not advisable; instead, use the weight option to increase the selection weighting of this server.
<?php
$mcd = new MemcacheManager();
$servers = array(
array('localhost', 11211, 33),
array('localhost', 11212, 33),
array('localhost', 11213, 33)
);
}
?>
bool
addServers
(array $servers)
-
array
$servers: Array of the servers to add to the pool.
Append data to an existing item.
Appends the given value string to the value of an existing item. The reason that value is forced to be a string is that appending mixed types is not well-defined. Note: If the OPT_COMPRESSION is enabled, the operation will fail and a warning will be issued, because appending compressed data to a value that is potentially already compressed is not possible.
<?php
$mcd = new MemcacheManager();
?>
bool
append
(string $key, string $value)
-
string
$key: The key under which to store the value.
-
string
$value: The string to append.
Append data to an existing item on a specific server.
Functionally equivalent to append, except that the free-form server_key can be used to map the key to a specific server.
<?php
$mcd = new MemcacheManager('server_key');
$mcd->setByKey('myserverkey', 'foo', 'abc');
?>
bool
appendByKey
(string $server_key, string $key, string $value)
-
string
$server_key: The key identifying the server to store the value on.
-
string
$key: The key under which to store the value.
-
string
$value: The string to append.
C.A.S. Compare and Swap an item.
Performs a "check and set" operation, so that the item will be stored only if no other client has updated it since it was last fetched by this client. The check is done via the cas_token parameter which is a unique 64-bit value assigned to the existing item by memcache. See the documentation for get methods for how to obtain this token. Note that the token is represented as a double due to the limitations of PHP's integer space.
<?php
$mcd = new MemcacheManager('server_key');
$mcd->setByKey('myserverkey', 'foo', 'abc');
?>
bool
cas
(float $cas_token, string $key, mixed $value, [ $expiration = 0], int $expire)
-
float
$cas_token: Unique value associated with the existing item. Generated by memcache.
-
string
$key: The key under which to store the value.
-
mixed
$value: The value to store.
-
int
$expire: The expiration time, defaults to 0. SEE: add() $expiration for more info.
-
$expiration
C.A.S. Compare and Swap an item on a specific server.
Functionally equivalent to cas, except that the free-form server_key can be used to map the key to a specific server. This is useful if you need to keep a bunch of related keys on a certain server.
<?php
$mcd = new MemcacheManager('server_key');
$mcd->setByKey('myserverkey', 'foo', 'abc');
?>
bool
casByKey
(float $cas_token, string $server_key, string $key, mixed $value, [ $expiration = 0], int $expire)
-
float
$cas_token: Unique value associated with the existing item. Generated by memcache.
-
string
$server_key: The key identifying the server to store the value on.
-
string
$key: The key under which to store the value.
-
mixed
$value: The value to store.
-
int
$expire: The expiration time, defaults to 0. SEE: add() $expiration for more info.
-
$expiration
Delete Keys and then any tags that might be saved
<?php
$mcd->set('foo','value1');
$mcd->set('bar','value2');
$mcd->addKeyTags('bar', array('tag_user_peter', 'tag_user_janet', 'tag_user_john'));
$mcd->addKeyTags('foo', array('tag_user_peter', 'tag_user_janet', 'tag_user_john'));
?>
void
cleanDelete
(array $array)
-
array
$array: Keys and Tags to delete from database and memcache
Decrement numeric item's value.
Decrements a numeric item's value by the specified offset. If the item's value is not numeric, it is treated as if the value were 0. If the operation would decrease the value below 0, the new value will be 0. decrement() will fail if the item does not exist.
<?php
$mcd = new MemcacheManager();
?>
mixed
decrement
(string $key, [int $offset = 1])
-
string
$key: Key of the item do decrement.
-
int
$offset: Decrement the item by value . Optional and defaults to 1.
Delete an item. Deletes the key from the server.
The time parameter is the amount of time in seconds (or Unix time until which) the client wishes the server to refuse add and replace commands for this key. For this amount of time, the item is put into a delete queue, which means that it won't possible to retrieve it by the get command, but add and replace command with this key will also fail (the set command will succeed, however). After the time passes, the item is finally deleted from server memory. The parameter time defaults to 0 (which means that the item will be deleted immediately and further storage commands with this key will succeed).
<?php
$mcd = new MemcacheManager();
?>
bool
delete
(string $key, [int $timeout = 0])
-
string
$key: The key to be deleted.
-
int
$timeout: The amount of time the server will wait to delete the item.
Delete an item from a specific server.
Functionally equivalent to delete, except that the free-form server_key can be used to map the key to a specific server.
<?php
$mcd = new MemcacheManager();
?>
bool
deleteByKey
(string $server_key, string $key, [int $timeout = 0])
-
string
$server_key: The key identifying the server to store the value on.
-
string
$key: The key to be deleted.
-
int
$timeout: The amount of time the server will wait to delete the item.
Delete Tags from a Key
<?php
$mcd = new MemcacheManager();
$mcd->addKeyTags('foo', array('tag1', 'tag2', 'tag3'));
?>
void
deleteKeyTags
(string $key, string $tags)
-
string
$key: Memcached Key
-
string
$tags: Memcached Tag
Delete Tag from Multiple Keys.
<?php
$mcd = new MemcacheManager();
$mcd->set('foo','value1');
$mcd->set('bar','value2');
$mcd->addKeyTags('bar', array('tag_user_peter', 'tag_user_janet', 'tag_user_john'));
$mcd->addKeyTags('foo', array('tag_user_peter', 'tag_user_janet', 'tag_user_john'));
?>
void
deleteMultiKeyTag
(array $keys, string $tag)
-
array
$keys: Memcached Keys
-
string
$tag: Memcached Tag
Fetch the next result.
Retrieves the next result from the last request.
<?php
$mcd = new MemcacheManager();
$mcd->set('string', 'a simple string');
$mcd->set('array', array(11, 12));
while ($result =
$mcd->fetch()) {
}
?>
mixed
fetch
()
Fetch the next result.
Retrieves the next result from the last request.
<?php
$mcd = new MemcacheManager();
$mcd->set('string', 'a simple string');
$mcd->set('array', array(11, 12));
?>
mixed
fetchAll
()
Fetch Tags associated with a Key
<?php
$mcd = new MemcacheManager();
$mcd->addKeyTags('foo', array('tag1', 'tag2', 'tag3'));
?>
void
fetchKeyTags
(string $key, string $tags)
-
string
$key: Memcached Key
-
string
$tags: Memcached Tag
Invalidate all items in the cache.
Invalidates all existing cache items immediately (by default) or after the delay specified. After invalidation none of the items will be returned in response to a retrieval command (unless it's stored again under the same key after Memcached::flush() has invalidated the items). The flush does not actually free all the memory taken up by the existing items; that will happen gradually as new items are stored.
<?php
$mcd = new MemcacheManager();
?>
bool
flushmc
([int $delay = 0])
-
int
$delay: Numer of seconds to wait before invalidating the items.
Retrieve an item.
Returns the item that was previously stored under the key. If the item is found and cas_token variable is provided, it will contain the CAS token value for the item. See cas for how to use CAS tokens. Read-through caching callback may be specified via cache_cb parameter.
<?php
$mcd = new MemcacheManager();
echo
$mcd->get('mykey1', NULL, NULL);
?>
mixed
get
(string $key, [callback $cache_cb = NULL], [ &$cas_token = NULL], float $cas_token)
-
string
$key: The key of the item to retrieve.
-
callback
$cache_cb: Read-through caching callback or NULL. This callback handler will only get executed if the $key DOES NOT exist.
-
float
$cas_token: The variable to store the CAS token in.
-
&$cas_token
Retrieve an item from a specific server.
Functionally equivalent to get, except that the free-form server_key can be used to map the key to a specific server.
<?php
$mcd = new MemcacheManager();
echo
$mcd->getByKey('server_key', 'mykey1');
?>
mixed
getByKey
(string $server_key, string $key, [callback $cache_cb = NULL], [ &$cas_token = NULL], float $cas_token)
-
string
$server_key: The key identifying the server to store the value on.
-
string
$key: The key of the item to retrieve.
-
callback
$cache_cb: Read-through caching callback or NULL.
-
float
$cas_token: The variable to store the CAS token in.
-
&$cas_token
Retrieve an item from a specific server.
Functionally equivalent to get, except that the free-form server_key can be used to map the key to a specific server.
<?php
$mcd = new MemcacheManager();
$mcd->set('string', 'a simple string');
$mcd->set('array', array(11, 12));
echo
$mcd->getDelayed(array('int', 'string', 'array'), TRUE, NULL);
?>
bool
getDelayed
(array $keys, [bool $with_cas = TRUE], [callback $value_cb = NULL])
-
array
$keys: Array of keys to request.
-
bool
$with_cas: Whether to request CAS token values also.
-
callback
$value_cb: The result callback or NULL.
Request multiple items from a specific server
Functionally equivalent to getDelayed, except that the free-form server_key can be used to map the key to a specific server.
<?php
$mcd = new MemcacheManager();
$mcd->set('string', 'a simple string');
$mcd->set('array', array(11, 12));
echo
$mcd->getDelayedByKey('server_key', array('int', 'string', 'array'), TRUE, NULL);
?>
bool
getDelayedByKey
(string $server_key, array $keys, [bool $with_cas = TRUE], [callback $value_cb = NULL])
-
string
$server_key: The key identifying the server to store the value on.
-
array
$keys: Array of keys to request.
-
bool
$with_cas: Whether to request CAS token values also.
-
callback
$value_cb: The result callback or NULL.
Default get callback handler
This is what is called if no other callback handler was used with get(). This only gets called when get failed to find the key you were looking for.
void
getFailCallback
( $memc, $item)
Retrieve multiple items
Similar to get, but instead of a single key item, it retrievess multiple items the keys of which are specified in the keys array. If cas_tokens variable is provided, it is filled with the CAS token values for the found items. The flags parameter can be used to specify additional options for getMulti(). Currently, the only available option is Memcached::GET_PRESERVE_ORDER that ensures that the keys are returned in the same order as they were requested in. NOTE: Unlike get it is not possible to specify a read-through cache callback for getMulti(), because the memcache protocol does not provide information on which keys were not found in the multi-key request.
<?php
$mcd = new MemcacheManager();
$items = array(
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3'
);
$result =
$mcd->getMulti(array('key1', 'key3', 'badkey'), $cas);
?>
mixed
getMulti
(array $keys, [ &$cas_tokens = NULL], [int $flags = NULL], array $cas_tokens)
-
array
$keys: Array of keys to retrieve.
-
array
$cas_tokens: The variable to store the CAS tokens for the found items.
-
int
$flags: The flags for the get operation.
-
&$cas_tokens
Retrieve multiple items from a specific server
functionally equivalent to getMulti, except that the free-form server_key can be used to map the keys to a specific server.
<?php
$mcd = new MemcacheManager();
$items = array(
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3'
);
$result =
$mcd->getMultiByKey('server_key', array('key1', 'key3', 'badkey'), $cas);
?>
mixed
getMultiByKey
(string $server_key, array $keys, [ &$cas_tokens = NULL], [int $flags = NULL], string $cas_tokens)
-
string
$server_key: The key identifying the server to store the value on.
-
array
$keys: Array of keys to retrieve.
-
string
$cas_tokens: The variable to store the CAS tokens for the found items.
-
int
$flags: The flags for the get operation.
-
&$cas_tokens
Retrieve a Memcached option value
This method returns the value of a Memcached option. Some options correspond to the ones defined by libmemcached, and some are specific to the extension. See Memcached Constants for more information.
<?php
$mcd = new MemcacheManager();
?>
mixed
getOption
(int $option)
-
int
$option: One of the Memcached::OPT_* constants. SEE: http://www.php.net/manual/en/memcached.constants.php
Return the result code of the last operation
Returns one of the Memcached::RES_* constants that is the result of the last executed Memcached method.
<?php
$mcd = new MemcacheManager();
// my code
}
?>
int
getResultCode
()
Return the message describing the result of the last operation
Returns a string that describes the result code of the last executed Memcached method.
<?php
$mcd = new MemcacheManager();
$mcd->add('foo', 'bar'); // first time should succeed
?>
string
getResultMessage
()
Map a key to a server
Returns the server that would be selected by a particular server_key in all the *ByKey() operations.
<?php
$mcd = new MemcacheManager();
array('mem1.domain.com', 11211, 40),
array('mem2.domain.com', 11211, 40),
array('mem3.domain.com', 11211, 20),
));
$mcd->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
?>
bool
getServerByKey
(string $server_key)
-
string
$server_key: The key identifying the server to store the value on.
Get the list of the servers in the pool
Returns the list of all servers that are in its server pool.
<?php
$mcd = new MemcacheManager();
$mcd->addServer('mem1.domain.com', 11211, 33);
$mcd->addServer('mem1.domain.com', 11211, 67);
?>
array
getServerList
()
Get server pool statistics
Returns an array containing the state of all available memcache servers. See memcache protocol specification for details on these statistics.
<?php
$mcd = new MemcacheManager();
?>
array
getStats
()
Get server pool version info
Returns an array containing the version info for all available memcache servers.
<?php
$mcd = new MemcacheManager();
?>
array
getVersion
()
Increment item's value
<?php
$mcd = new MemcacheManager();
?>
mixed
increment
(string $key, [int $offset = 1])
-
string
$key: Key of the item to increment
-
int
$offset: Increment the item by value . Optional and defaults to 1.
Prepend data to an existing item
prepends the given value string to the value of an existing item. The reason that value is forced to be a string is that prepending mixed types is not well-defined. Note: If the OPT_COMPRESSION is enabled, the operation will fail and a warning will be issued, because appending compressed data to a value that is potentially already compressed is not possible.
<?php
$mcd = new MemcacheManager();
?>
bool
prepend
(string $key, string $value)
-
string
$key: The key of the item to prepend the data to.
-
string
$value: The string to prepend.
Prepend data to an existing item on a specific server
Functionally equivalent to prepend, except that the free-form server_key can be used to map the key to a specific server.
<?php
$mcd = new MemcacheManager('server_key');
$mcd->setByKey('myserverkey', 'foo', 'abc');
?>
bool
prependByKey
(string $server_key, string $key, string $value)
-
string
$server_key: The key identifying the server to store the value on.
-
string
$key: The key of the item to prepend the data to.
-
string
$value: The string to prepend.
Replace the item under an existing key.
Similar to set, but the operation fails if the key does not exist on the server.
<?php
$mcd = new MemcacheManager();
$mcd->replace('mykey1', $myarray1, 30);
?>
bool
replace
(string $key, mixed $value, [int $expire = 0])
-
string
$key: The key under which to store the value.
-
mixed
$value: The value to store.
-
int
$expire: The expiration time, defaults to 0. SEE: add() $expiration for more info.
Replace the item under an existing key on a specific server
Functionally equivalent to replace, except that the free-form server_key can be used to map the key to a specific server. This is useful if you need to keep a bunch of related keys on a certain server.
<?php
$mcd = new MemcacheManager();
?>
bool
replaceByKey
(string $server_key, string $key, mixed $value, [int $expire = 0])
-
string
$server_key: The key under which to store the value.
-
string
$key: The key under which to store the value.
-
mixed
$value: The value to store.
-
int
$expire: The expiration time, defaults to 0. SEE: add() $expiration for more info.
Search for Namespaced Tag
<?php
$mcd = new MemcacheManager();
$mcd->addKeyTags('foo', array('tag_user_peter', 'tag_user_janet', 'tag_user_john'));
print_r($mcd->searchTag('_user_'));
?>
void
searchForKeys
(string $tag)
-
string
$tag: Namespaced Tag Search
Store an item.
Stores the value on a memcache server under the specified key. The expiration parameter can be used to control when the value is considered expired. The value can be any valid PHP type except for resources, because those cannot be represented in a serialized form. If the OPT_COMPRESSION option is turned on, the serialized value will also be compressed before storage.
<?php
$mcd = new MemcacheManager();
$mcd->set('mykey1', $myarray1, 30);
?>
bool
set
(string $key, mixed $value, [int $expire = 0])
-
string
$key: The key under which to store the value.
-
mixed
$value: The value to store.
-
int
$expire: The expiration time, defaults to 0. SEE: add() $expiration for more info.
Store an item.
Stores the value on a memcache server under the specified key. The expiration parameter can be used to control when the value is considered expired. The value can be any valid PHP type except for resources, because those cannot be represented in a serialized form. If the OPT_COMPRESSION option is turned on, the serialized value will also be compressed before storage.
<?php
$mcd = new MemcacheManager();
$mcd->setByKey('server_key', 'mykey1', $myarray1, 30);
?>
bool
setByKey
( $server_key, string $key, mixed $value, [int $expire = 0])
-
string
$key: The key under which to store the value.
-
mixed
$value: The value to store.
-
int
$expire: The expiration time, defaults to 0. SEE: add() $expiration for more info.
-
$server_key
Store multiple items
Similar to set, but instead of a single key/value item, it works on multiple items specified in items. The expiration time applies to all the items at once.
<?php
$mcd = new MemcacheManager();
$items = array(
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3'
);
?>
bool
setMulti
(array $items, [int $expiration = 0])
-
array
$items: An array of key/value pairs to store on the server.
-
int
$expiration: The expiration time, defaults to 0. SEE: add() $expiration for more info.
Store multiple items
Similar to set, but instead of a single key/value item, it works on multiple items specified in items. The expiration time applies to all the items at once.
<?php
$mcd = new MemcacheManager();
$items = array(
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3'
);
?>
bool
setMultiByKey
(string $server_key, array $items, [int $expiration = 0])
-
string
$server_key: The key identifying the server to store the value on.
-
array
$items: An array of key/value pairs to store on the server.
-
int
$expiration: The expiration time, defaults to 0. SEE: add() $expiration for more info.
Set a Memcached option
This method sets the value of a Memcached option. Some options correspond to the ones defined by libmemcached, and some are specific to the extension. See Memcached Constants for more information.
<?php
$mcd = new MemcacheManager();
$mcd->setOption(Memcached::OPT_HASH, Memcached::HASH_MURMUR);
$mcd->setOption(Memcached::OPT_PREFIX_KEY, "widgets");
?>
bool
setOption
(int $option, mixed $value)
-
int
$option: Memcached Option
-
mixed
$value: Memcached Option Value