PHP 8.4.1 Released!

Memcached::getMulti

(PECL memcached >= 0.1.0)

Memcached::getMulti检索多个元素

说明

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

Memcached::getMulti()Memcached::get() 类似,但是这个方法用来检索 keys 数组指定的多个 key 对应的元素。

注意:

在 v3.0 之前,使用的第二个参数是 &cas_tokens。 它会填充进元素的 CAS token 值。 在本扩展里,&cas_tokens 参数在 v3.0 中已经移除。 它被新的附加选项(flag) Memcached::GET_EXTENDED 代替,需要在 get_flags 值里使用。

get_flags参数可以用做指定Memcached::getMulti()的附加选项。 当前,仅可以指定为Memcached::GET_PRESERVE_ORDER以保证返回的key的顺序和请求时一致。 Memcached::GET_EXTENDED 可以确保同时返回了 CAS token 信息。

参数

keys

要检索的key的数组。

get_flags

Get 操作的附加选项。

返回值

返回检索到的元素的数组 或者在失败时返回 false。 如需要则使用 Memcached::getResultCode()

更新日志

版本 说明
PECL memcached 3.0.0 移除 &cas_tokens 参数。 新增 Memcached::GET_EXTENDED,当作为 flag 传递时,确保获取到 CAS token。

示例

示例 #1 Memcached::getMulti() 的 Memcached v3 示例

<?php
// 扩展版本 v3 有效

$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() 的 Memcached v1 和 v2 示例

<?php
// 仅在扩展版本 v1 和 v2 中有效

$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 的 Memcached v3 示例

<?php
// v3 扩展有效

$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 的 Memcached v1 和 v2 示例

<?php
// 在扩展版本 v1 和 v2 中有效

$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

参见

添加备注

用户贡献的备注 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