Make WordPress Core

Opened 14 years ago

Closed 14 years ago

Last modified 14 years ago

#5550 closed defect (bug) (fixed)

ENABLE_CACHE and DISABLE_CACHE constants

Reported by: mrasnika's profile Mrasnika Owned by:
Milestone: 2.5 Priority: normal
Severity: major Version: 2.3.2
Component: Optimization Keywords: cache
Focuses: Cc:

Description

Hi,

I work on several wordpress installations that have a lot of categories and tags. "A lot" means several thousand. Usually this means that saving a category or a tag after an update results in crashing the site because of depleting the memory assigned to PHP (in my case 32MB). This is because of the $data = unserialize(serialize($data)); thing - since the structure that is serialized/unserialzed is too huge, this eats up the available memory. I know this is done in order to remove any circular references towards the categories, so it has a purpose of some sort. Anyway, for me, it crashes the sites I am working on.

Now, what I do is

define ('DISABLE_CACHE', '1');

in the config file. I expect that this will stop doing any caching. However, it is not.

These two constants (ENABLE_CACHE and DISABLE_CACHE) are used in the constructor
of the WP_Object_Cache object to either build the object or not. However, when the wp_cache_add(), wp_cache_replace() and wp_cache_set() functions are called, they are not aware that the caching is disabled, and they do the $data = unserialize(serialize($data)); anyway. So no matter if caching is enabled or not, calling those functions crashes the site.

I patched this for my wordpress installations (using it since 2.2.2 till my current upgrade to 2.3.2) and I decided to propose it to put it in the next release. It is in fact a really simple patch - in the wp_cache_add(), wp_cache_replace() and wp_cache_set() functions you need to add a check for whether the caching is disables, like this:

function wp_cache_add($key, $data, $flag = '', $expire = 0) {

	if (defined('DISABLE_CACHE'))
		return;

	if ( ! defined('ENABLE_CACHE') )
		return;

	global $wp_object_cache;
	$data = unserialize(serialize($data));

	return $wp_object_cache->add($key, $data, $flag, $expire);
}
...
function wp_cache_replace($key, $data, $flag = '', $expire = 0) {

	if (defined('DISABLE_CACHE'))
		return;

	if ( ! defined('ENABLE_CACHE') )
		return;

	global $wp_object_cache;
	$data = unserialize(serialize($data));

	return $wp_object_cache->replace($key, $data, $flag, $expire);
}

function wp_cache_set($key, $data, $flag = '', $expire = 0) {

	if (defined('DISABLE_CACHE'))
		return;

	if ( ! defined('ENABLE_CACHE') )
		return;

	global $wp_object_cache;
	$data = unserialize(serialize($data));

	return $wp_object_cache->set($key, $data, $flag, $expire);
}

Attachments (1)

cache.php (10.5 KB) - added by Mrasnika 14 years ago.
This is the patched version of the wp-includes/cache.php file

Download all attachments as: .zip

Change History (9)

@Mrasnika
14 years ago

This is the patched version of the wp-includes/cache.php file

#1 follow-up: @DD32
14 years ago

Related: #5147 - Object cache should check if ENABLE_CACHE is TRUE (not false)

define('ENABLE_CACHE',false)

results in the Object cache being enabled.

#2 follow-up: @ryan
14 years ago

If you disable the in-memory caching, page loads can generate hundreds of queries. This is not something that would go into production. A better option might be to not do any serializing when the persistent cache is not being used.

#3 in reply to: ↑ 1 @Mrasnika
14 years ago

Replying to DD32:

Related: #5147 - Object cache should check if ENABLE_CACHE is TRUE (not false)

define('ENABLE_CACHE',false)

results in the Object cache being enabled.

Thank you. Anyway, the snippet I inserted in those methods is copied from the constructor of the WP_Object_Cache, and to me it seems that they are ambiguous.

#4 in reply to: ↑ 2 ; follow-up: @Mrasnika
14 years ago

Replying to ryan:

A better option might be to not do any serializing when the persistent cache is not being used.

That's exactly what I am trying to say - when the WP_Object_Cache is off, there should not be any serialization done.

And instead of DISABLE_CACHE=true, I must do ENABLE_CACHE=false, correct ?

#5 @lloydbudd
14 years ago

  • Milestone set to 2.5

#6 in reply to: ↑ 4 @ryan
14 years ago

Replying to Mrasnika:

Replying to ryan:

A better option might be to not do any serializing when the persistent cache is not being used.

That's exactly what I am trying to say - when the WP_Object_Cache is off, there should not be any serialization done.

And instead of DISABLE_CACHE=true, I must do ENABLE_CACHE=false, correct ?

But this patch avoids serialization by completely avoiding the in-memory cache. This will result in lots of DB queries.

DISABLE_CACHE is a misnomer. That disables the persistent cache only, not the entire cache. We'll probably be removing the default persistent caching, leaving that to plugins. The persistent cache is not enabled by default anyway. With persistence removed, I think we can lose the unserialize(serialize()) calls.

#7 @ryan
14 years ago

  • Resolution set to fixed
  • Status changed from new to closed

#5570 removes the persistent object cache and the serialization. That should fix this ticket.

#8 @darkdragon
14 years ago

  • Milestone changed from 2.6 to 2.5
Note: See TracTickets for help on using tickets.