Skip to content

Commit 5a371b7

Browse files
Oblomovgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e9fdd74 commit 5a371b7

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

gitweb/gitweb.perl

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,6 +1512,27 @@ sub format_subject_html {
15121512
}
15131513
}
15141514

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+
15151536
# Insert an avatar for the given $email at the given $size if the feature
15161537
# is enabled.
15171538
sub git_get_avatar {
@@ -1522,8 +1543,7 @@ sub git_get_avatar {
15221543
my $size = $avatar_size{$opts{-size}} || $avatar_size{'default'};
15231544
my $url = "";
15241545
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);
15271547
}
15281548
# Currently only gravatars are supported, but other forms such as
15291549
# picons can be added by putting an else up here and defining $url

0 commit comments

Comments
 (0)