From 6d75218f6a838d7d2917f25ec1320c2a6fd04800 Mon Sep 17 00:00:00 2001 From: Jon Cave Date: Thu, 15 Sep 2011 20:23:00 +0000 Subject: [PATCH] Introduce wp_suspend_cache_addition() to allow reduced memory usage when cache additions aren't useful. Fixes #5389. git-svn-id: https://develop.svn.wordpress.org/trunk@18681 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-includes/cache.php | 3 +++ wp-includes/functions.php | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/wp-includes/cache.php b/wp-includes/cache.php index 70753aa2d4..02b28f70ce 100644 --- a/wp-includes/cache.php +++ b/wp-includes/cache.php @@ -294,6 +294,9 @@ class WP_Object_Cache { * @return bool False if cache key and group already exist, true on success */ function add( $key, $data, $group = 'default', $expire = '' ) { + if ( wp_suspend_cache_addition() ) + return false; + if ( empty ($group) ) $group = 'default'; diff --git a/wp-includes/functions.php b/wp-includes/functions.php index e453c39431..ab208f9a96 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -3709,6 +3709,30 @@ function wp_guess_url() { return rtrim($url, '/'); } +/** + * Temporarily suspend cache additions. + * + * Stops more data being added to the cache, but still allows cache retrieval. + * This is useful for actions, such as imports, when a lot of data would otherwise + * be almost uselessly added to the cache. + * + * Suspension lasts for a single page load at most. Remember to call this + * function again if you wish to re-enable cache adds earlier. + * + * @since 3.3.0 + * + * @param bool $suspend Optional. Suspends additions if true, re-enables them if false. + * @return bool The current suspend setting + */ +function wp_suspend_cache_addition( $suspend = null ) { + static $_suspend = false; + + if ( is_bool( $suspend ) ) + $_suspend = $suspend; + + return $_suspend; +} + /** * Suspend cache invalidation. *