* Added isCacheWorthy() optimization (checks if the file exists, stale or not)
authorAaron Schulz <aaron@users.mediawiki.org>
Sun, 2 Oct 2011 19:44:31 +0000 (19:44 +0000)
committerAaron Schulz <aaron@users.mediawiki.org>
Sun, 2 Oct 2011 19:44:31 +0000 (19:44 +0000)
* Made isCached() use process cache
* Added MISS_TTL_SEC constant and tweaked MISS_FACTOR constant

includes/cache/FileCacheBase.php
includes/cache/HTMLFileCache.php
includes/cache/ResourceFileCache.php

index 1ed9546..e6be049 100644 (file)
@@ -10,9 +10,12 @@ abstract class FileCacheBase {
        protected $mExt = 'cache';
        protected $mFilePath;
        protected $mUseGzip;
+       /* lazy loaded */
+       protected $mCached;
 
        /* @TODO: configurable? */
-       const MISS_FACTOR = 10; // log 1 every MISS_FACTOR cache misses
+       const MISS_FACTOR = 15; // log 1 every MISS_FACTOR cache misses
+       const MISS_TTL_SEC = 3600; // how many seconds ago is "recent"
 
        protected function __construct() {
                global $wgUseGzip;
@@ -70,7 +73,10 @@ abstract class FileCacheBase {
         * @return bool
         */
        public function isCached() {
-               return file_exists( $this->cachePath() );
+               if ( $this->mCached === null ) {
+                       $this->mCached = file_exists( $this->cachePath() );
+               }
+               return $this->mCached;
        }
 
        /**
@@ -142,9 +148,11 @@ abstract class FileCacheBase {
 
                $this->checkCacheDirs(); // build parent dir
                if ( !file_put_contents( $this->cachePath(), $text, LOCK_EX ) ) {
+                       $this->mCached = null;
                        return false;
                }
 
+               $this->mCached = true;
                return $text;
        }
 
@@ -156,6 +164,7 @@ abstract class FileCacheBase {
                wfSuppressWarnings();
                unlink( $this->cachePath() );
                wfRestoreWarnings();
+               $this->mCached = false;
        }
 
        /**
@@ -216,12 +225,12 @@ abstract class FileCacheBase {
                        if ( $wgMemc->get( $key ) ) {
                                return; // possibly the same user
                        }
-                       $wgMemc->set( $key, 1, 3600 );
+                       $wgMemc->set( $key, 1, self::MISS_TTL_SEC );
 
                        # Increment the number of cache misses...
                        $key = $this->cacheMissKey();
                        if ( $wgMemc->get( $key ) === false ) {
-                               $wgMemc->set( $key, 1, 3600 );
+                               $wgMemc->set( $key, 1, self::MISS_TTL_SEC );
                        } else {
                                $wgMemc->incr( $key );
                        }
index ac63a2a..532850a 100644 (file)
@@ -75,14 +75,14 @@ class HTMLFileCache extends FileCacheBase {
                // Get all query values
                $queryVals = $context->getRequest()->getValues();
                foreach ( $queryVals as $query => $val ) {
-                       if ( $query == 'title' || $query == 'curid' ) {
+                       if ( $query === 'title' || $query === 'curid' ) {
                                continue; // note: curid sets title
                        // Normal page view in query form can have action=view.
                        // Raw hits for pages also stored, like .css pages for example.
-                       } elseif ( $query == 'action' && in_array( $val, self::cacheablePageActions() ) ) {
+                       } elseif ( $query === 'action' && in_array( $val, self::cacheablePageActions() ) ) {
                                continue;
                        // Below are header setting params
-                       } elseif ( $query == 'maxage' || $query == 'smaxage' ) {
+                       } elseif ( $query === 'maxage' || $query === 'smaxage' ) {
                                continue;
                        }
                        return false;
index 3d725e7..3609847 100644 (file)
@@ -72,12 +72,15 @@ class ResourceFileCache extends FileCacheBase {
        }
 
        /**
-        * Recent cache misses
+        * Item has many recent cache misses
         * @return bool
         */
        public function isCacheWorthy() {
                if ( $this->mCacheWorthy === null ) {
-                       $this->mCacheWorthy = ( $this->getMissesRecent() >= self::MISS_THRESHOLD );
+                       $this->mCacheWorthy = (
+                               $this->isCached() || // even stale cache indicates it was cache worthy
+                               $this->getMissesRecent() >= self::MISS_THRESHOLD // many misses
+                       );
                }
                return $this->mCacheWorthy;
        }