Ticket #21434: 21434.5.diff

File 21434.5.diff, 9.6 KB (added by ryan, 10 months ago)

Turn global_groups into an associate array for faster isset checks

Line 
1Index: wp-includes/load.php
2===================================================================
3--- wp-includes/load.php        (revision 21393)
4+++ wp-includes/load.php        (working copy)
5@@ -381,7 +381,7 @@
6  * @since 3.0.0
7  */
8 function wp_start_object_cache() {
9-       global $_wp_using_ext_object_cache;
10+       global $_wp_using_ext_object_cache, $blog_id;
11 
12        $first_init = false;
13        if ( ! function_exists( 'wp_cache_init' ) ) {
14@@ -403,8 +403,8 @@
15        // If cache supports reset, reset instead of init if already initialized.
16        // Reset signals to the cache that global IDs have changed and it may need to update keys
17        // and cleanup caches.
18-       if ( !$first_init && function_exists('wp_cache_reset') )
19-               wp_cache_reset();
20+       if ( ! $first_init && function_exists( 'wp_cache_switch_to_blog' ) )
21+               wp_cache_switch_to_blog( $blog_id );
22        else
23                wp_cache_init();
24 
25Index: wp-includes/ms-blogs.php
26===================================================================
27--- wp-includes/ms-blogs.php    (revision 21393)
28+++ wp-includes/ms-blogs.php    (working copy)
29@@ -487,18 +487,22 @@
30                        $current_user->for_blog( $blog_id );
31        }
32 
33-       if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
34-               $global_groups = $wp_object_cache->global_groups;
35-       else
36-               $global_groups = false;
37-
38-       wp_cache_init();
39-       if ( function_exists('wp_cache_add_global_groups') ) {
40-               if ( is_array( $global_groups ) )
41-                       wp_cache_add_global_groups( $global_groups );
42+       if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
43+               wp_cache_switch_to_blog( $blog_id );
44+       } else {
45+               if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
46+                       $global_groups = $wp_object_cache->global_groups;
47                else
48-                       wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) );
49-               wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
50+                       $global_groups = false;
51+       
52+               wp_cache_init();
53+               if ( function_exists('wp_cache_add_global_groups') ) {
54+                       if ( is_array( $global_groups ) )
55+                               wp_cache_add_global_groups( $global_groups );
56+                       else
57+                               wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) );
58+                       wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
59+               }
60        }
61 
62        do_action('switch_blog', $blog_id, $prev_blog_id);
63@@ -551,18 +555,22 @@
64                        $current_user->for_blog( $blog_id );
65        }
66 
67-       if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
68-               $global_groups = $wp_object_cache->global_groups;
69-       else
70-               $global_groups = false;
71-
72-       wp_cache_init();
73-       if ( function_exists('wp_cache_add_global_groups') ) {
74-               if ( is_array( $global_groups ) )
75-                       wp_cache_add_global_groups( $global_groups );
76+       if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
77+               wp_cache_switch_to_blog( $blog_id );
78+       } else {
79+               if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
80+                       $global_groups = $wp_object_cache->global_groups;
81                else
82-                       wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) );
83-               wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
84+                       $global_groups = false;
85+       
86+               wp_cache_init();
87+               if ( function_exists('wp_cache_add_global_groups') ) {
88+                       if ( is_array( $global_groups ) )
89+                               wp_cache_add_global_groups( $global_groups );
90+                       else
91+                               wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) );
92+                       wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
93+               }
94        }
95 
96        do_action('switch_blog', $blog_id, $prev_blog_id);
97Index: wp-includes/cache.php
98===================================================================
99--- wp-includes/cache.php       (revision 21393)
100+++ wp-includes/cache.php       (working copy)
101@@ -180,6 +180,19 @@
102 }
103 
104 /**
105+ * Switch the interal blog id.
106+ *
107+ * This changes the blog id used to create keys in blog specific groups.
108+ *
109+ * @param int $blog_id Blog ID
110+ */
111+function wp_cache_switch_to_blog( $blog_id ) {
112+       global $wp_object_cache;
113+
114+       return $wp_object_cache->switch_to_blog( $blog_id );
115+}
116+
117+/**
118  * Adds a group or set of groups to the list of global groups.
119  *
120  * @since 2.6.0
121@@ -189,7 +202,7 @@
122 function wp_cache_add_global_groups( $groups ) {
123        global $wp_object_cache;
124 
125-       return $wp_object_cache->add_global_groups($groups);
126+       return $wp_object_cache->add_global_groups( $groups );
127 }
128 
129 /**
130@@ -209,8 +222,11 @@
131  * this function instructs the backend to reset those keys and perform any cleanup since blog or site IDs have changed since cache init.
132  *
133  * @since 2.6.0
134+ * @deprecated 3.5.0
135  */
136 function wp_cache_reset() {
137+       _deprecated_function( __FUNCTION__, '3.5', 'wp_cache_switch_to_blog()' );
138+
139        global $wp_object_cache;
140 
141        return $wp_object_cache->reset();
142@@ -271,6 +287,15 @@
143        var $global_groups = array();
144 
145        /**
146+        * The blog prefix to prepend to keys in non-global groups.
147+        *
148+        * @var int
149+        * @access private
150+        * @since 3.5.0
151+        */
152+       var $blog_prefix;
153+
154+       /**
155         * Adds data to the cache if it doesn't already exist.
156         *
157         * @uses WP_Object_Cache::_exists Checks to see if the cache already has data.
158@@ -292,7 +317,11 @@
159                if ( empty( $group ) )
160                        $group = 'default';
161 
162-               if ( $this->_exists($key, $group) )
163+               $id = $key;
164+               if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
165+                       $id = $this->blog_prefix . $key;
166+
167+               if ( $this->_exists( $id, $group ) )
168                        return false;
169 
170                return $this->set($key, $data, $group, $expire);
171@@ -308,8 +337,8 @@
172        function add_global_groups( $groups ) {
173                $groups = (array) $groups;
174 
175-               $this->global_groups = array_merge($this->global_groups, $groups);
176-               $this->global_groups = array_unique($this->global_groups);
177+               $groups = array_fill_keys( $groups, true );
178+               $this->global_groups = array_merge( $this->global_groups, $groups );
179        }
180 
181        /**
182@@ -325,7 +354,10 @@
183        function decr( $key, $offset = 1, $group = 'default' ) {
184                if ( empty( $group ) )
185                        $group = 'default';
186-               
187+
188+               if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
189+                       $key = $this->blog_prefix . $key;
190+
191                if ( ! $this->_exists( $key, $group ) )
192                        return false;
193 
194@@ -361,6 +393,9 @@
195                if ( empty( $group ) )
196                        $group = 'default';
197 
198+               if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
199+                       $key = $this->blog_prefix . $key;
200+
201                if ( ! $force && ! $this->_exists( $key, $group ) )
202                        return false;
203 
204@@ -402,6 +437,9 @@
205                if ( empty( $group ) )
206                        $group = 'default';
207 
208+               if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
209+                       $key = $this->blog_prefix . $key;
210+
211                if ( $this->_exists( $key, $group ) ) {
212                        $found = true;
213                        $this->cache_hits += 1;
214@@ -430,6 +468,9 @@
215                if ( empty( $group ) )
216                        $group = 'default';
217 
218+               if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
219+                       $key = $this->blog_prefix . $key;
220+
221                if ( ! $this->_exists( $key, $group ) )
222                        return false;
223 
224@@ -458,26 +499,33 @@
225         * @param int $expire When to expire the cache contents
226         * @return bool False if not exists, true if contents were replaced
227         */
228-       function replace($key, $data, $group = 'default', $expire = '') {
229+       function replace( $key, $data, $group = 'default', $expire = '' ) {
230                if ( empty( $group ) )
231                        $group = 'default';
232 
233-               if ( ! $this->_exists( $key, $group ) )
234+               $id = $key;
235+               if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
236+                       $id = $this->blog_prefix . $key;
237+
238+               if ( ! $this->_exists( $id, $group ) )
239                        return false;
240 
241-               return $this->set($key, $data, $group, $expire);
242+               return $this->set( $key, $data, $group, $expire );
243        }
244 
245        /**
246         * Reset keys
247         *
248         * @since 3.0.0
249+        * @deprecated 3.5.0
250         */
251        function reset() {
252+               _deprecated_function( __FUNCTION__, '3.5', 'switch_to_blog()' );
253+
254                // Clear out non-global caches since the blog ID has changed.
255-               foreach ( array_keys($this->cache) as $group ) {
256-                       if ( !in_array($group, $this->global_groups) )
257-                               unset($this->cache[$group]);
258+               foreach ( array_keys( $this->cache ) as $group ) {
259+                       if ( ! isset( $this->global_groups[ $group ] ) )
260+                               unset( $this->cache[ $group ] );
261                }
262        }
263 
264@@ -505,7 +553,10 @@
265                if ( empty( $group ) )
266                        $group = 'default';
267 
268-               if ( is_object($data) )
269+               if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
270+                       $key = $this->blog_prefix . $key;
271+
272+               if ( is_object( $data ) )
273                        $data = clone $data;
274 
275                $this->cache[$group][$key] = $data;
276@@ -533,6 +584,18 @@
277        }
278 
279        /**
280+        * Switch the interal blog id.
281+        *
282+        * This changes the blog id used to create keys in blog specific groups.
283+        *
284+        * @param int $blog_id Blog ID
285+        */
286+       function switch_to_blog( $blog_id ) {
287+               $blog_id = (int) $blog_id;
288+               $this->blog_prefix = $this->multisite ? $blog_id . ':' : '';
289+       }
290+
291+       /**
292         * Utility function to determine whether a key exists in the cache.
293         *
294         * @since 3.4.0
295@@ -550,11 +613,17 @@
296         * @return null|WP_Object_Cache If cache is disabled, returns null.
297         */
298        function __construct() {
299+               global $blog_id;
300+
301+               $this->multisite = is_multisite();
302+               $this->blog_prefix =  $this->multisite ? $blog_id . ':' : '';
303+               
304+
305                /**
306                 * @todo This should be moved to the PHP4 style constructor, PHP5
307                 * already calls __destruct()
308                 */
309-               register_shutdown_function(array(&$this, "__destruct"));
310+               register_shutdown_function( array( &$this, "__destruct" ) );
311        }
312 
313        /**