Reedy has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/313756

Change subject: Detect/use APCu properly
......................................................................

Detect/use APCu properly

In PHP 5.5 and above, userland APC caching moved to an extension

Bug: T140587
Change-Id: Ie0871776cd7e67838471a4fe95451cf4164079b7
(cherry picked from commit c214c9a95255abf1032d389d0fe8bcbde38b55db)
---
M autoload.php
M includes/DefaultSettings.php
M includes/installer/Installer.php
M includes/installer/i18n/en.json
M includes/installer/i18n/qqq.json
M includes/libs/MemoizedCallable.php
M includes/libs/objectcache/APCBagOStuff.php
A includes/libs/objectcache/APCUBagOStuff.php
M includes/objectcache/ObjectCache.php
M tests/phpunit/includes/libs/MemoizedCallableTest.php
10 files changed, 133 insertions(+), 21 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/56/313756/1

diff --git a/autoload.php b/autoload.php
index 679f5f3..f36e613 100644
--- a/autoload.php
+++ b/autoload.php
@@ -5,6 +5,7 @@
 
 $wgAutoloadLocalClasses = [
        'APCBagOStuff' => __DIR__ . 
'/includes/libs/objectcache/APCBagOStuff.php',
+       'APCUBagOStuff' => __DIR__ . 
'/includes/libs/objectcache/APCUBagOStuff.php',
        'AbstractContent' => __DIR__ . '/includes/content/AbstractContent.php',
        'Action' => __DIR__ . '/includes/actions/Action.php',
        'ActiveUsersPager' => __DIR__ . 
'/includes/specials/pagers/ActiveUsersPager.php',
diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php
index 613c219..5c6d204 100644
--- a/includes/DefaultSettings.php
+++ b/includes/DefaultSettings.php
@@ -2193,7 +2193,7 @@
  *   - CACHE_NONE:       Do not cache
  *   - CACHE_DB:         Store cache objects in the DB
  *   - CACHE_MEMCACHED:  MemCached, must specify servers in $wgMemCachedServers
- *   - CACHE_ACCEL:      APC, XCache or WinCache
+ *   - CACHE_ACCEL:      APC, APCU, XCache or WinCache
  *   - (other):          A string may be used which identifies a cache
  *                       configuration in $wgObjectCaches.
  *
@@ -2269,6 +2269,7 @@
        ],
 
        'apc' => [ 'class' => 'APCBagOStuff', 'reportDupes' => false ],
+       'apcu' => [ 'class' => 'APCUBagOStuff', 'reportDupes' => false ],
        'xcache' => [ 'class' => 'XCacheBagOStuff', 'reportDupes' => false ],
        'wincache' => [ 'class' => 'WinCacheBagOStuff', 'reportDupes' => false 
],
        'memcached-php' => [ 'class' => 'MemcachedPhpBagOStuff', 'loggroup' => 
'memcached' ],
diff --git a/includes/installer/Installer.php b/includes/installer/Installer.php
index 84a665d..aace867 100644
--- a/includes/installer/Installer.php
+++ b/includes/installer/Installer.php
@@ -242,6 +242,7 @@
        protected $objectCaches = [
                'xcache' => 'xcache_get',
                'apc' => 'apc_fetch',
+               'apcu' => 'apcu_fetch',
                'wincache' => 'wincache_ucache_get'
        ];
 
diff --git a/includes/installer/i18n/en.json b/includes/installer/i18n/en.json
index 2d9b991..f5440d4 100644
--- a/includes/installer/i18n/en.json
+++ b/includes/installer/i18n/en.json
@@ -57,6 +57,7 @@
        "config-memory-bad": "<strong>Warning:</strong> PHP's 
<code>memory_limit</code> is $1.\nThis is probably too low.\nThe installation 
may fail!",
        "config-xcache": "[http://xcache.lighttpd.net/ XCache] is installed",
        "config-apc": "[http://www.php.net/apc APC] is installed",
+       "config-apcu": "[http://www.php.net/apcu APCu] is installed",
        "config-wincache": "[http://www.iis.net/download/WinCacheForPhp 
WinCache] is installed",
        "config-no-cache-apcu": "<strong>Warning:</strong> Could not find 
[http://www.php.net/apcu APCu], [http://xcache.lighttpd.net/ XCache] or 
[http://www.iis.net/download/WinCacheForPhp WinCache].\nObject caching is not 
enabled.",
        "config-mod-security": "<strong>Warning:</strong> Your web server has 
[http://modsecurity.org/ mod_security]/mod_security2 enabled. Many common 
configurations of this will cause problems for MediaWiki and other software 
that allows users to post arbitrary content.\nIf possible, this should be 
disabled. Otherwise, refer to [http://modsecurity.org/documentation/ 
mod_security documentation] or contact your host's support if you encounter 
random errors.",
@@ -249,7 +250,7 @@
        "config-cache-options": "Settings for object caching:",
        "config-cache-help": "Object caching is used to improve the speed of 
MediaWiki by caching frequently used data.\nMedium to large sites are highly 
encouraged to enable this, and small sites will see benefits as well.",
        "config-cache-none": "No caching (no functionality is removed, but 
speed may be impacted on larger wiki sites)",
-       "config-cache-accel": "PHP object caching (APC, XCache or WinCache)",
+       "config-cache-accel": "PHP object caching (APC, APCu, XCache or 
WinCache)",
        "config-cache-memcached": "Use Memcached (requires additional setup and 
configuration)",
        "config-memcached-servers": "Memcached servers:",
        "config-memcached-help": "List of IP addresses to use for 
Memcached.\nShould specify one per line and specify the port to be used. For 
example:\n 127.0.0.1:11211\n 192.168.1.25:1234",
diff --git a/includes/installer/i18n/qqq.json b/includes/installer/i18n/qqq.json
index 307cf4d..6579eb4 100644
--- a/includes/installer/i18n/qqq.json
+++ b/includes/installer/i18n/qqq.json
@@ -75,6 +75,7 @@
        "config-memory-bad": "Parameters:\n* $1 is the configured 
<code>memory_limit</code>.",
        "config-xcache": "Message indicates if this program is available",
        "config-apc": "Message indicates if this program is available",
+       "config-apcu": "Message indicates if this program is available",
        "config-wincache": "Message indicates if this program is available",
        "config-no-cache-apcu": "Status message in the MediaWiki installer 
environment checks.",
        "config-mod-security": "Status message in the MediaWiki installer 
environment checks.",
diff --git a/includes/libs/MemoizedCallable.php 
b/includes/libs/MemoizedCallable.php
index 50e9732..12a5cad 100644
--- a/includes/libs/MemoizedCallable.php
+++ b/includes/libs/MemoizedCallable.php
@@ -1,6 +1,6 @@
 <?php
 /**
- * APC-backed function memoization
+ * APC-backed and APCu-backed function memoization
  *
  * This class provides memoization for pure functions. A function is pure
  * if its result value depends on nothing other than its input parameters
@@ -8,7 +8,7 @@
  *
  * The first invocation of the memoized callable with a particular set of
  * arguments will be delegated to the underlying callable. Repeat invocations
- * with the same input parameters will be served from APC.
+ * with the same input parameters will be served from APC or APCu.
  *
  * @par Example:
  * @code
@@ -70,7 +70,7 @@
        }
 
        /**
-        * Fetch the result of a previous invocation from APC.
+        * Fetch the result of a previous invocation from APC or APCu.
         *
         * @param string $key
         * @param bool &$success
@@ -79,12 +79,14 @@
                $success = false;
                if ( function_exists( 'apc_fetch' ) ) {
                        return apc_fetch( $key, $success );
+               } elseif ( function_exists( 'apcu_fetch' ) ) {
+                       return apcu_fetch( $key, $success );
                }
                return false;
        }
 
        /**
-        * Store the result of an invocation in APC.
+        * Store the result of an invocation in APC or APCu.
         *
         * @param string $key
         * @param mixed $result
@@ -92,6 +94,8 @@
        protected function storeResult( $key, $result ) {
                if ( function_exists( 'apc_store' ) ) {
                        apc_store( $key, $result, $this->ttl );
+               } elseif ( function_exists( 'apcu_store' ) ) {
+                       apcu_store( $key, $result, $this->ttl );
                }
        }
 
diff --git a/includes/libs/objectcache/APCBagOStuff.php 
b/includes/libs/objectcache/APCBagOStuff.php
index 8f70fc7..9bfcee7 100644
--- a/includes/libs/objectcache/APCBagOStuff.php
+++ b/includes/libs/objectcache/APCBagOStuff.php
@@ -75,25 +75,35 @@
        }
 
        protected function doGet( $key, $flags = 0 ) {
-               $val = apc_fetch( $key . self::KEY_SUFFIX );
+               return $this->getUnserialize(
+                       apc_fetch( $key . self::KEY_SUFFIX )
+               );
+       }
 
-               if ( is_string( $val ) && !$this->nativeSerialize ) {
-                       $val = $this->isInteger( $val )
-                               ? intval( $val )
-                               : unserialize( $val );
+       protected function getUnserialize( $value ) {
+               if ( is_string( $value ) && !$this->nativeSerialize ) {
+                       $value = $this->isInteger( $value )
+                               ? intval( $value )
+                               : unserialize( $value );
                }
-
-               return $val;
+               return $value;
        }
 
        public function set( $key, $value, $exptime = 0, $flags = 0 ) {
+               apc_store(
+                       $key . self::KEY_SUFFIX,
+                       $this->setSerialize( $value ),
+                       $exptime
+               );
+
+               return true;
+       }
+
+       protected function setSerialize( $value ) {
                if ( !$this->nativeSerialize && !$this->isInteger( $value ) ) {
                        $value = serialize( $value );
                }
-
-               apc_store( $key . self::KEY_SUFFIX, $value, $exptime );
-
-               return true;
+               return $value;
        }
 
        public function delete( $key ) {
diff --git a/includes/libs/objectcache/APCUBagOStuff.php 
b/includes/libs/objectcache/APCUBagOStuff.php
new file mode 100644
index 0000000..02b3c92
--- /dev/null
+++ b/includes/libs/objectcache/APCUBagOStuff.php
@@ -0,0 +1,91 @@
+<?php
+/**
+ * Object caching using PHP's APCU accelerator.
+ *
+ * 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
+ */
+
+/**
+ * This is a wrapper for APCU's shared memory functions
+ *
+ * @ingroup Cache
+ */
+class APCUBagOStuff extends APCBagOStuff {
+       /**
+        * Constructor
+        *
+        * Available parameters are:
+        *   - nativeSerialize:     If true, pass objects to apcu_store(), and 
trust it
+        *                          to serialize them correctly. If false, 
serialize
+        *                          all values in PHP.
+        *
+        * @param array $params
+        */
+       public function __construct( array $params = [] ) {
+               parent::__construct( $params );
+       }
+
+       protected function doGet( $key, $flags = 0 ) {
+               return $this->getUnserialize(
+                       apcu_fetch( $key . self::KEY_SUFFIX )
+               );
+       }
+
+       public function set( $key, $value, $exptime = 0, $flags = 0 ) {
+               apcu_store(
+                       $key . self::KEY_SUFFIX,
+                       $this->setSerialize( $value ),
+                       $exptime
+               );
+
+               return true;
+       }
+
+       public function delete( $key ) {
+               apcu_delete( $key . self::KEY_SUFFIX );
+
+               return true;
+       }
+
+       public function incr( $key, $value = 1 ) {
+               /**
+                * @todo When we only support php 7 or higher remove this hack
+                *
+                * https://github.com/krakjoe/apcu/issues/166
+                */
+               if ( apcu_exists( $key . self::KEY_SUFFIX ) ) {
+                       return apcu_inc( $key . self::KEY_SUFFIX, $value );
+               } else {
+                       return apcu_set( $key . self::KEY_SUFFIX, $value );
+               }
+       }
+
+       public function decr( $key, $value = 1 ) {
+               /**
+                * @todo When we only support php 7 or higher remove this hack
+                *
+                * https://github.com/krakjoe/apcu/issues/166
+                */
+               if ( apcu_exists( $key . self::KEY_SUFFIX ) ) {
+                       return apcu_dec( $key . self::KEY_SUFFIX, $value );
+               } else {
+                       return apcu_set( $key . self::KEY_SUFFIX, -$value );
+               }
+       }
+}
diff --git a/includes/objectcache/ObjectCache.php 
b/includes/objectcache/ObjectCache.php
index 24846e6..29df5af6 100644
--- a/includes/objectcache/ObjectCache.php
+++ b/includes/objectcache/ObjectCache.php
@@ -50,7 +50,7 @@
  *
  * - ObjectCache::getLocalServerInstance( $fallbackType )
  *   Purpose: Memory cache for very hot keys.
- *   Stored only on the individual web server (typically APC for web requests,
+ *   Stored only on the individual web server (typically APC or APCu for web 
requests,
  *   and EmptyBagOStuff in CLI mode).
  *   Not replicated to the other servers.
  *
@@ -232,7 +232,7 @@
        /**
         * Factory function for CACHE_ACCEL (referenced from 
DefaultSettings.php)
         *
-        * This will look for any APC style server-local cache.
+        * This will look for any APC or APCu style server-local cache.
         * A fallback cache can be specified if none is found.
         *
         *     // Direct calls
@@ -249,6 +249,8 @@
        public static function getLocalServerInstance( $fallback = CACHE_NONE ) 
{
                if ( function_exists( 'apc_fetch' ) ) {
                        $id = 'apc';
+               } elseif ( function_exists( 'apcu_fetch' ) ) {
+                       $id = 'apcu';
                } elseif ( function_exists( 'xcache_get' ) && wfIniGetBool( 
'xcache.var_size' ) ) {
                        $id = 'xcache';
                } elseif ( function_exists( 'wincache_ucache_get' ) ) {
diff --git a/tests/phpunit/includes/libs/MemoizedCallableTest.php 
b/tests/phpunit/includes/libs/MemoizedCallableTest.php
index 6eb96b1..881f5e1 100644
--- a/tests/phpunit/includes/libs/MemoizedCallableTest.php
+++ b/tests/phpunit/includes/libs/MemoizedCallableTest.php
@@ -1,7 +1,7 @@
 <?php
 /**
  * A MemoizedCallable subclass that stores function return values
- * in an instance property rather than APC.
+ * in an instance property rather than APC or APCu.
  */
 class ArrayBackedMemoizedCallable extends MemoizedCallable {
        private $cache = [];
@@ -44,7 +44,7 @@
         * Consecutive calls to the memoized callable with the same arguments
         * should result in just one invocation of the underlying callable.
         *
-        * @requires function apc_store
+        * @requires function apc_store/apcu_store
         */
        public function testCallableMemoized() {
                $observer = $this->getMock( 'stdClass', [ 'computeSomething' ] 
);

-- 
To view, visit https://gerrit.wikimedia.org/r/313756
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie0871776cd7e67838471a4fe95451cf4164079b7
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: REL1_27
Gerrit-Owner: Reedy <re...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to