Memcached::getMulti
(PECL memcached >= 0.1.0)
Memcached::getMulti — Retrieve multiple items
Description
Memcached::getMulti() is similar to
Memcached::get(), but instead of a single key
item, it retrieves 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.
Note:
Unlike Memcached::get() it is not possible to specify a read-through cache callback for Memcached::getMulti(), because the memcache protocol does not provide information on which keys were not found in the multi-key request.
The flags
parameter can be used to specify
additional options for Memcached::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.
Parameters
-
keys
-
Array of keys to retrieve.
-
cas_tokens
-
The variable to store the CAS tokens for the found items.
-
flags
-
The flags for the get operation.
Return Values
Returns the array of found items or FALSE
on failure.
Use Memcached::getResultCode() if necessary.
Examples
Example #1 Memcached::getMulti() example
<?php
$m = new Memcached();
$m->addServer('localhost', 11211);
$items = array(
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3'
);
$m->setMulti($items);
$result = $m->getMulti(array('key1', 'key3', 'badkey'), $cas);
var_dump($result, $cas);
?>
The above example will output something similar to:
array(2) { ["key1"]=> string(6) "value1" ["key3"]=> string(6) "value3" } array(2) { ["key1"]=> float(2360) ["key3"]=> float(2362) }
Example #2 Memcached::GET_PRESERVE_ORDER
example
<?php
$m = new Memcached();
$m->addServer('localhost', 11211);
$data = array(
'foo' => 'foo-data',
'bar' => 'bar-data',
'baz' => 'baz-data',
'lol' => 'lol-data',
'kek' => 'kek-data',
);
$m->setMulti($data, 3600);
$null = null;
$keys = array_keys($data);
$keys[] = 'zoo';
$got = $m->getMulti($keys, $null, Memcached::GET_PRESERVE_ORDER);
foreach ($got as $k => $v) {
echo "$k $v\n";
}
?>
The above example will output something similar to:
foo foo-data bar bar-data baz baz-data lol lol-data kek kek-data zoo
See Also
- Memcached::getMultiByKey() - Retrieve multiple items from a specific server
- Memcached::get() - Retrieve an item
- Memcached::getDelayed() - Request multiple items
- Функция Memcached::add() - Add an item under a new key
- Функция Memcached::addByKey() - Add an item under a new key on a specific server
- Функция Memcached::addServer() - Add a server to the server pool
- Функция Memcached::addServers() - Add multiple servers to the server pool
- Функция Memcached::append() - Append data to an existing item
- Функция Memcached::appendByKey() - Append data to an existing item on a specific server
- Функция Memcached::cas() - Compare and swap an item
- Функция Memcached::casByKey() - Compare and swap an item on a specific server
- Функция Memcached::__construct() - Create a Memcached instance
- Функция Memcached::decrement() - Decrement numeric item's value
- Функция Memcached::decrementByKey() - Decrement numeric item's value, stored on a specific server
- Функция Memcached::delete() - Delete an item
- Функция Memcached::deleteByKey() - Delete an item from a specific server
- Функция Memcached::deleteMulti() - Delete multiple items
- Функция Memcached::deleteMultiByKey() - Delete multiple items from a specific server
- Функция Memcached::fetch() - Fetch the next result
- Функция Memcached::fetchAll() - Fetch all the remaining results
- Функция Memcached::flush() - Invalidate all items in the cache
- Функция Memcached::get() - Retrieve an item
- Функция Memcached::getAllKeys() - Gets the keys stored on all the servers
- Функция Memcached::getByKey() - Retrieve an item from a specific server
- Функция Memcached::getDelayed() - Request multiple items
- Функция Memcached::getDelayedByKey() - Request multiple items from a specific server
- Функция Memcached::getMulti() - Retrieve multiple items
- Функция Memcached::getMultiByKey() - Retrieve multiple items from a specific server
- Функция Memcached::getOption() - Retrieve a Memcached option value
- Функция Memcached::getResultCode() - Return the result code of the last operation
- Функция Memcached::getResultMessage() - Return the message describing the result of the last operation
- Функция Memcached::getServerByKey() - Map a key to a server
- Функция Memcached::getServerList() - Get the list of the servers in the pool
- Функция Memcached::getStats() - Get server pool statistics
- Функция Memcached::getVersion() - Get server pool version info
- Функция Memcached::increment() - Increment numeric item's value
- Функция Memcached::incrementByKey() - Increment numeric item's value, stored on a specific server
- Функция Memcached::isPersistent() - Check if a persitent connection to memcache is being used
- Функция Memcached::isPristine() - Check if the instance was recently created
- Функция Memcached::prepend() - Prepend data to an existing item
- Функция Memcached::prependByKey() - Prepend data to an existing item on a specific server
- Функция Memcached::quit() - Close any open connections
- Функция Memcached::replace() - Replace the item under an existing key
- Функция Memcached::replaceByKey() - Replace the item under an existing key on a specific server
- Функция Memcached::resetServerList() - Clears all servers from the server list
- Функция Memcached::set() - Store an item
- Функция Memcached::setByKey() - Store an item on a specific server
- Функция Memcached::setMulti() - Store multiple items
- Функция Memcached::setMultiByKey() - Store multiple items on a specific server
- Функция Memcached::setOption() - Set a Memcached option
- Функция Memcached::setOptions() - Set Memcached options
- Функция Memcached::setSaslAuthData() - Set the credentials to use for authentication
- Функция Memcached::touch() - Set a new expiration on an item
- Функция Memcached::touchByKey() - Set a new expiration on an item on a specific server
Коментарии
GOTCHA: Recently I was tasked with moving from PECL memcache to PECL memcached and ran into a major problem -- memcache and memcached serialize data differently, meaning that data written with one library can't necessarily be read with the other library.
For example, If you write an object or an array with memcache, it's interpreted as an integer by memcached. If you write it with memcached, it's interpreted as a string by memcache.
tl;dr - You can't safely switch between memcache and memcached without a either a cache flush or isolated cache environments.
<?php
$memcache = new Memcache;
$memcacheD = new Memcached;
$memcache->addServer($host);
$memcacheD->addServers($servers);
$checks = array(
123,
4542.32,
'a string',
true,
array(123, 'string'),
(object)array('key1' => 'value1'),
);
foreach ($checks as $i => $value) {
print "Checking WRITE with Memcache\n";
$key = 'cachetest' . $i;
$memcache->set($key, $value);
usleep(100);
$val = $memcache->get($key);
$valD = $memcacheD->get($key);
if ($val !== $valD) {
print "Not compatible!";
var_dump(compact('val', 'valD'));
}
print "Checking WRITE with MemcacheD\n";
$key = 'cachetest' . $i;
$memcacheD->set($key, $value);
usleep(100);
$val = $memcache->get($key);
$valD = $memcacheD->get($key);
if ($val !== $valD) {
print "Not compatible!";
var_dump(compact('val', 'valD'));
}
}