views:

25

answers:

1

I would like to use getDelayed on the PHP Memcached extension but I think it's not implemented in the right way.

Right now you ask for some keys and then retrieve all of them with fetch() and fetchAll(). But imagine a scenario where I need to retrieve 15 keys used in different parts of the page which I don't know in advance, but I can ask the various objects to give me the list. What I want is give the Memcached instance this list (each component would give its part) then later when I need them retrieve from the instance, but not all of them at once: each component would take the one it needs.

Basically if I were to implement this I would prohibit using getDelayed alone and implement a bookGet($keys) method where you would add the keys to book (which actually calls getDelayed), and redefine get to handle these three cases:

  1. key is booked and retrieved -> return the value;
  2. key is booked but not retrieved -> go and force the fetch of the booked keys and return the correct value;
  3. key not booked -> do a normal lookup.

I want to know if this makes sense, your thoughts on the subject and if someone already implemented this or maybe PECL Memcached already works this way and actually the documentation doesn't explain it correctly.

A: 

I'd think it's a bad idea to add it to PECL like that: the get() function should retrieve directly from memcache, which perhaps has altered data since the last fetch()/fetchAll() you performed. Your functionality would be easily implemented by extending the memcached class for your specific purposes, which might not be what the rest of the world wants. Just add some private stores of 'booked' and 'already retrieved' keys in your class & redefine your get() method to behave as you like (and maybe call parent::get() of course), and you're good to go.

I do think I should be able to call getDelayed() more then once and expect it to add the extra keys to retrieve instead of overwriting it. (so a $m->getDelayed(array('foo'); followed by a $m->getDelayed(array('bar'); would result in a fetchAll() of both those keys, not just the last 'bar'.

Wrikken
Yes, I know I can implement it, and I know `getDelayed` doesn't forget old requests. The problem I have with this approach can be explained in this scenario: `$m->getDelayed('foo');` / `...` / `$m->getDelayed('bar');` / `...` / now I want 'bar' and so I need to do a fetchAll() which returns also 'foo' and I have to store 'foo' somewhere / `...` / now I need 'foo' so I need to check if in the previous fetch it was retrieved or not, if not I need to fetch now. Don't know if my reasoning is somewhat limited, but this leads to lot of duplicated code and non-expandability...
iBobo
Wrikken