[RFC PATCHv3 06/10] gitweb: Use CHI compatibile (compute method) caching

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



If $cache provides CHI compatible ->compute($key, $code) method, use it
instead of Cache::Cache compatible ->get($key) and ->set($key, $data).
While at it, refactor regenerating cache into cache_calculate subroutine.

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.

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.

Signed-off-by: Jakub Narebski <jnareb@xxxxxxxxx>
---
This commit could be moved one commit earlier in this patch series for
shortlog to look nicer (although commit message would have to be adjusted to
that fact) ;-)

Differences from v2:
* Update/change signature of cache_fetch_compute and cache_fetch_get_set
* cache_fetch_compute updated to use new output capturing mechanism
* Fixed cache_fetch_get_set

This patch doesn't strictly have an equivalent in J.H. patch adding caching
to gitweb.

 gitweb/cache.pm |   46 ++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 42 insertions(+), 4 deletions(-)

diff --git a/gitweb/cache.pm b/gitweb/cache.pm
index dcddd28..b828102 100644
--- a/gitweb/cache.pm
+++ b/gitweb/cache.pm
@@ -455,12 +455,50 @@ sub cache_capture (&) {
 sub cache_fetch {
 	my ($cache, $key, $code) = @_;
 
-	my $data = $cache->get($key);
+	if ($cache->can('compute')) {
+		#goto &cache_fetch_compute
+		cache_fetch_compute($cache, $key, $code);
+	} else {
+		#goto &cache_fetch_get_set
+		cache_fetch_get_set($cache, $key, $code);
+	}
+}
+
+# calculate data to regenerate cache via capturing output
+# (probably unnecessary level of indirection)
+sub cache_calculate {
+	my $code = shift;
+
+	my $data = cache_capture {
+		$code->();
+	};
+
+	return $data;
+}
+
+# for $cache which can ->compute($key, $code)
+sub cache_fetch_compute {
+	my ($cache, $key, $code) = @_;
+
+	my $data = $cache->compute($key, sub { cache_calculate($code) });
 
+	if (defined $data) {
+		# print cached data
+		#binmode STDOUT, ':raw';
+		#print STDOUT $data;
+		# for t9503 test:
+		binmode select(), ':raw';
+		print $data;
+	}
+}
+
+# for $cache which can ->get($key) and ->set($key, $data)
+sub cache_fetch_get_set {
+	my ($cache, $key, $code) = @_;
+
+	my $data = $cache->get($key);
 	if (!defined $data) {
-		$data = cache_capture {
-			$code->();
-		};
+		$data = cache_calculate($code);
 		$cache->set($key, $data) if defined $data;
 	}
 
-- 
1.6.6.1

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]