Thread (55 messages) flat view 55 messages, 3 authors, 2016-06-15
STALE3733d

[RFC PATCH 07/10] gitweb: Use CHI compatibile (compute method) caching (WIP)

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

Signed-off-by: Jakub Narebski <redacted>
---
This patch, as you can see, lack proper commit message: it is work in
progress.

The reason behind supporting compute() as interface to cache is that
with this interface it is possible (as it can be seen in the following
patch) to use locking to avoid cache miss stampede (only one process
regenerates cache).

The support for $cache which do not provide '->compute($key, $code)'
method is left just in case we would want to use such (external)
caching engine.  We should probably add generic_compute() subroutine
which would use get/set, and provide compute-like interface.

 gitweb/cache.pm |   53 +++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 43 insertions(+), 10 deletions(-)
diff --git a/gitweb/cache.pm b/gitweb/cache.pm
index 9692e8d..8dd4f39 100644
--- a/gitweb/cache.pm
+++ b/gitweb/cache.pm
@@ -432,6 +432,48 @@ sub cache_fetch {
 	my ($cache, $action) = @_;
 
 	my $key = gitweb_output_key();
+	if ($cache->can('compute')) {
+		cache_fetch_compute($cache, $action, $key);
+	} else {
+		cache_fetch_get_set($cache, $action, $key);
+	}
+}
+
+# calculate data to regenerate cache
+sub cache_calculate {
+	my ($action) = @_;
+
+	my $data;
+	open my $data_fh, '>', \$data
+		or die "Can't open memory file: $!";
+	# matches "binmode STDOUT, ':uft8'" at beginning
+	binmode $data_fh, ':utf8';
+
+	$out = $data_fh || \*STDOUT;
+	$actions{$action}->();
+
+	close $data_fh;
+
+	return $data;
+}
+
+# for $cache which can ->compute($key, $code)
+sub cache_fetch_compute {
+	my ($cache, $action, $key) = @_;
+
+	my $data = $cache->compute($key, sub { cache_calculate($action) });
+
+	if (defined $data) {
+		# print cached data
+		binmode STDOUT, ':raw';
+		local $/ = undef;
+		print STDOUT $data;
+	}
+}
+
+# for $cache which can ->get($key) and ->set($key, $data)
+sub cache_fetch_get_set {
+	my ($cache, $action, $key) = @_;
+
 	my $data = $cache->get($key);
 
 	if (defined $data) {
@@ -440,22 +482,13 @@ sub cache_fetch {
 		print STDOUT $data;
 
 	} else {
-		# calculate data and regenerate data
-		open my $data_fh, '>', \$data
-			or die "Can't open memory file: $!";
-		# matches "binmode STDOUT, ':uft8'" at beginning
-		binmode $data_fh, ':utf8';
-
-		$out = $data_fh || \*STDOUT;
-		$actions{$action}->();
+		$data = cache_calculate($action);
 
 		if (defined $data) {
 			$cache->set($key, $data);
 			binmode STDOUT, ':raw';
 			local $/ = undef;
 			print STDOUT $data;
 		}
-
-		close $data_fh;
 	}
 }
 
-- 
1.6.6.1
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help