objectcache: Introduce IExpiringStore for convenient TTL constants
authorTimo Tijhof <krinklemail@gmail.com>
Mon, 19 Oct 2015 17:52:19 +0000 (18:52 +0100)
committerTimo Tijhof <krinklemail@gmail.com>
Wed, 28 Oct 2015 04:07:25 +0000 (04:07 +0000)
Also consistently use self:: instead of BagOStuff:: for constants
referenced within the BagOStuff class.

Change-Id: I20fde9fa5cddcc9e92fa6a02b05dc7effa846742

16 files changed:
autoload.php
includes/Block.php
includes/Collation.php
includes/SiteStats.php
includes/User.php
includes/filerepo/ForeignAPIRepo.php
includes/jobqueue/JobQueueGroup.php
includes/libs/objectcache/BagOStuff.php
includes/libs/objectcache/IExpiringStore.php [new file with mode: 0644]
includes/libs/objectcache/WANObjectCache.php
includes/media/TransformationalImageHandler.php
includes/page/WikiPage.php
includes/parser/DateFormatter.php
includes/specials/SpecialUserlogin.php
includes/upload/UploadBase.php
thumb.php

index d7bccb3..2bcf6b5 100644 (file)
@@ -541,6 +541,7 @@ $wgAutoloadLocalClasses = array(
        'IDatabase' => __DIR__ . '/includes/db/IDatabase.php',
        'IEContentAnalyzer' => __DIR__ . '/includes/libs/IEContentAnalyzer.php',
        'IEUrlExtension' => __DIR__ . '/includes/libs/IEUrlExtension.php',
+       'IExpiringStore' => __DIR__ . '/includes/libs/objectcache/IExpiringStore.php',
        'IJobSpecification' => __DIR__ . '/includes/jobqueue/JobSpecification.php',
        'IORMRow' => __DIR__ . '/includes/db/IORMRow.php',
        'IORMTable' => __DIR__ . '/includes/db/IORMTable.php',
index 5dee23e..b57b3e8 100644 (file)
@@ -681,10 +681,10 @@ class Block {
        public static function isWhitelistedFromAutoblocks( $ip ) {
                // Try to get the autoblock_whitelist from the cache, as it's faster
                // than getting the msg raw and explode()'ing it.
-
-               $lines = ObjectCache::getMainWANInstance()->getWithSetCallback(
+               $cache = ObjectCache::getMainWANInstance();
+               $lines = $cache->getWithSetCallback(
                        wfMemcKey( 'ipb', 'autoblock', 'whitelist' ),
-                       86400,
+                       $cache::TTL_DAY,
                        function () {
                                return explode( "\n",
                                        wfMessage( 'autoblock_whitelist' )->inContentLanguage()->plain() );
index 40e8627..7fa5c6e 100644 (file)
@@ -508,7 +508,7 @@ class IcuCollation extends Collation {
 
                // Save to cache
                $this->firstLetterData = $data;
-               $cache->set( $cacheKey, $data, 86400 * 7 /* 1 week */ );
+               $cache->set( $cacheKey, $data, $cache::TTL_WEEK );
                return $data;
        }
 
index 5b361b9..33bab65 100644 (file)
@@ -180,9 +180,10 @@ class SiteStats {
         * @return int
         */
        static function numberingroup( $group ) {
-               return ObjectCache::getMainWANInstance()->getWithSetCallback(
+               $cache = ObjectCache::getMainWANInstance();
+               return $cache->getWithSetCallback(
                        wfMemcKey( 'SiteStats', 'groupcounts', $group ),
-                       3600,
+                       $cache::TTL_HOUR,
                        function ( $oldValue, &$ttl, array &$setOpts ) use ( $group ) {
                                $dbr = wfGetDB( DB_SLAVE );
 
index 1727a4a..a6b897d 100644 (file)
@@ -438,10 +438,11 @@ class User implements IDBAccessObject {
                        $data[$name] = $this->$name;
                }
                $data['mVersion'] = self::VERSION;
-               $key = wfMemcKey( 'user', 'id', $this->mId );
-
                $opts = Database::getCacheSetOptions( wfGetDB( DB_SLAVE ) );
-               ObjectCache::getMainWANInstance()->set( $key, $data, 3600, $opts );
+
+               $cache = ObjectCache::getMainWANInstance();
+               $key = wfMemcKey( 'user', 'id', $this->mId );
+               $cache->set( $key, $data, $cache::TTL_HOUR, $opts );
        }
 
        /** @name newFrom*() static factory methods */
index 0a3314e..f898bb6 100644 (file)
@@ -55,11 +55,11 @@ class ForeignAPIRepo extends FileRepo {
        );
 
        protected $fileFactory = array( 'ForeignAPIFile', 'newFromTitle' );
-       /** @var int Check back with Commons after a day (24*60*60) */
-       protected $apiThumbCacheExpiry = 86400;
+       /** @var int Check back with Commons after this expiry */
+       protected $apiThumbCacheExpiry = 86400; // 1 day (24*3600)
 
-       /** @var int Redownload thumbnail files after a month (86400*30) */
-       protected $fileCacheExpiry = 2592000;
+       /** @var int Redownload thumbnail files after this expiry */
+       protected $fileCacheExpiry = 2592000; // 1 month (30*24*3600)
 
        /** @var array */
        protected $mFileExists = array();
index a702d59..4609d2d 100644 (file)
@@ -394,7 +394,11 @@ class JobQueueGroup {
                                return $value['v'];
                        } else {
                                $value = $wgConf->getConfig( $this->wiki, $name );
-                               $cache->set( $key, array( 'v' => $value ), 86400 + mt_rand( 0, 86400 ) );
+                               $cache->set(
+                                       $key,
+                                       array( 'v' => $value ),
+                                       $cache::TTL_DAY + mt_rand( 0, $cache::TTL_DAY )
+                               );
 
                                return $value;
                        }
index ecc5e37..703c195 100644 (file)
@@ -42,7 +42,7 @@ use Psr\Log\NullLogger;
  *
  * @ingroup Cache
  */
-abstract class BagOStuff implements LoggerAwareInterface {
+abstract class BagOStuff implements IExpiringStore, LoggerAwareInterface {
        /** @var array[] Lock tracking */
        protected $locks = array();
 
@@ -220,7 +220,7 @@ abstract class BagOStuff implements LoggerAwareInterface {
                do {
                        $this->clearLastError();
                        $casToken = null; // passed by reference
-                       $currentValue = $this->getWithToken( $key, $casToken, BagOStuff::READ_LATEST );
+                       $currentValue = $this->getWithToken( $key, $casToken, self::READ_LATEST );
                        if ( $this->getLastError() ) {
                                return false; // don't spam retries (retry only on races)
                        }
@@ -276,7 +276,7 @@ abstract class BagOStuff implements LoggerAwareInterface {
                }
 
                $this->clearLastError();
-               $currentValue = $this->get( $key, BagOStuff::READ_LATEST );
+               $currentValue = $this->get( $key, self::READ_LATEST );
                if ( $this->getLastError() ) {
                        $success = false;
                } else {
@@ -319,7 +319,7 @@ abstract class BagOStuff implements LoggerAwareInterface {
                        }
                }
 
-               $expiry = min( $expiry ?: INF, 86400 );
+               $expiry = min( $expiry ?: INF, self::TTL_DAY );
 
                $this->clearLastError();
                $timestamp = microtime( true ); // starting UNIX timestamp
@@ -389,7 +389,7 @@ abstract class BagOStuff implements LoggerAwareInterface {
         * @since 1.26
         */
        final public function getScopedLock( $key, $timeout = 6, $expiry = 30, $rclass = '' ) {
-               $expiry = min( $expiry ?: INF, 86400 );
+               $expiry = min( $expiry ?: INF, self::TTL_DAY );
 
                if ( !$this->lock( $key, $timeout, $expiry, $rclass ) ) {
                        return null;
@@ -582,7 +582,7 @@ abstract class BagOStuff implements LoggerAwareInterface {
         * @return int
         */
        protected function convertExpiry( $exptime ) {
-               if ( ( $exptime != 0 ) && ( $exptime < 86400 * 3650 /* 10 years */ ) ) {
+               if ( $exptime != 0 && $exptime < ( 10 * self::TTL_YEAR ) ) {
                        return time() + $exptime;
                } else {
                        return $exptime;
@@ -597,7 +597,7 @@ abstract class BagOStuff implements LoggerAwareInterface {
         * @return int
         */
        protected function convertToRelative( $exptime ) {
-               if ( $exptime >= 86400 * 3650 /* 10 years */ ) {
+               if ( $exptime >= ( 10 * self::TTL_YEAR ) ) {
                        $exptime -= time();
                        if ( $exptime <= 0 ) {
                                $exptime = 1;
diff --git a/includes/libs/objectcache/IExpiringStore.php b/includes/libs/objectcache/IExpiringStore.php
new file mode 100644 (file)
index 0000000..b5ad702
--- /dev/null
@@ -0,0 +1,40 @@
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup Cache
+ * @author 2015 Timo Tijhof
+ */
+
+/**
+ * Generic base class for storage interfaces.
+ *
+ * Provides convenient TTL constants.
+ *
+ * @ingroup Cache
+ * @since 1.27
+ */
+interface IExpiringStore {
+
+       // Constants for TTL values, in seconds
+       const TTL_MINUTE = 60;
+       const TTL_HOUR = 3600;
+       const TTL_DAY = 86400; // 24 * 3600
+       const TTL_WEEK = 604800; // 7 * 24 * 3600
+       const TTL_MONTH = 2592000; // 30 * 24 * 3600
+       const TTL_YEAR = 31536000; // 365 * 24 * 3600
+}
index 0c7b7ed..506c94e 100644 (file)
@@ -64,7 +64,7 @@ use Psr\Log\NullLogger;
  * @ingroup Cache
  * @since 1.26
  */
-class WANObjectCache implements LoggerAwareInterface {
+class WANObjectCache implements IExpiringStore, LoggerAwareInterface {
        /** @var BagOStuff The local datacenter cache */
        protected $cache;
        /** @var HashBagOStuff Script instance PHP cache */
@@ -89,7 +89,7 @@ class WANObjectCache implements LoggerAwareInterface {
        const HOLDOFF_TTL = 14; // MAX_COMMIT_DELAY + MAX_REPLICA_LAG + MAX_SNAPSHOT_LAG + 1
 
        /** Seconds to keep dependency purge keys around */
-       const CHECK_KEY_TTL = 31536000; // 1 year
+       const CHECK_KEY_TTL = self::TTL_YEAR;
        /** Seconds to keep lock keys around */
        const LOCK_TTL = 5;
        /** Default remaining TTL at which to consider pre-emptive regeneration */
@@ -296,7 +296,7 @@ class WANObjectCache implements LoggerAwareInterface {
         *     // Fetch the row from the DB
         *     $row = $dbr->selectRow( ... );
         *     $key = $cache->makeKey( 'building', $buildingId );
-        *     $cache->set( $key, $row, 86400, $setOpts );
+        *     $cache->set( $key, $row, $cache::TTL_DAY, $setOpts );
         * @endcode
         *
         * @param string $key Cache key
@@ -562,8 +562,8 @@ class WANObjectCache implements LoggerAwareInterface {
         *     $catInfo = $cache->getWithSetCallback(
         *         // Key to store the cached value under
         *         $cache->makeKey( 'cat-attributes', $catId ),
-        *         // Time-to-live (seconds)
-        *         60,
+        *         // Time-to-live (in seconds)
+        *         $cache::TTL_MINUTE,
         *         // Function that derives the new key value
         *         function ( $oldValue, &$ttl, array &$setOpts ) {
         *             $dbr = wfGetDB( DB_SLAVE );
@@ -580,8 +580,8 @@ class WANObjectCache implements LoggerAwareInterface {
         *     $catConfig = $cache->getWithSetCallback(
         *         // Key to store the cached value under
         *         $cache->makeKey( 'site-cat-config' ),
-        *         // Time-to-live (seconds)
-        *         86400,
+        *         // Time-to-live (in seconds)
+        *         $cache::TTL_DAY,
         *         // Function that derives the new key value
         *         function ( $oldValue, &$ttl, array &$setOpts ) {
         *             $dbr = wfGetDB( DB_SLAVE );
@@ -632,7 +632,7 @@ class WANObjectCache implements LoggerAwareInterface {
         *     $lastCatActions = $cache->getWithSetCallback(
         *         // Key to store the cached value under
         *         $cache->makeKey( 'cat-last-actions', 100 ),
-        *         // Time-to-live (seconds)
+        *         // Time-to-live (in seconds)
         *         10,
         *         // Function that derives the new key value
         *         function ( $oldValue, &$ttl, array &$setOpts ) {
index 3558149..30f9e2e 100644 (file)
@@ -508,9 +508,10 @@ abstract class TransformationalImageHandler extends ImageHandler {
         * @return string|bool Representing the IM version; false on error
         */
        protected function getMagickVersion() {
-               return ObjectCache::newAccelerator( CACHE_NONE )->getWithSetCallback(
-                       "imagemagick-version",
-                       3600,
+               $cache = ObjectCache::newAccelerator( CACHE_NONE );
+               return $cache->getWithSetCallback(
+                       'imagemagick-version',
+                       $cache::TTL_HOUR,
                        function () {
                                global $wgImageMagickConvertCommand;
 
@@ -523,7 +524,6 @@ abstract class TransformationalImageHandler extends ImageHandler {
                                );
                                if ( $x != 1 ) {
                                        wfDebug( __METHOD__ . ": ImageMagick version check failed\n" );
-
                                        return false;
                                }
 
index 8b4980a..3c6109b 100644 (file)
@@ -2914,8 +2914,9 @@ class WikiPage implements Page, IDBAccessObject {
                $status->value = $logid;
 
                // Show log excerpt on 404 pages rather than just a link
+               $cache = ObjectCache::getMainStashInstance();
                $key = wfMemcKey( 'page-recent-delete', md5( $logTitle->getPrefixedText() ) );
-               ObjectCache::getMainStashInstance()->set( $key, 1, 86400 );
+               $cache->set( $key, 1, $cache::TTL_DAY );
 
                return $status;
        }
index 78f7775..5ffca23 100644 (file)
@@ -134,7 +134,7 @@ class DateFormatter {
                if ( !$dateFormatter ) {
                        $dateFormatter = $cache->getWithSetCallback(
                                $cache->makeKey( 'dateformatter', $lang->getCode() ),
-                               3600,
+                               $cache::TTL_HOUR,
                                function () use ( $lang ) {
                                        return new DateFormatter( $lang );
                                }
index 8864b98..6c6ba3b 100644 (file)
@@ -636,7 +636,7 @@ class LoginForm extends SpecialPage {
                                $key = wfMemcKey( 'acctcreate', 'ip', $ip );
                                $value = $cache->get( $key );
                                if ( !$value ) {
-                                       $cache->set( $key, 0, 86400 );
+                                       $cache->set( $key, 0, $cache::TTL_DAY );
                                }
                                if ( $value >= $wgAccountCreationThrottle ) {
                                        return Status::newFatal( 'acct_creation_throttle_hit', $wgAccountCreationThrottle );
index f600e32..17fcab8 100644 (file)
@@ -1968,7 +1968,7 @@ abstract class UploadBase {
                if ( $value === false ) {
                        $cache->delete( $key );
                } else {
-                       $cache->set( $key, $value, 86400 );
+                       $cache->set( $key, $value, $cache::TTL_DAY );
                }
        }
 }
index bd14e41..8daf301 100644 (file)
--- a/thumb.php
+++ b/thumb.php
@@ -392,7 +392,7 @@ function wfGenerateThumbnail( File $file, array $params, $thumbName, $thumbPath
                if ( !$done ) { // transform() gave a fatal
                        global $wgMemc;
                        // Randomize TTL to reduce stampedes
-                       $wgMemc->incrWithInit( $key, 3600 + mt_rand( 0, 300 ) );
+                       $wgMemc->incrWithInit( $key, $wgMemc::TTL_HOUR + mt_rand( 0, 300 ) );
                }
        } );
 
@@ -445,7 +445,7 @@ function wfGenerateThumbnail( File $file, array $params, $thumbName, $thumbPath
 
        if ( !$thumb || $thumb->isError() ) {
                // Randomize TTL to reduce stampedes
-               $wgMemc->incrWithInit( $key, 3600 + mt_rand( 0, 300 ) );
+               $wgMemc->incrWithInit( $key, $wgMemc::TTL_HOUR + mt_rand( 0, 300 ) );
        }
 
        return array( $thumb, $errorHtml );