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.
Memcache IO happens on either
getDelayedorfetchAll.getDelayedbasically 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:
If you just resized the image and then used
getMulti, it would take 5 seconds.If you called
getDelayedfor your keys, then resized the image and then usedfetchAll, the whole thing would take just 3 seconds.