From 1ba93d9b77b06daba56b21ed4af13eff9c2beb8e Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Sun, 2 Oct 2011 19:44:31 +0000 Subject: [PATCH] * Added isCacheWorthy() optimization (checks if the file exists, stale or not) * Made isCached() use process cache * Added MISS_TTL_SEC constant and tweaked MISS_FACTOR constant --- includes/cache/FileCacheBase.php | 17 +++++++++++++---- includes/cache/HTMLFileCache.php | 6 +++--- includes/cache/ResourceFileCache.php | 7 +++++-- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/includes/cache/FileCacheBase.php b/includes/cache/FileCacheBase.php index 1ed9546f93..e6be049335 100644 --- a/includes/cache/FileCacheBase.php +++ b/includes/cache/FileCacheBase.php @@ -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 ); } diff --git a/includes/cache/HTMLFileCache.php b/includes/cache/HTMLFileCache.php index ac63a2a5b3..532850a596 100644 --- a/includes/cache/HTMLFileCache.php +++ b/includes/cache/HTMLFileCache.php @@ -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; diff --git a/includes/cache/ResourceFileCache.php b/includes/cache/ResourceFileCache.php index 3d725e72a7..36098475e0 100644 --- a/includes/cache/ResourceFileCache.php +++ b/includes/cache/ResourceFileCache.php @@ -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; } -- 2.20.1