PHP 8.4.2 Released!

Memcached::getMulti

(PECL memcached >= 0.1.0)

Memcached::getMultiRetrieve multiple items

Опис

public Memcached::getMulti(array $keys, int $get_flags = 0): array|false

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.

Зауваження:

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 flag Memcached::GET_EXTENDED that needs is to be used as the value for get_flags.

The get_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.

Параметри

keys

Array of keys to retrieve.

get_flags

The flags for the get operation.

Значення, що повертаються

Returns the array of found items або false в разі помилки. За необхідності використовується Memcached::getResultCode().

Журнал змін

Версія Опис
PECL memcached 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.

Приклади

Приклад #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);
?>

Поданий вище приклад виведе щось схоже на:

array(2) {
  ["key1"]=>
  string(6) "value1"
  ["key3"]=>
  string(6) "value3"
}

Приклад #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);
?>

Поданий вище приклад виведе щось схоже на:

array(2) {
  ["key1"]=>
  string(6) "value1"
  ["key3"]=>
  string(6) "value3"
}
array(2) {
  ["key1"]=>
  float(2360)
  ["key3"]=>
  float(2362)
}

Приклад #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";
}
?>

Поданий вище приклад виведе щось схоже на:

foo foo-data
bar bar-data
baz baz-data
lol lol-data
kek kek-data
zoo

Приклад #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";
}
?>

Поданий вище приклад виведе щось схоже на:

foo foo-data
bar bar-data
baz baz-data
lol lol-data
kek kek-data
zoo

Прогляньте також

add a note

User Contributed Notes 1 note

up
1
gabriel dot maybrun at demandmedia dot com
10 years ago
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'));
}
}
To Top