[FileBackend] Made stat cache factor in last-modified time.
authorAaron Schulz <aschulz@wikimedia.org>
Sun, 14 Apr 2013 20:32:34 +0000 (13:32 -0700)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 19 Apr 2013 03:32:57 +0000 (03:32 +0000)
* This should lesson the chance of consistency problems
  if there is any memcached flapping.
* Also cleaned up some documentation.

Change-Id: I89401892c7e848fcff0b093eaa861dfe02387a5c

includes/filebackend/FileBackendStore.php

index 46ec177..6d142b9 100644 (file)
@@ -1581,9 +1581,10 @@ abstract class FileBackendStore extends FileBackend {
         * Set the cached info for a container
         *
         * @param string $container Resolved container name
-        * @param $val mixed Information to cache
+        * @param array $val Information to cache
+        * @return void
         */
-       final protected function setContainerCache( $container, $val ) {
+       final protected function setContainerCache( $container, array $val ) {
                $this->memCache->add( $this->containerCacheKey( $container ), $val, 14 * 86400 );
        }
 
@@ -1592,6 +1593,7 @@ abstract class FileBackendStore extends FileBackend {
         * The cache key is salted for a while to prevent race conditions.
         *
         * @param string $container Resolved container name
+        * @return void
         */
        final protected function deleteContainerCache( $container ) {
                if ( !$this->memCache->set( $this->containerCacheKey( $container ), 'PURGED', 300 ) ) {
@@ -1672,14 +1674,17 @@ abstract class FileBackendStore extends FileBackend {
         * salting for the case when a file is created at a path were there was none before.
         *
         * @param string $path Storage path
-        * @param $val mixed Information to cache
+        * @param array $val Stat information to cache
+        * @return void
         */
-       final protected function setFileCache( $path, $val ) {
+       final protected function setFileCache( $path, array $val ) {
                $path = FileBackend::normalizeStoragePath( $path );
                if ( $path === null ) {
                        return; // invalid storage path
                }
-               $this->memCache->add( $this->fileCacheKey( $path ), $val, 7 * 86400 );
+               $age = time() - wfTimestamp( TS_UNIX, $val['mtime'] );
+               $ttl = min( 7 * 86400, max( 300, floor( .1 * $age ) ) );
+               $this->memCache->add( $this->fileCacheKey( $path ), $val, $ttl );
        }
 
        /**
@@ -1689,6 +1694,7 @@ abstract class FileBackendStore extends FileBackend {
         * a file is created at a path were there was none before.
         *
         * @param string $path Storage path
+        * @return void
         */
        final protected function deleteFileCache( $path ) {
                $path = FileBackend::normalizeStoragePath( $path );