Convert DjVU cache to WANObjectCache
authorAaron Schulz <aschulz@wikimedia.org>
Mon, 26 Oct 2015 17:43:21 +0000 (10:43 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Wed, 28 Oct 2015 04:47:18 +0000 (04:47 +0000)
Also unified the total and per page dimensions cache

Change-Id: Ib6ad7c3cdfb4fe3a88d25240c3e362664a9b41fd

includes/media/DjVu.php
includes/media/FormatMetadata.php
includes/media/MediaHandler.php

index b422bfa..662c330 100644 (file)
@@ -289,7 +289,7 @@ class DjVuHandler extends ImageHandler {
         * @param bool $gettext DOCUMENT (Default: false)
         * @return bool|SimpleXMLElement
         */
-       function getMetaTree( $image, $gettext = false ) {
+       public function getMetaTree( $image, $gettext = false ) {
                if ( $gettext && isset( $image->djvuTextTree ) ) {
                        return $image->djvuTextTree;
                }
@@ -375,56 +375,52 @@ class DjVuHandler extends ImageHandler {
                return !empty( $metadata ) && $metadata != serialize( array() );
        }
 
-       function pageCount( $image ) {
-               global $wgMemc;
+       function pageCount( File $image ) {
+               $info = $this->getDimensionInfo( $image );
 
-               $key = wfMemcKey( 'file-djvu', 'pageCount', $image->getSha1() );
+               return $info ? $info['pageCount'] : false;
+       }
 
-               $count = $wgMemc->get( $key );
-               if ( $count === false ) {
-                       $tree = $this->getMetaTree( $image );
-                       if ( !$tree ) {
-                               return false;
-                       }
-                       $count = count( $tree->xpath( '//OBJECT' ) );
-                       $wgMemc->set( $key, $count );
+       function getPageDimensions( File $image, $page ) {
+               $index = $page - 1; // MW starts pages at 1
+
+               $info = $this->getDimensionInfo( $image );
+               if ( $info && isset( $info['dimensionsByPage'][$index] ) ) {
+                       return $info['dimensionsByPage'][$index];
                }
 
-               return $count;
+               return false;
        }
 
-       function getPageDimensions( $image, $page ) {
-               global $wgMemc;
-
-               $key = wfMemcKey( 'file-djvu', 'dimensions', $image->getSha1() );
-
-               $dimsByPage = $wgMemc->get( $key );
-               if ( !is_array( $dimsByPage ) ) {
-                       $tree = $this->getMetaTree( $image );
-                       if ( !$tree ) {
-                               return false;
-                       }
+       protected function getDimensionInfo( File $file ) {
+               $that = $this;
 
-                       $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;
+               return ObjectCache::getMainWANInstance()->getWithSetCallback(
+                       wfMemcKey( 'file-djvu', 'dimensions', $file->getSha1() ),
+                       WANObjectCache::TTL_INDEFINITE,
+                       function () use ( $that, $file ) {
+                               $tree = $that->getMetaTree( $file );
+                               if ( !$tree ) {
+                                       return false;
                                }
-                       }
-
-                       $wgMemc->set( $key, $dimsByPage );
-               }
 
-               $index = $page - 1; // MW starts pages at 1
+                               $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;
+                                       }
+                               }
 
-               return isset( $dimsByPage[$index] ) ? $dimsByPage[$index] : false;
+                               return array( 'pageCount' => $count, 'dimensionsByPage' => $dimsByPage );
+                       }
+               );
        }
 
        /**
@@ -432,7 +428,7 @@ class DjVuHandler extends ImageHandler {
         * @param int $page Page number to get information for
         * @return bool|string Page text or false when no text found.
         */
-       function getPageText( $image, $page ) {
+       function getPageText( File $image, $page ) {
                $tree = $this->getMetaTree( $image, true );
                if ( !$tree ) {
                        return false;
index 5b57821..25f4806 100644 (file)
@@ -1571,7 +1571,7 @@ class FormatMetadata extends ContextSource {
         * @since 1.23
         */
        public function fetchExtendedMetadata( File $file ) {
-               global $wgMemc;
+               $cache = ObjectCache::getMainWANInstance();
 
                // If revision deleted, exit immediately
                if ( $file->isDeleted( File::DELETED_FILE ) ) {
@@ -1585,7 +1585,7 @@ class FormatMetadata extends ContextSource {
                        $file->getSha1()
                );
 
-               $cachedValue = $wgMemc->get( $cacheKey );
+               $cachedValue = $cache->get( $cacheKey );
                if (
                        $cachedValue
                        && Hooks::run( 'ValidateExtendedMetadataCache', array( $cachedValue['timestamp'], $file ) )
@@ -1605,7 +1605,7 @@ class FormatMetadata extends ContextSource {
                        // computation on a cache hit.
                        $this->sanitizeArrayForAPI( $extendedMetadata );
                        $valueToCache = array( 'data' => $extendedMetadata, 'timestamp' => wfTimestampNow() );
-                       $wgMemc->set( $cacheKey, $valueToCache, $maxCacheTime );
+                       $cache->set( $cacheKey, $valueToCache, $maxCacheTime );
                }
 
                return $extendedMetadata;
index 015eb5a..bad1468 100644 (file)
@@ -373,7 +373,7 @@ abstract class MediaHandler {
         * @param File $file
         * @return bool
         */
-       function pageCount( $file ) {
+       function pageCount( File $file ) {
                return false;
        }
 
@@ -434,7 +434,7 @@ abstract class MediaHandler {
         * @param int $page What page to get dimensions of
         * @return array|bool
         */
-       function getPageDimensions( $image, $page ) {
+       function getPageDimensions( File $image, $page ) {
                $gis = $this->getImageSize( $image, $image->getLocalRefPath() );
                if ( $gis ) {
                        return array(
@@ -454,7 +454,7 @@ abstract class MediaHandler {
         * @return bool|string Page text or false when no text found or if
         *   unsupported.
         */
-       function getPageText( $image, $page ) {
+       function getPageText( File $image, $page ) {
                return false;
        }