Multiget/intelligent generic PHP wrapper function... thoughts/advice wanted

mike mike503 at gmail.com
Wed Oct 31 22:49:13 UTC 2007


On 10/31/07, Dustin Sallings <dustin at spy.net> wrote:
>
> On Oct 31, 2007, at 14:04 , mike wrote:
>
> > (side note, is there a such thing as a multi-set?)
>
>        In the binary protocol, there is neither a multi-get nor a multi-
> set.  Instead, a multi-get is built by streaming gets that don't
> report errors and then harvesting the results from the ones that did.
>
>        A similar thing could be built for sets, although I'd question the
> benefit of that over plain protocol pipelining given the relative
> rarity of sets.

That's cool. I was just wondering. Individual cache sets weren't my
biggest concern.

What would be interesting (and really useful) if the memcache_get
returned the key/value pairs, and if the cache was missed, returns
"false" or something else. Then I could just build an array on the
cache misses, instead of doing 2 or 3 iterations just due to making a
key list, checking the list, creating a list of what i need, adding
the individual missing rows back into the list, etc.

Or a second parameter that memcache_get returns with an array of what
was missed. I have to think doing it on the C/API level would be much
faster, and would make for much cleaner client-side code.

Then I could do something like:

function episode_get($keys = array()) {
	if(!isset($GLOBALS['ch'])) {
		cache_open();
	}
	list($cache_array, $cache_misses) = memcache_get($GLOBALS['ch'], $keys);
	if($cache_misses) {
		$q = db_query("SELECT * FROM episodes WHERE ID
IN(".implode(','$cache_misses).")");
		while($r = db_rows_assoc($q)) {
			$k = 'episodes:'.$r['ID'];
			cache_set($k, $r);
			$cache_array[$k] = $r;
		}
		db_free($q);
	}
	return $cache_array;
}

I think just that little bit of code would be able to replace everything..


More information about the memcached mailing list