[wp-trac] [WordPress Trac] #5550: ENABLE_CACHE and DISABLE_CACHE constants

WordPress Trac wp-trac at lists.automattic.com
Sun Dec 30 11:17:35 GMT 2007


#5550: ENABLE_CACHE and DISABLE_CACHE constants
--------------------------+-------------------------------------------------
 Reporter:  Mrasnika      |       Owner:  anonymous
     Type:  defect        |      Status:  new      
 Priority:  normal        |   Milestone:           
Component:  Optimization  |     Version:  2.3.2    
 Severity:  major         |    Keywords:  cache    
--------------------------+-------------------------------------------------
 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);
 }
 }}}

-- 
Ticket URL: <http://trac.wordpress.org/ticket/5550>
WordPress Trac <http://trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list