gitweb: gravatar url cache
Views which contain many occurrences of the same email address (e.g. shortlog view) benefit from not having to recalculate the MD5 of the email address every time. Signed-off-by: Giuseppe Bilotta <giuseppe.bilotta@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Giuseppe Bilotta committed
Jun 30, 2009 at 00:00 UTC
5a371b7bbbffeddfb356b327995a95cf7bae71d5
1 file changed
+22
-2
gitweb/gitweb.perl
+22
-2
@@ -1512,6 +1512,27 @@ sub format_subject_html {
1512
}
1513
}
1514
1515
+# Rather than recomputing the url for an email multiple times, we cache it
1516
+# after the first hit. This gives a visible benefit in views where the avatar
1517
+# for the same email is used repeatedly (e.g. shortlog).
1518
+# The cache is shared by all avatar engines (currently gravatar only), which
1519
+# are free to use it as preferred. Since only one avatar engine is used for any
1520
+# given page, there's no risk for cache conflicts.
1521
+our %avatar_cache = ();
1522
+
1523
+# Compute the gravatar url for a given email, if it's not in the cache already.
1524
+# Gravatar stores only the part of the URL before the size, since that's the
1525
+# one computationally more expensive. This also allows reuse of the cache for
1526
+# different sizes (for this particular engine).
1527
+sub gravatar_url {
1528
+ my $email = lc shift;
1529
+ my $size = shift;
1530
+ $avatar_cache{$email} ||=
1531
+ "http://www.gravatar.com/avatar/" .
1532
+ Digest::MD5::md5_hex($email) . "?s=";
1533
+ return $avatar_cache{$email} . $size;
1534
+}
1535
+
1536
# Insert an avatar for the given $email at the given $size if the feature
1537
# is enabled.
1538
sub git_get_avatar {
@@ -1522,8 +1543,7 @@ sub git_get_avatar {
1543
my $size = $avatar_size{$opts{-size}} || $avatar_size{'default'};
1544
my $url = "";
1545
if ($git_avatar eq 'gravatar') {
1525
- $url = "http://www.gravatar.com/avatar/" .
1526
- Digest::MD5::md5_hex(lc $email) . "?s=$size";
1546
+ $url = gravatar_url($email, $size);
1547
}
1548
# Currently only gravatars are supported, but other forms such as
1549
# picons can be added by putting an else up here and defining $url