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.
Note:
Before v3.0 a second argument
&cas_tokens
was in use. It was filled with the CAS token values for the found items. The&cas_tokens
parameter was removed in v3.0 of the extension. It was replaced with a new flagMemcached::GET_EXTENDED
that needs is to be used as the value forflags
.
The flags
parameter can be used to specify
additional options for Memcached::getMulti().
Memcached::GET_PRESERVE_ORDER
ensures that the
keys are returned in the same order as they were requested in.
Memcached::GET_EXTENDED
ensures that the
CAS tokens will be fetched too.
Return Values
Returns the array of found items or FALSE
on failure.
Use Memcached::getResultCode() if necessary.
Examples
Example #1 Memcached::getMulti() example for Memcached v3
<?php
// Valid for v3 of the extension
$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'));
var_dump($result);
?>
The above example will output something similar to:
array(2) { ["key1"]=> string(6) "value1" ["key3"]=> string(6) "value3" }
Example #2 Memcached::getMulti() example for Memcached v1 and v2
<?php
// Valid for v1 and v2 of the extension
$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 #3 Memcached::GET_PRESERVE_ORDER
example for Memcached v3
<?php
// Valid for v3 of the extension
$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);
$keys = array_keys($data);
$keys[] = 'zoo';
$got = $m->getMulti($keys, 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
Example #4 Memcached::GET_PRESERVE_ORDER
example for Memcached v1 and v2
<?php
// Valid for v1 and v2 of the extension
$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
Changelog
Version | Description |
---|---|
3.0.0 |
The &cas_tokens parameter was removed.
The Memcached::GET_EXTENDED was added and when passed as a flag it ensures the CAS tokens to be fetched.
|
See Also
- Memcached::getMultiByKey() - Retrieve multiple items from a specific server
- Memcached::get() - Retrieve an item
- Memcached::getDelayed() - Request multiple items
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-memcached.getmulti.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.