开发者

Memcached getDelayed alternative implementation

开发者 https://www.devze.com 2023-01-03 16:15 出处:网络
I would like to use getDelayed on the PHP Memcached extension but I think it\'s not implemented in the right way.

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 a开发者_运维百科sk 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.


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'.

0

精彩评论

暂无评论...
验证码 换一张
取 消