Subject: | Two memcached round-trips for each cache hit |
Date: | Thu, 3 Mar 2016 10:47:03 +0100 |
To: | bug-Memoize-Memcached [...] rt.cpan.org |
From: | Peter Valdemar Mørch <peter [...] morch.com> |
Memoize.pm has this:
if (exists $cache->{$argstr}) {
return @{$cache->{$argstr}};
} else {
..
}
So first exists is tested to see if we have a cache hit, and then the value
is fetched.
Memoize/Memcached.pm has this:
sub FETCH {
my $self = shift;
my $key = $self->_get_key(shift);
return $self->{memcached_obj}->get($key);
}
sub EXISTS {
my $self = shift;
return defined $self->FETCH(@_);
}
So if there is a cache hit, first EXISTS is called (which calls FETCH
internally) and then afterwards, FETCH is called again. Here is what it
looks like in a NYTProf flamegraph:
[image: Inline image 1]
( http://i.imgur.com/WYcDbiK.png )
for:
sub foo {
return "2";
}
memoize_memcached('foo');
foreach (1..100000) {
foo();
}
I don't know the side effects, but it looks like execution time for a cache
hit (the common case?) could be cut in half rather easily...
Peter
--
Peter Valdemar Mørch
http://www.morch.com