Added common metadata caching to the djvu handler
authorAaron Schulz <aschulz@wikimedia.org>
Sun, 10 May 2015 19:27:38 +0000 (12:27 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Mon, 11 May 2015 16:15:00 +0000 (09:15 -0700)
* This should reduce expensive metadata fetches
  for djvu files with large (~2mb) img_metadata

Bug: T96360
Change-Id: I7273ea083f90948f8c1d8948a64929bfb759ee72

includes/media/DjVu.php

index 011fb2a..a45f6e8 100644 (file)
@@ -365,29 +365,55 @@ class DjVuHandler extends ImageHandler {
        }
 
        function pageCount( $image ) {
-               $tree = $this->getMetaTree( $image );
-               if ( !$tree ) {
-                       return false;
+               global $wgMemc;
+
+               $key = wfMemcKey( 'file-djvu', 'pageCount', $image->getSha1() );
+
+               $count = $wgMemc->get( $key );
+               if ( $count === false ) {
+                       $tree = $this->getMetaTree( $image );
+                       if ( !$tree ) {
+                               return false;
+                       }
+                       $count = count( $tree->xpath( '//OBJECT' ) );
+                       $wgMemc->set( $key, $count );
                }
 
-               return count( $tree->xpath( '//OBJECT' ) );
+               return $count;
        }
 
        function getPageDimensions( $image, $page ) {
-               $tree = $this->getMetaTree( $image );
-               if ( !$tree ) {
-                       return false;
-               }
+               global $wgMemc;
 
-               $o = $tree->BODY[0]->OBJECT[$page - 1];
-               if ( $o ) {
-                       return array(
-                               'width' => intval( $o['width'] ),
-                               'height' => intval( $o['height'] )
-                       );
-               } else {
-                       return false;
+               $key = wfMemcKey( 'file-djvu', 'dimensions', $image->getSha1() );
+
+               $dimsByPage = $wgMemc->get( $key );
+               if ( !is_array( $dimsByPage ) ) {
+                       $tree = $this->getMetaTree( $image );
+                       if ( !$tree ) {
+                               return false;
+                       }
+
+                       $dimsByPage = array();
+                       $count = count( $tree->xpath( '//OBJECT' ) );
+                       for ( $i = 0; $i < $count; ++$i ) {
+                               $o = $tree->BODY[0]->OBJECT[$i];
+                               if ( $o ) {
+                                       $dimsByPage[$i] = array(
+                                               'width' => (int)$o['width'],
+                                               'height' => (int)$o['height']
+                                       );
+                               } else {
+                                       $dimsByPage[$i] = false;
+                               }
+                       }
+
+                       $wgMemc->set( $key, $dimsByPage );
                }
+
+               $index = $page - 1; // MW starts pages at 1
+
+               return isset( $dimsByPage[$index] ) ? $dimsByPage[$index] : false;
        }
 
        /**