From: Jakub Narebski Date: Sun, 5 Dec 2010 20:48:56 +0000 (+0100) Subject: gitweb/lib - Use CHI compatibile (compute method) caching interface X-Git-Url: https://repo.or.cz/w/git/jnareb-git.git/commitdiff_plain/7e1cadb741c8b183accb66447a7cd8f37d5406a1 gitweb/lib - Use CHI compatibile (compute method) caching interface If $cache provides CHI compatible ->compute($key, $code) method, use it instead of Cache::Cache compatible ->get($key) and ->set($key, $data). In the future other compatibile but differently named methods, like Cache::FastMmap's ->get_and_set($key, $code) method, could also be supported; though CHI which has ->compute() includes CHI::Driver::FastMmap wrapper. GitwebCache::SimpleFileCache provides 'compute' method, which currently simply use 'get' and 'set' methods in proscribed manner. Nevertheless 'compute' method can be more flexible in choosing when to refresh cache, and which process is to refresh/(re)generate cache entry. This method would use (advisory) locking to prevent 'cache miss stampede' (aka 'stampeding herd') problem in the next commit. Signed-off-by: Jakub Narebski --- diff --git a/gitweb/lib/GitwebCache/CacheOutput.pm b/gitweb/lib/GitwebCache/CacheOutput.pm index 458e314236..4a96ac9e57 100644 --- a/gitweb/lib/GitwebCache/CacheOutput.pm +++ b/gitweb/lib/GitwebCache/CacheOutput.pm @@ -38,6 +38,36 @@ sub cache_output { $capture = setup_capture($capture); + my $data; + if ($cache->can('compute')) { + $data = cache_output_compute($cache, $capture, $key, $code); + } else { + $data = cache_output_get_set($cache, $capture, $key, $code); + } + + if (defined $data) { + binmode STDOUT, ':raw'; + print $data; + } + + return $data; +} + +# for $cache which can ->compute($key, $code) +sub cache_output_compute { + my ($cache, $capture, $key, $code) = @_; + + my $data = $cache->compute($key, sub { + $capture->capture($code); + }); + + return $data; +} + +# for $cache which can ->get($key) and ->set($key, $data) +sub cache_output_get_set { + my ($cache, $capture, $key, $code) = @_; + # check if data is in the cache my $data = $cache->get($key); @@ -47,12 +77,6 @@ sub cache_output { $cache->set($key, $data) if defined $data; } - # print cached data - if (defined $data) { - binmode STDOUT, ':raw'; - print $data; - } - return $data; }