Advertisement
Guest User

wp-cache optimizations

a guest
May 20th, 2015
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.53 KB | None | 0 0
  1. --- commonFiles/wordpress_from_svn/wp-includes/cache.php        (revision 32479)
  2. +++ commonFiles/wordpress_from_svn/wp-includes/cache.php        (working copy)
  3. @@ -24,6 +24,18 @@
  4.  function wp_cache_add( $key, $data, $group = '', $expire = 0 ) {
  5.         global $wp_object_cache;
  6.  
  7. +       error_log('@@@@@@@@@@@@@ wp_cache_add ############' . $key. ' ' . $group);
  8. +       if ($key == 'alloptions') {
  9. +                if (!is_array($data))
  10. +                        error_log('@@@@@@@@@@@@@ wp_cache_add got non array as data ###########');
  11. +                $i = 0;
  12. +                foreach($data as $k => $v) { //isn't serialized yet
  13. +                        if ($wp_object_cache->add( $k, $v, $group, $expire )) //if does not return false
  14. +                                $i++;
  15. +                }
  16. +                error_log("@@@@@@@@@@@@@ Added $i keys from alloptions ############");
  17. +        }
  18. +
  19.         return $wp_object_cache->add( $key, $data, $group, (int) $expire );
  20.  }
  21.  
  22. @@ -139,6 +151,8 @@
  23.   */
  24.  function wp_cache_init() {
  25.         $GLOBALS['wp_object_cache'] = new WP_Object_Cache();
  26. +
  27. +       wp_load_alloptions(); //load all the options into the cache
  28.  }
  29.  
  30.  /**
  31.  
  32.  
  33. --- commonFiles/wordpress_from_svn/wp-includes/default-widgets.php (revision 32479)
  34. +++ commonFiles/wordpress_from_svn/wp-includes/default-widgets.php (working copy)
  35. @@ -744,9 +744,10 @@
  36.                 $instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
  37.                 $this->flush_widget_cache();
  38.  
  39. -               $alloptions = wp_cache_get( 'alloptions', 'options' );
  40. -               if ( isset($alloptions['widget_recent_entries']) )
  41. -                       delete_option('widget_recent_entries');
  42. +               //$alloptions = wp_cache_get( 'alloptions', 'options' );
  43. +               //if ( isset($alloptions['widget_recent_entries']) )
  44. +               if (wp_cache_get('widget_recent_entries', 'options')) //save db time just check cache
  45. +                       delete_option('widget_recent_entries');       //delete from DB if it existed
  46.  
  47.                 return $instance;
  48.         }
  49. @@ -897,8 +898,9 @@
  50.                 $instance['number'] = absint( $new_instance['number'] );
  51.                 $this->flush_widget_cache();
  52.  
  53. -               $alloptions = wp_cache_get( 'alloptions', 'options' );
  54. -               if ( isset($alloptions['widget_recent_comments']) )
  55. +               //$alloptions = wp_cache_get( 'alloptions', 'options' );
  56. +               //if ( isset($alloptions['widget_recent_comments']) )
  57. +               if (wp_cache_get('widget_recent_comments', 'options'))
  58.                         delete_option('widget_recent_comments');
  59.  
  60.                 return $instance;
  61.  
  62.  
  63. --- commonFiles/wordpress_from_svn/wp-includes/formatting.php   (revision 32479)
  64. +++ commonFiles/wordpress_from_svn/wp-includes/formatting.php   (working copy)
  65. @@ -602,8 +602,9 @@
  66.         if ( ! $charset ) {
  67.                 static $_charset;
  68.                 if ( ! isset( $_charset ) ) {
  69. -                       $alloptions = wp_load_alloptions();
  70. -                       $_charset = isset( $alloptions['blog_charset'] ) ? $alloptions['blog_charset'] : '';
  71. +                       //$alloptions = wp_load_alloptions();
  72. +                       //$_charset = isset( $alloptions['blog_charset'] ) ? $alloptions['blog_charset'] : '';
  73. +                       $_charset = get_option('blog_charset', ''); //this will try the cache first
  74.                 }
  75.                 $charset = $_charset;
  76.         }
  77.  
  78.  
  79. --- commonFiles/wordpress_from_svn/wp-includes/functions.php    (revision 32479)
  80. +++ commonFiles/wordpress_from_svn/wp-includes/functions.php    (working copy)
  81. @@ -1268,6 +1288,7 @@
  82.                 return true;
  83.  
  84.         $suppress = $wpdb->suppress_errors();
  85. +/*
  86.         if ( ! defined( 'WP_INSTALLING' ) ) {
  87.                 $alloptions = wp_load_alloptions();
  88.         }
  89. @@ -1276,6 +1297,9 @@
  90.                 $installed = $wpdb->get_var( "SELECT option_value FROM $wpdb->options WHERE option_name = 'siteurl'" );
  91.         else
  92.                 $installed = $alloptions['siteurl'];
  93. +*/
  94. +       $installed = get_option('siteurl'); //this looks equivalent
  95. +
  96.         $wpdb->suppress_errors( $suppress );
  97.  
  98.         $installed = !empty( $installed );
  99.  
  100.  
  101.  
  102. --- commonFiles/wordpress_from_svn/wp-includes/option.php       (revision 32479)
  103. +++ commonFiles/wordpress_from_svn/wp-includes/option.php       (working copy)
  104. @@ -49,8 +49,11 @@
  105.         if ( defined( 'WP_SETUP_CONFIG' ) )
  106.                 return false;
  107.  
  108. -       if ( ! defined( 'WP_INSTALLING' ) ) {
  109. -               // prevent non-existent options from triggering multiple queries
  110. +        if ( ! defined( 'WP_INSTALLING' ) ) {              
  111. +                
  112. +                $value = wp_cache_get( $option, 'options' );
  113. +                if ( false === $value ) { //if we didn't get anything back from the cache
  114. +                    //check the notoptions
  115.                 $notoptions = wp_cache_get( 'notoptions', 'options' );
  116.                 if ( isset( $notoptions[ $option ] ) ) {
  117.                         /**
  118. @@ -66,14 +69,7 @@
  119.                         return apply_filters( 'default_option_' . $option, $default );
  120.                 }
  121.  
  122. -               $alloptions = wp_load_alloptions();
  123. -
  124. -               if ( isset( $alloptions[$option] ) ) {
  125. -                       $value = $alloptions[$option];
  126. -               } else {
  127. -                       $value = wp_cache_get( $option, 'options' );
  128. -
  129. -                       if ( false === $value ) {
  130. +                    //if we got this far, and we still don't have the value, then request from the DB
  131.                                 $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
  132.  
  133.                                 // Has to be get_row instead of get_var because of funkiness with 0, false, null values
  134. @@ -88,7 +84,6 @@
  135.                                         return apply_filters( 'default_option_' . $option, $default );
  136.                                 }
  137.                         }
  138. -               }
  139.         } else {
  140.                 $suppress = $wpdb->suppress_errors();
  141.                 $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
  142. @@ -106,8 +101,8 @@
  143.                 return get_option( 'siteurl' );
  144.  
  145.         if ( in_array( $option, array('siteurl', 'home', 'category_base', 'tag_base') ) )
  146. -               $value = untrailingslashit( $value );
  147. -
  148. +                $value = rtrim( $value, '/\\' ); //$value = untrailingslashit( $value ); //untrailingslashit does not yet exist if called from within functions.php
  149. +                
  150.         /**
  151.          * Filter the value of an existing option.
  152.          *
  153. @@ -284,10 +279,19 @@
  154.          */
  155.         do_action( 'update_option', $option, $old_value, $value );
  156.  
  157. +        //what cases does $result == false?
  158. +        //if argument 2 or 3 are not arrays
  159. +        //prepare() returns '' if query is null (which it isnt)
  160. +        //query() returns false if $this->ready is false
  161. +        //also if db_connection does not exist
  162. +        //the mysql query generated an error
  163. +        //or if rows effected == 0
  164.         $result = $wpdb->update( $wpdb->options, array( 'option_value' => $serialized_value ), array( 'option_name' => $option ) );
  165. -       if ( ! $result )
  166. +        if ( ! $result && ! $result == 0) { //if update affected zero rows we still want to try to update the cache anyways
  167.                 return false;
  168. +        }
  169.  
  170. +        //remove it from notoptions if it was there
  171.         $notoptions = wp_cache_get( 'notoptions', 'options' );
  172.         if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
  173.                 unset( $notoptions[$option] );
  174. @@ -295,14 +299,14 @@
  175.         }
  176.  
  177.         if ( ! defined( 'WP_INSTALLING' ) ) {
  178. -               $alloptions = wp_load_alloptions();
  179. -               if ( isset( $alloptions[$option] ) ) {
  180. -                       $alloptions[ $option ] = $serialized_value;
  181. -                       wp_cache_set( 'alloptions', $alloptions, 'options' );
  182. -               } else {
  183. +                //$alloptions = wp_load_alloptions();
  184. +                //if ( isset( $alloptions[$option] ) ) {
  185. +                //        $alloptions[ $option ] = $serialized_value;
  186. +                //        wp_cache_set( 'alloptions', $alloptions, 'options' );
  187. +                //} else {
  188.                         wp_cache_set( $option, $serialized_value, 'options' );
  189. +                //}
  190.                 }
  191. -       }
  192.  
  193.         /**
  194.          * Fires after the value of a specific option has been successfully updated.
  195. @@ -315,7 +319,7 @@
  196.          * @param mixed $value     The new option value.
  197.          */
  198.         do_action( "update_option_{$option}", $old_value, $value );
  199. -
  200. +        
  201.         /**
  202.          * Fires after the value of an option has been successfully updated.
  203.          *
  204. @@ -385,22 +389,24 @@
  205.          */
  206.         do_action( 'add_option', $option, $value );
  207.  
  208. +        //why on update clause, didn't we just check a bunch of times whether the option already existed?
  209.         $result = $wpdb->query( $wpdb->prepare( "INSERT INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, %s) ON DUPLICATE KEY UPDATE `option_name` = VALUES(`option_name`), `option_value` = VALUES(`option_value`), `autoload` = VALUES(`autoload`)", $option, $serialized_value, $autoload ) );
  210.         if ( ! $result )
  211.                 return false;
  212. -
  213. +                
  214.         if ( ! defined( 'WP_INSTALLING' ) ) {
  215. -               if ( 'yes' == $autoload ) {
  216. -                       $alloptions = wp_load_alloptions();
  217. -                       $alloptions[ $option ] = $serialized_value;
  218. -                       wp_cache_set( 'alloptions', $alloptions, 'options' );
  219. -               } else {
  220. +                //if ( 'yes' == $autoload ) {
  221. +                        //$alloptions = wp_load_alloptions();
  222. +                //        $alloptions[ $option ] = $serialized_value;
  223. +                //        wp_cache_set( 'alloptions', $alloptions, 'options' );
  224. +                //} else {
  225.                         wp_cache_set( $option, $serialized_value, 'options' );
  226. +                //}
  227.                 }
  228. -       }
  229.  
  230.         // This option exists now
  231.         $notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh
  232. +        //but why must it be fresh? I guess another query might have run in the meantime?
  233.         if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
  234.                 unset( $notoptions[$option] );
  235.                 wp_cache_set( 'notoptions', $notoptions, 'options' );
  236. @@ -449,6 +455,7 @@
  237.         wp_protect_special_option( $option );
  238.  
  239.         // Get the ID, if no ID then return
  240. +        //checking if the option exists to be deleted
  241.         $row = $wpdb->get_row( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) );
  242.         if ( is_null( $row ) )
  243.                 return false;
  244. @@ -464,18 +471,22 @@
  245.  
  246.         $result = $wpdb->delete( $wpdb->options, array( 'option_name' => $option ) );
  247.         if ( ! defined( 'WP_INSTALLING' ) ) {
  248. -               if ( 'yes' == $row->autoload ) {
  249. -                       $alloptions = wp_load_alloptions();
  250. -                       if ( is_array( $alloptions ) && isset( $alloptions[$option] ) ) {
  251. -                               unset( $alloptions[$option] );
  252. -                               wp_cache_set( 'alloptions', $alloptions, 'options' );
  253. -                       }
  254. -               } else {
  255. +                //if ( 'yes' == $row->autoload ) {
  256. +                //        $alloptions = wp_load_alloptions();
  257. +                //        if ( is_array( $alloptions ) && isset( $alloptions[$option] ) ) {
  258. +                //                unset( $alloptions[$option] );
  259. +                //                wp_cache_set( 'alloptions', $alloptions, 'options' );
  260. +                //        }
  261. +                //} else {
  262.                         wp_cache_delete( $option, 'options' );
  263. +                //}
  264.                 }
  265. -       }
  266.         if ( $result ) {
  267. -
  268. +                //now to pre-emptively cache the negative result of the newly deleted option
  269. +                $notoptions = wp_cache_get( 'notoptions', 'options' );
  270. +                $notoptions[$option] = true;
  271. +                wp_cache_set( 'notoptions', $notoptions, 'options' );
  272. +            
  273.                 /**
  274.                  * Fires after a specific option has been deleted.
  275.                  *
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement