Made SwiftFileBackend::loadObjectListing() populate stat entries in reverse order.
authorAaron Schulz <aschulz@wikimedia.org>
Sun, 19 May 2013 20:15:50 +0000 (13:15 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Sun, 19 May 2013 20:15:50 +0000 (13:15 -0700)
Change-Id: I07bf9c852729d65cee72364ca3249e31936d0562

includes/filebackend/SwiftFileBackend.php

index f9e2ce4..54a90d3 100644 (file)
@@ -1111,7 +1111,11 @@ class SwiftFileBackend extends FileBackendStore {
                $names = array();
                $storageDir = rtrim( $params['dir'], '/' );
                $suffixStart = ( $dir === '' ) ? 0 : strlen( $dir ) + 1; // size of "path/to/dir/"
-               foreach ( $cfObjects as $object ) {
+               // Iterate over the list *backwards* as this primes the stat cache, which is LRU.
+               // If this fills the cache and the caller stats an uncached file before stating
+               // the ones on the listing, there would be zero cache hits if this went forwards.
+               for ( end( $cfObjects ); key( $cfObjects ) !== null; prev( $cfObjects ) ) {
+                       $object = current( $cfObjects );
                        $path = "{$storageDir}/" . substr( $object->name, $suffixStart );
                        $val = array(
                                // Convert dates like "Tue, 03 Jan 2012 22:01:04 GMT" to TS_MW
@@ -1122,7 +1126,7 @@ class SwiftFileBackend extends FileBackendStore {
                        $this->cheapCache->set( $path, 'stat', $val );
                        $names[] = $object->name;
                }
-               return $names;
+               return array_reverse( $names ); // keep the paths in original order
        }
 
        /**