tags:

views:

110

answers:

2

Hi everybody,

I have thought a bit recently about how to use getDelayed and getMulti in a PHP application, and their difference.

From reading the documentation about 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."

So obviously there's a need to call fetchAll before having the keys available, unlike getMulti. But when is the actual memcached call being done? At fetchAll or when getDelayed is run?

Updated with example:

    $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.
+1  A: 

Memcache IO happens on either getDelayed or fetchAll.

getDelayed basically says "I'm going to want these keys but I don't need them now".

The main benefit of this is it allows PHP to do this in the background as a parallel operation. If you know what keys you're going to need later on in a process, you can ask PHP to go and get them and when you do need them you call fetchAll.

If PHP has managed to get the data while it's been doing other things, when you call fetchAll, there's no wait. If it hasn't, the process pauses while the data from Memcached finishes being transferred.

A very silly example is if you had two things to do:

  1. Resize an image that took 3 seconds.
  2. Get 100 values from a memcache which took 2 seconds.

If you just resized the image and then used getMulti, it would take 5 seconds.

If you called getDelayed for your keys, then resized the image and then used fetchAll, the whole thing would take just 3 seconds.

Oli
Hi Oli, thanks for your answer. I updated my post with an example. Apparently the getDelayed call overwrite all previous getDelayed, and only returns the values in the last call. Check out my first post and let me know what you think
Industrial
Yeah I'm wrong. I'll update my answer.
Oli
No worries, but if that's the case, what is the difference between getMulti and getDelayed then? :)
Industrial
Hi Oli, Thanks a lot! Best and most detailed answer in a long time here on SO! Big ups!
Industrial
+1  A: 

It's being done in the background between that time. Basically, when you call getDelayed, it tells the memcache client on that server that it wants those keys, and it will be back to get them at a later point. So your PHP script is free to do what it wants while the memcache client executes the fetch. Then, when you need them, you call fetchAll. If all the data is already available when you call fetchAll, it returns immediately. If not, it will block until all the data is ready.

getMulti and fetchAll are both blocking calls. They wait until they have all the data to return. getDelayed is non-blocking, in that it returns right away no matter if it has data or not (because you "pull" the data later when you need it).

An example of why you would use getDelayed, would be something like this: Suppose you have an app that you know you're going to need a large object from memcached (say a rendered html page). As soon as you know you're going to need it, you can call getDelayed. Then, you can handle all your application cleanup, or logging, or headers, etc. Once everything is done, you can then call fetchAll to actually import the data into your script. Since everything else is already done, all you need to do is echo the data and die.

ircmaxell
Can I use several getDelayed calls and one fetchAll to get them without causing overhead?
Industrial
Honestly, I'm not sure. Try it... That's the best way to find out. Just experiment...
ircmaxell