Tweaked "latest" handling of filebackend stat entries
authorAaron Schulz <aschulz@wikimedia.org>
Sat, 15 Mar 2014 09:08:04 +0000 (02:08 -0700)
committerOri.livneh <ori@wikimedia.org>
Mon, 24 Mar 2014 06:55:01 +0000 (06:55 +0000)
* Let "latest" stat entries override non "latest" so
  that future getFileStat() calls with the "latest" flag
  can actually have a cache hit.

Change-Id: I1e9391039537d608b89773b4d51575e3b364a751

includes/filebackend/FileBackendStore.php

index 2d8214e..2fd1bf6 100644 (file)
@@ -1666,7 +1666,22 @@ abstract class FileBackendStore extends FileBackend {
                }
                $age = time() - wfTimestamp( TS_UNIX, $val['mtime'] );
                $ttl = min( 7 * 86400, max( 300, floor( .1 * $age ) ) );
-               $this->memCache->add( $this->fileCacheKey( $path ), $val, $ttl );
+               $key = $this->fileCacheKey( $path );
+               // Set the cache unless it is currently salted with the value "PURGED".
+               // Using add() handles this except it also is a no-op in that case where
+               // the current value is not "latest" but $val is, so use CAS in that case.
+               if ( !$this->memCache->add( $key, $val, $ttl ) && !empty( $val['latest'] ) ) {
+                       $this->memCache->merge(
+                               $key,
+                               function( BagOStuff $cache, $key, $cValue ) use ( $val ) {
+                                       return ( is_array( $cValue ) && empty( $cValue['latest'] ) )
+                                               ? $val // update the stat cache with the lastest info
+                                               : false; // do nothing (cache is salted or some error happened)
+                               },
+                               $ttl,
+                               1
+                       );
+               }
        }
 
        /**