Convert File::getDescriptionText() to getWithSetCallback()
authorAaron Schulz <aschulz@wikimedia.org>
Tue, 12 Apr 2016 23:21:52 +0000 (16:21 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Tue, 12 Apr 2016 23:21:52 +0000 (16:21 -0700)
Change-Id: Ia27d6fc851598162367ad6e749feb6d76f2569bc

includes/filerepo/file/File.php

index 7e00793..c037516 100644 (file)
@@ -2046,34 +2046,29 @@ abstract class File implements IDBAccessObject {
                $renderUrl = $this->repo->getDescriptionRenderUrl( $this->getName(), $lang->getCode() );
                if ( $renderUrl ) {
                        $cache = ObjectCache::getMainWANInstance();
+                       $key = $this->repo->getLocalCacheKey(
+                               'RemoteFileDescription',
+                               'url',
+                               $lang->getCode(),
+                               $this->getName()
+                       );
 
-                       $key = null;
-                       if ( $this->repo->descriptionCacheExpiry > 0 ) {
-                               wfDebug( "Attempting to get the description from cache..." );
-                               $key = $this->repo->getLocalCacheKey(
-                                       'RemoteFileDescription',
-                                       'url',
-                                       $lang->getCode(),
-                                       $this->getName()
-                               );
-                               $obj = $cache->get( $key );
-                               if ( $obj ) {
-                                       wfDebug( "success!\n" );
-
-                                       return $obj;
-                               }
-                               wfDebug( "miss\n" );
-                       }
-                       wfDebug( "Fetching shared description from $renderUrl\n" );
-                       $res = Http::get( $renderUrl, [], __METHOD__ );
-                       if ( $res && $key ) {
-                               $cache->set( $key, $res, $this->repo->descriptionCacheExpiry );
-                       }
+                       return $cache->getWithSetCallback(
+                               $key,
+                               $this->repo->descriptionCacheExpiry ?: $cache::TTL_UNCACHEABLE,
+                               function ( $oldValue, &$ttl, array &$setOpts ) use ( $renderUrl ) {
+                                       wfDebug( "Fetching shared description from $renderUrl\n" );
+                                       $res = Http::get( $renderUrl, [], __METHOD__ );
+                                       if ( !$res ) {
+                                               $ttl = WANObjectCache::TTL_UNCACHEABLE;
+                                       }
 
-                       return $res;
-               } else {
-                       return false;
+                                       return $res;
+                               }
+                       );
                }
+
+               return false;
        }
 
        /**