]> git.donarmstrong.com Git - debbugs.git/blobdiff - Debbugs/Libravatar.pm
Libravatar: Refactor cache handling to avoid unncessary stat
[debbugs.git] / Debbugs / Libravatar.pm
index 8dcbad09033055b925a631b077005c4b89163014..0dafaf27adbdee63fc7634dd243223d98be49151 100644 (file)
@@ -31,7 +31,7 @@ None known.
 use warnings;
 use strict;
 use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT);
-use base qw(Exporter);
+use Exporter qw(import);
 
 use Debbugs::Config qw(:config);
 use Debbugs::Common qw(:lock);
@@ -39,7 +39,6 @@ use Libravatar::URL;
 use CGI::Simple;
 use Debbugs::CGI qw(cgi_parameters);
 use Digest::MD5 qw(md5_hex);
-use LWP::UserAgent;
 use File::Temp qw(tempfile);
 use File::LibMagic;
 use Cwd qw(abs_path);
@@ -51,22 +50,13 @@ BEGIN{
      $DEBUG = 0 unless defined $DEBUG;
 
      @EXPORT = ();
-     %EXPORT_TAGS = (libravatar => [qw(cache_valid retrieve_libravatar cache_location)]
+     %EXPORT_TAGS = (libravatar => [qw(retrieve_libravatar cache_location)]
                    );
      @EXPORT_OK = ();
      Exporter::export_ok_tags(keys %EXPORT_TAGS);
      $EXPORT_TAGS{all} = [@EXPORT_OK];
 }
 
-sub cache_valid{
-    my ($cache_location) = @_;
-    if (-e $cache_location) {
-        if (time - (stat($cache_location))[9] < 60*60) {
-            return 1;
-        }
-    }
-    return 0;
-}
 
 =over
 
@@ -104,11 +94,13 @@ sub retrieve_libravatar{
     } else {
         # figure out if the cache is now valid; if it is, return the
         # cache location
-        my $temp_location = cache_location(email => $param{email});
-        if (cache_valid($temp_location)) {
+        my ($temp_location, $is_valid) = cache_location(email => $param{email});
+        if ($is_valid) {
             return $temp_location;
         }
     }
+    require LWP::UserAgent;
+
     my $dest_type;
     eval {
         my $uri = libravatar_url(email => $param{email},
@@ -193,9 +185,13 @@ sub blocked_libravatar {
     return $blocked;
 }
 
+# Returns ($path, $is_valid)
+# - For blocked images, $path will be undef
+# - If $is_valid is false (and $path is not undef), the image should
+#   be re-fetched.
 sub cache_location {
     my %param = @_;
-    my $md5sum;
+    my ($md5sum, $stem);
     if (exists $param{md5sum}) {
         $md5sum = $param{md5sum};
     }elsif (exists $param{email}) {
@@ -203,13 +199,16 @@ sub cache_location {
     } else {
         croak("cache_location must be called with one of md5sum or email");
     }
-    return undef if blocked_libravatar($param{email},$md5sum);
-    for my $ext (qw(.png .jpg)) {
-        if (-e $config{libravatar_cache_dir}.'/'.$md5sum.$ext) {
-            return $config{libravatar_cache_dir}.'/'.$md5sum.$ext;
+    return (undef, 0) if blocked_libravatar($param{email},$md5sum);
+    $stem = $config{libravatar_cache_dir}.'/'.$md5sum;
+    for my $ext ('.png', '.jpg', '') {
+        my $path = $stem.$ext;
+        if (-e $path) {
+            my $is_valid = (time - (stat(_))[9] < 60*60) ? 1 : 0;
+            return ($path, $is_valid);
         }
     }
-    return $config{libravatar_cache_dir}.'/'.$md5sum;
+    return ($stem, 0);
 }
 
 ## the following is mod_perl specific
@@ -259,9 +258,9 @@ sub handler {
         return Apache2::Const::DECLINED();
     }
     # figure out what the md5sum of the e-mail is.
-    my $cache_location = cache_location(email => $email);
+    my ($cache_location, $is_valid) = cache_location(email => $email);
     # if we've got it, and it's less than one hour old, return it.
-    if (cache_valid($cache_location)) {
+    if ($is_valid) {
         serve_cache_mod_perl($cache_location,$r);
         return Apache2::Const::DECLINED();
     }