【问题标题】:Memcache::get not working with array of keysMemcache::get 不使用键数组
【发布时间】:2016-11-19 19:22:21
【问题描述】:

我正在尝试在 PHP 中测试 memcached 客户端的 multi_get 功能,我知道

array Memcache::get ( array $keys [, array &$flags ] )

可用。

00000001 <?php
00000002     function rand01()
00000003     {   // auxiliary function
00000004          // returns random number with flat distribution from 0 to 1
00000005         return (float)rand()/(float)getrandmax();
00000006     }
00000007     ini_set('display_errors', 1);
00000008     $mc=new Memcached();
00000009     $mc->setOption(Memcached::OPT_DISTRIBUTION,Memcached::DISTRIBUTION_CONSISTENT);
00000010     $mc->setOption(Memcached::OPT_REMOVE_FAILED_SERVERS,true);
00000011     echo "return value of addServer<br>";
00000012     var_dump($mc->addServer("mc1",11211));
00000013     var_dump($mc->addServer("mc2",11211));
00000014     var_dump($mc->addServer("mc3",11211));
00000015     echo "<br>";
00000016     $mc->set('00010111222',"testval");
00000017     $mc->set('00010333444',"testval");
00000018     echo "<br>";
00000019     var_dump($mc->get(array('00010111222', '00010333444')));
00000020     var_dump($mc->getResultCode());
00000023 ?>

但它给了我以下输出:

   return value of addServer
   bool(true) bool(true) bool(true)
   Warning: Memcached::get() expects parameter 1 to be string, array given in /var/www/html/memcache.php on line 19
   NULL int(0)

这意味着服务器添加成功,但第19行的get()给出了参数为数组且返回对象为NULL的警告。返回码为 0,表示查询成功,并且密钥存在于 memcached 中,因为我在第 16 行和第 17 行设置它们。是我做错了什么还是 PHP::Memcache 中的错误?

【问题讨论】:

  • 作为拥有 1000+ 声誉的用户,我希望您至少会在第 39 行显示错误的代码。但出于尊重,我不会投反对票。但是,如果您的意思是第 39 行实际上是 00000019 我建议您遵守错误消息。
  • 啊,是的,行号弄乱了。实际上是第 19 行。

标签: php arrays memcached


【解决方案1】:

这是MemcacheMemcached 的问题。

Memcached::get 只接受一个字符串作为第一个参数。

http://php.net/manual/en/memcached.get.php

Memcache::get 是接受字符串或数组的方法。

http://php.net/manual/en/memcache.get.php

您可以完成与遍历数组并为每个值调用 Memcached::get($key) 相同的结果。

【讨论】:

  • 我认为你是对的,我不知道我是怎么错过的。 Memcached::getMulti()Memcached 。谢谢。
猜你喜欢
  • 1970-01-01
  • 2012-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-13
  • 1970-01-01
  • 2018-10-03
相关资源
最近更新 更多