Thread (25 messages) flat view 25 messages, 1 author, 2016-06-15
DORMANTno replies

[PATCH 10/24] gitweb/lib - Use CHI compatibile (compute method) caching interface

From: Jakub Narebski <hidden>
Date: 2016-06-15 22:50:11
Subsystem: the rest · Maintainer: Linus Torvalds

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 <redacted>
---
This patch has less importance that it had before, because when using
GitwebCache::FileCacheWithLocking it is ->compute_fh interface that would
be used, not ->compute.  As far as I understand CHI ->compute looks the
same simple wrapper around ->get/->set that GitwebCache::SimpleFileCache
uses.

On the other hand (as you would see later) ->compute and ->compute_fh have
lot of code in common, and it is easier to test behavior of ->compute in
t9503 tests.

 gitweb/lib/GitwebCache/CacheOutput.pm |   36 +++++++++++++++++++++++++++-----
 1 files changed, 30 insertions(+), 6 deletions(-)
diff --git a/gitweb/lib/GitwebCache/CacheOutput.pm b/gitweb/lib/GitwebCache/CacheOutput.pm
index 458e314..4a96ac9 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;
 }
 
-- 
1.7.3
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help