Merge "Fix edit link for messages in $wgForceUIMsgAsContentMsg"
[lhc/web/wiklou.git] / includes / objectcache / ObjectCache.php
index 2e47e24..7faf4bb 100644 (file)
@@ -26,10 +26,20 @@ use MediaWiki\Logger\LoggerFactory;
 /**
  * Functions to get cache objects
  *
+ * The word "cache" has two main dictionary meanings, and both
+ * are used in this factory class. They are:
+ *   - a) A place to store copies or computations on existing data
+ *     for higher access speeds (the computer science definition)
+ *   - b) A place to store lightweight data that is not canonically
+ *     stored anywhere else (e.g. a "hoard" of objects)
+ *
  * @ingroup Cache
  */
 class ObjectCache {
+       /** @var Array Map of (id => BagOStuff) */
        public static $instances = array();
+       /** @var Array Map of (id => WANObjectCache) */
+       public static $wanInstances = array();
 
        /**
         * Get a cached instance of the specified type of cache object.
@@ -48,11 +58,30 @@ class ObjectCache {
                return $object;
        }
 
+       /**
+        * Get a cached instance of the specified type of cache object.
+        *
+        * @param string $id
+        *
+        * @return WANObjectCache
+        * @since 1.26
+        */
+       static function getWANInstance( $id ) {
+               if ( isset( self::$wanInstances[$id] ) ) {
+                       return self::$wanInstances[$id];
+               }
+
+               $object = self::newWANCacheFromId( $id );
+               self::$wanInstances[$id] = $object;
+               return $object;
+       }
+
        /**
         * Clear all the cached instances.
         */
        static function clear() {
                self::$instances = array();
+               self::$wanInstances = array();
        }
 
        /**
@@ -167,4 +196,64 @@ class ObjectCache {
        static function newMemcached( $params ) {
                return new MemcachedPhpBagOStuff( $params );
        }
+
+       /**
+        * Create a new cache object of the specified type
+        *
+        * @param string $id
+        *
+        * @throws MWException
+        * @return WANObjectCache
+        * @since 1.26
+        */
+       static function newWANCacheFromId( $id ) {
+               global $wgWANObjectCaches;
+
+               if ( !isset( $wgWANObjectCaches[$id] ) ) {
+                       throw new MWException( "Invalid object cache type \"$id\" requested. " .
+                               "It is not present in \$wgWANObjectCaches." );
+               }
+
+               $params = $wgWANObjectCaches[$id];
+               $class = $params['relayerConfig']['class'];
+               $params['relayer'] = new $class( $params['relayerConfig'] );
+               $params['cache'] = self::newFromId( $params['cacheId'] );
+               $class = $params['class'];
+
+               return new $class( $params );
+       }
+
+       /**
+        * Get the main WAN cache object
+        *
+        * @return WANObjectCache
+        * @since 1.26
+        */
+       static function getMainWANInstance() {
+               global $wgMainWANCache;
+
+               return self::getWANInstance( $wgMainWANCache );
+       }
+
+       /**
+        * Stash objects are BagOStuff instances suitable for storing light
+        * weight data that is not canonically stored elsewhere (such as RDBMS).
+        * Stashes should be configured to propagate changes to all data-centers.
+        *
+        * Callers should be prepared for:
+        *   - a) Writes to be slower in non-"primary" (e.g. HTTP GET/HEAD only) DCs
+        *   - b) Reads to be eventually consistent, e.g. for get()/getMulti()
+        * In general, this means avoiding updates on idempotent HTTP requests and
+        * avoiding an assumption of perfect serializability (or accepting anomalies).
+        * Reads may be eventually consistent or data might rollback as nodes flap.
+        *
+        *
+        * @return BagOStuff
+        * @since 1.26
+        */
+       static function getMainStashInstance() {
+               global $wgMainStash;
+
+               return self::getInstance( $wgMainStash );
+       }
 }