加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP memcached:getDelayed&getMulti – 如何使用?

发布时间:2020-12-13 21:37:16 所属栏目:PHP教程 来源:网络整理
导读:我最近想过如何在 PHP应用程序中使用getDelayed和getMulti,以及它们的区别. 从阅读有关getDelayed的文档: “The method does not wait for response and returns right away. When you are ready to collect the items,call either Memcached::fetch or Mem
我最近想过如何在 PHP应用程序中使用getDelayed和getMulti,以及它们的区别.

从阅读有关getDelayed的文档:

“The method does not wait for response
and returns right away. When you are
ready to collect the items,call
either Memcached::fetch or
Memcached::fetchAll.”

因此,与getMulti不同,显然需要在键可用之前调用fetchAll.但实际的memcached调用何时完成?
在fetchAll或getDelayed运行时?

更新了示例:

$this->memcached->set('int',99);
    $this->memcached->set('string','a simple string');
    $this->memcached->set('array',array(11,12));

    $this->memcached->getDelayed(array('int'));
    $this->memcached->getDelayed(array('string'));
    $this->memcached->getDelayed(array('array'));

    print("<pre>".print_r( $this->memcached->fetchAll() )."</pre>"); // returns the array element only.

解决方法

Memcache IO发生在getDelayed或fetchAll上.

getDelayed基本上说“我想要这些键,但我现在不需要它们”.

这样做的主要好处是它允许PHP在后台执行此操作作为并行操作.如果您知道以后在进程中需要哪些键,您可以让PHP去获取它们,当您需要它们时,可以调用fetchAll.

如果PHP在设置其他东西的同时设法获取数据,那么当你调用fetchAll时,就没有等待了.如果没有,则当Memcached中的数据完成传输时,进程暂停.

一个非常愚蠢的例子是你有两件事要做:

>调整花费3秒钟的图像大小.
>从memcache获取100个值,耗时2秒.

如果您只调整了图像大小然后使用了getMulti,则需要5秒钟.

如果你为你的键调用了getDelayed,那么调整图像大小然后使用fetchAll,整个过程只需要3秒钟.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读