<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[BuddyPress] [2617] trunk:
Added proper caching to object count and meta functions.</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd>2617</dd>
<dt>Author</dt> <dd>apeatling</dd>
<dt>Date</dt> <dd>2010-02-07 15:21:39 +0000 (Sun, 07 Feb 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>Added proper caching to object count and meta functions.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbpblogsphp">trunk/bp-blogs.php</a></li>
<li><a href="#trunkbpcorebpcoreactivationphp">trunk/bp-core/bp-core-activation.php</a></li>
<li><a href="#trunkbpcorebpcoresignupphp">trunk/bp-core/bp-core-signup.php</a></li>
<li><a href="#trunkbpcorephp">trunk/bp-core.php</a></li>
<li><a href="#trunkbpfriendsphp">trunk/bp-friends.php</a></li>
<li><a href="#trunkbpgroupsphp">trunk/bp-groups.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbpblogsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-blogs.php (2616 => 2617)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-blogs.php        2010-02-07 14:36:39 UTC (rev 2616)
+++ trunk/bp-blogs.php        2010-02-07 15:21:39 UTC (rev 2617)
</span><span class="lines">@@ -604,8 +604,11 @@
</span><span class="cx"> add_action( 'delete_comment', 'bp_blogs_remove_comment' );
</span><span class="cx">
</span><span class="cx"> function bp_blogs_total_blogs() {
</span><del>-        $blogs = BP_Blogs_Blog::get_all();
-        return $blogs['total'];
</del><ins>+        if ( !$count = wp_cache_get( 'bp_total_blogs', 'bp' ) ) {
+                $blogs = BP_Blogs_Blog::get_all();
+                $count = $blogs['total'];
+        }
+        return $count;
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> function bp_blogs_total_blogs_for_user( $user_id = false ) {
</span><span class="lines">@@ -614,7 +617,10 @@
</span><span class="cx">         if ( !$user_id )
</span><span class="cx">                 $user_id = ( $bp->displayed_user->id ) ? $bp->displayed_user->id : $bp->loggedin_user->id;
</span><span class="cx">
</span><del>-        return BP_Blogs_Blog::total_blog_count_for_user( $user_id );
</del><ins>+        if ( !$count = wp_cache_get( 'bp_total_blogs_for_user_' . $user_id, 'bp' ) )
+                $count = BP_Blogs_Blog::total_blog_count_for_user( $user_id );
+
+        return $count;
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> function bp_blogs_remove_data_for_blog( $blog_id ) {
</span><span class="lines">@@ -717,8 +723,7 @@
</span><span class="cx">                 $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->blogs->table_name_blogmeta} WHERE blog_id = %d AND meta_key = %s", $blog_id, $meta_key ) );
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        // TODO need to look into using this.
-        // wp_cache_delete($group_id, 'groups');
</del><ins>+        wp_cache_delete( 'bp_blogs_blogmeta_' . $blog_id . '_' . $meta_key, 'bp' );
</ins><span class="cx">
</span><span class="cx">         return true;
</span><span class="cx"> }
</span><span class="lines">@@ -734,14 +739,10 @@
</span><span class="cx">         if ( !empty($meta_key) ) {
</span><span class="cx">                 $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);
</span><span class="cx">
</span><del>-                // TODO need to look into using this.
-                //$user = wp_cache_get($user_id, 'users');
-
-                // Check the cached user object
-                //if ( false !== $user && isset($user->$meta_key) )
-                //        $metas = array($user->$meta_key);
-                //else
-                $metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM {$bp->blogs->table_name_blogmeta} WHERE blog_id = %d AND meta_key = %s", $blog_id, $meta_key) );
</del><ins>+                if ( !$metas = wp_cache_get( 'bp_blogs_blogmeta_' . $blog_id . '_' . $meta_key, 'bp' ) ) {
+                        $metas = $wpdb->get_col( $wpdb->prepare( "SELECT meta_value FROM {$bp->blogs->table_name_blogmeta} WHERE blog_id = %d AND meta_key = %s", $blog_id, $meta_key ) );
+                        wp_cache_set( 'bp_blogs_blogmeta_' . $blog_id . '_' . $meta_key, $metas, 'bp' );
+                }
</ins><span class="cx">         } else {
</span><span class="cx">                 $metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM {$bp->blogs->table_name_blogmeta} WHERE blog_id = %d", $blog_id) );
</span><span class="cx">         }
</span><span class="lines">@@ -788,8 +789,7 @@
</span><span class="cx">                 return false;
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        // TODO need to look into using this.
-        // wp_cache_delete($user_id, 'users');
</del><ins>+        wp_cache_set( 'bp_blogs_blogmeta_' . $blog_id . '_' . $meta_key, $metas, 'bp' );
</ins><span class="cx">
</span><span class="cx">         return true;
</span><span class="cx"> }
</span><span class="lines">@@ -828,6 +828,7 @@
</span><span class="cx"> function bp_blogs_clear_blog_object_cache( $blog_id, $user_id ) {
</span><span class="cx">         wp_cache_delete( 'bp_blogs_of_user_' . $user_id, 'bp' );
</span><span class="cx">         wp_cache_delete( 'bp_blogs_for_user_' . $user_id, 'bp' );
</span><ins>+        wp_cache_delete( 'bp_total_blogs_for_user_' . $user_id, 'bp' );
</ins><span class="cx">
</span><span class="cx">         /* Clear the sitewide activity cache */
</span><span class="cx">         wp_cache_delete( 'sitewide_activity', 'bp' );
</span><span class="lines">@@ -838,6 +839,7 @@
</span><span class="cx">
</span><span class="cx">         /* Clear the sitewide activity cache */
</span><span class="cx">         wp_cache_delete( 'sitewide_activity', 'bp' );
</span><ins>+        wp_cache_delete( 'bp_total_blogs', 'bp' );
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> function bp_blogs_clear_post_object_cache( $blog_id, $post_id, $user_id ) {
</span></span></pre></div>
<a id="trunkbpcorebpcoreactivationphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-core/bp-core-activation.php (2616 => 2617)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-core/bp-core-activation.php        2010-02-07 14:36:39 UTC (rev 2616)
+++ trunk/bp-core/bp-core-activation.php        2010-02-07 15:21:39 UTC (rev 2617)
</span><span class="lines">@@ -61,6 +61,8 @@
</span><span class="cx">                 }
</span><span class="cx">
</span><span class="cx">                 do_action( 'bp_core_account_activated', &$signup, $_GET['key'] );
</span><ins>+                wp_cache_delete( 'bp_total_member_count', 'bp' );
+
</ins><span class="cx">                 bp_core_add_message( __( 'Your account is now active!', 'buddypress' ) );
</span><span class="cx">
</span><span class="cx">                 $bp->activation_complete = true;
</span></span></pre></div>
<a id="trunkbpcorebpcoresignupphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-core/bp-core-signup.php (2616 => 2617)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-core/bp-core-signup.php        2010-02-07 14:36:39 UTC (rev 2616)
+++ trunk/bp-core/bp-core-signup.php        2010-02-07 15:21:39 UTC (rev 2617)
</span><span class="lines">@@ -230,6 +230,7 @@
</span><span class="cx">         update_usermeta( $user_id, 'last_activity', gmdate( "Y-m-d H:i:s" ) );
</span><span class="cx">
</span><span class="cx">         wp_new_user_notification( $user_id, $user_pass );
</span><ins>+        wp_cache_delete( 'bp_total_member_count', 'bp' );
</ins><span class="cx">
</span><span class="cx">         $bp->signup->username = $user_login;
</span><span class="cx">
</span></span></pre></div>
<a id="trunkbpcorephp"></a>
<div class="modfile"><h4>Modified: trunk/bp-core.php (2616 => 2617)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-core.php        2010-02-07 14:36:39 UTC (rev 2616)
+++ trunk/bp-core.php        2010-02-07 15:21:39 UTC (rev 2617)
</span><span class="lines">@@ -1201,9 +1201,11 @@
</span><span class="cx"> function bp_core_get_total_member_count() {
</span><span class="cx">         global $wpdb, $bp;
</span><span class="cx">
</span><del>-        $status_sql = bp_core_get_status_sql();
</del><ins>+        if ( !$count = wp_cache_get( 'bp_total_member_count', 'bp' ) ) {
+                $status_sql = bp_core_get_status_sql();
+                $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM " . CUSTOM_USER_TABLE . " WHERE {$status_sql}" ) );
+        }
</ins><span class="cx">
</span><del>-        $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM " . CUSTOM_USER_TABLE . " WHERE {$status_sql}" ) );
</del><span class="cx">         return apply_filters( 'bp_core_get_total_member_count', $count );
</span><span class="cx"> }
</span><span class="cx">
</span></span></pre></div>
<a id="trunkbpfriendsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-friends.php (2616 => 2617)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-friends.php        2010-02-07 14:36:39 UTC (rev 2616)
+++ trunk/bp-friends.php        2010-02-07 15:21:39 UTC (rev 2617)
</span><span class="lines">@@ -441,7 +441,6 @@
</span><span class="cx">                 friends_notification_accepted_request( $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id );
</span><span class="cx">
</span><span class="cx">                 do_action( 'friends_friendship_accepted', $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id );
</span><del>-
</del><span class="cx">                 return true;
</span><span class="cx">         }
</span><span class="cx">
</span><span class="lines">@@ -482,11 +481,11 @@
</span><span class="cx">         if ( !$user_id )
</span><span class="cx">                 $user_id = ( $bp->displayed_user->id ) ? $bp->displayed_user->id : $bp->loggedin_user->id;
</span><span class="cx">
</span><del>-        $count = get_usermeta( $user_id, 'total_friend_count' );
</del><ins>+        if ( !$count = wp_cache_get( 'bp_total_friend_count_' . $user_id, 'bp' ) ) {
+                $count = get_usermeta( $user_id, 'total_friend_count' );
+                if ( empty( $count ) ) $count = 0;
+        }
</ins><span class="cx">
</span><del>-        if ( empty( $count ) )
-                $count = 0;
-
</del><span class="cx">         return apply_filters( 'friends_get_total_friend_count', $count );
</span><span class="cx"> }
</span><span class="cx">
</span><span class="lines">@@ -631,6 +630,8 @@
</span><span class="cx">
</span><span class="cx">         wp_cache_delete( 'friends_friend_ids_' . $friendship->initiator_user_id, 'bp' );
</span><span class="cx">         wp_cache_delete( 'friends_friend_ids_' . $friendship->friend_user_id, 'bp' );
</span><ins>+        wp_cache_delete( 'bp_total_friend_count_' . $friendship->initiator_user_id, 'bp' );
+        wp_cache_delete( 'bp_total_friend_count_' . $friendship->friend_user_id, 'bp' );
</ins><span class="cx">         wp_cache_delete( 'popular_users', 'bp' );
</span><span class="cx">
</span><span class="cx">         /* Clear the sitewide activity cache */
</span></span></pre></div>
<a id="trunkbpgroupsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-groups.php (2616 => 2617)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-groups.php        2010-02-07 14:36:39 UTC (rev 2616)
+++ trunk/bp-groups.php        2010-02-07 15:21:39 UTC (rev 2617)
</span><span class="lines">@@ -1629,8 +1629,6 @@
</span><span class="cx">         if ( !groups_uninvite_user( $user_id, $group_id ) )
</span><span class="cx">                 return false;
</span><span class="cx">
</span><del>-        do_action( 'groups_leave_group', $group_id, $user_id );
-
</del><span class="cx">         /* Modify group member count */
</span><span class="cx">         groups_update_groupmeta( $group_id, 'total_member_count', (int) groups_get_groupmeta( $group_id, 'total_member_count') - 1 );
</span><span class="cx">
</span><span class="lines">@@ -1639,6 +1637,8 @@
</span><span class="cx">
</span><span class="cx">         bp_core_add_message( __( 'You successfully left the group.', 'buddypress' ) );
</span><span class="cx">
</span><ins>+        do_action( 'groups_leave_group', $group_id, $user_id );
+
</ins><span class="cx">         return true;
</span><span class="cx"> }
</span><span class="cx">
</span><span class="lines">@@ -1749,7 +1749,10 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> function groups_get_total_group_count() {
</span><del>-        return BP_Groups_Group::get_total_group_count();
</del><ins>+        if ( !$count = wp_cache_get( 'bp_total_group_count', 'bp' ) )
+                $count = BP_Groups_Group::get_total_group_count();
+
+        return $count;
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> function groups_get_user_groups( $user_id = false, $pag_num = false, $pag_page = false ) {
</span><span class="lines">@@ -1794,7 +1797,10 @@
</span><span class="cx">         if ( !$user_id )
</span><span class="cx">                 $user_id = ( $bp->displayed_user->id ) ? $bp->displayed_user->id : $bp->loggedin_user->id;
</span><span class="cx">
</span><del>-        return BP_Groups_Member::total_group_count( $user_id );
</del><ins>+        if ( !$count = wp_cache_get( 'bp_total_groups_for_user_' . $user_id, 'bp' ) )
+                $count = BP_Groups_Member::total_group_count( $user_id );
+
+        return $count;
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> function groups_search_groups( $search_terms, $pag_num_per_page = 5, $pag_page = 1, $sort_by = false, $order = false ) {
</span><span class="lines">@@ -2234,7 +2240,7 @@
</span><span class="cx">
</span><span class="cx">         $member = new BP_Groups_Member( $user_id, $group_id );
</span><span class="cx">
</span><del>-        do_action( 'groups_premote_member', $user_id, $group_id, $status );
</del><ins>+        do_action( 'groups_premote_member', $group_id, $user_id, $status );
</ins><span class="cx">
</span><span class="cx">         return $member->promote( $status );
</span><span class="cx"> }
</span><span class="lines">@@ -2244,7 +2250,7 @@
</span><span class="cx">
</span><span class="cx">         $member = new BP_Groups_Member( $user_id, $group_id );
</span><span class="cx">
</span><del>-        do_action( 'groups_demote_member', $user_id, $group_id );
</del><ins>+        do_action( 'groups_demote_member', $group_id, $user_id );
</ins><span class="cx">
</span><span class="cx">         return $member->demote();
</span><span class="cx"> }
</span><span class="lines">@@ -2257,7 +2263,7 @@
</span><span class="cx">
</span><span class="cx">         $member = new BP_Groups_Member( $user_id, $group_id );
</span><span class="cx">
</span><del>-        do_action( 'groups_ban_member', $user_id, $group_id );
</del><ins>+        do_action( 'groups_ban_member', $group_id, $user_id );
</ins><span class="cx">
</span><span class="cx">         if ( !$member->ban() )
</span><span class="cx">                 return false;
</span><span class="lines">@@ -2273,7 +2279,7 @@
</span><span class="cx">
</span><span class="cx">         $member = new BP_Groups_Member( $user_id, $group_id );
</span><span class="cx">
</span><del>-        do_action( 'groups_unban_member', $user_id, $group_id );
</del><ins>+        do_action( 'groups_unban_member', $group_id, $user_id );
</ins><span class="cx">
</span><span class="cx">         return $member->unban();
</span><span class="cx"> }
</span><span class="lines">@@ -2421,8 +2427,8 @@
</span><span class="cx">                 $wpdb->query( $wpdb->prepare( "DELETE FROM " . $bp->groups->table_name_groupmeta . " WHERE group_id = %d AND meta_key = %s", $group_id, $meta_key ) );
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        // TODO need to look into using this.
-        // wp_cache_delete($group_id, 'groups');
</del><ins>+        /* Delete the cached object */
+        wp_cache_delete( 'bp_groups_groupmeta_' . $group_id . '_' . $meta_key, 'bp' );
</ins><span class="cx">
</span><span class="cx">         return true;
</span><span class="cx"> }
</span><span class="lines">@@ -2438,14 +2444,10 @@
</span><span class="cx">         if ( !empty($meta_key) ) {
</span><span class="cx">                 $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);
</span><span class="cx">
</span><del>-                // TODO need to look into using this.
-                //$user = wp_cache_get($user_id, 'users');
-
-                // Check the cached user object
-                //if ( false !== $user && isset($user->$meta_key) )
-                //        $metas = array($user->$meta_key);
-                //else
-                $metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM " . $bp->groups->table_name_groupmeta . " WHERE group_id = %d AND meta_key = %s", $group_id, $meta_key) );
</del><ins>+                if ( !$metas = wp_cache_get( 'bp_groups_groupmeta_' . $group_id . '_' . $meta_key, 'bp' ) ) {
+                        $metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM " . $bp->groups->table_name_groupmeta . " WHERE group_id = %d AND meta_key = %s", $group_id, $meta_key) );
+                        wp_cache_set( 'bp_groups_groupmeta_' . $group_id . '_' . $meta_key, $metas, 'bp' );
+                }
</ins><span class="cx">         } else {
</span><span class="cx">                 $metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM " . $bp->groups->table_name_groupmeta . " WHERE group_id = %d", $group_id) );
</span><span class="cx">         }
</span><span class="lines">@@ -2492,8 +2494,8 @@
</span><span class="cx">                 return false;
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        // TODO need to look into using this.
-        // wp_cache_delete($user_id, 'users');
</del><ins>+        /* Update the cached object and recache */
+        wp_cache_set( 'bp_groups_groupmeta_' . $group_id . '_' . $meta_key, $meta_value, 'bp' );
</ins><span class="cx">
</span><span class="cx">         return true;
</span><span class="cx"> }
</span><span class="lines">@@ -2547,15 +2549,23 @@
</span><span class="cx">         wp_cache_delete( 'active_groups', 'bp' );
</span><span class="cx">         wp_cache_delete( 'popular_groups', 'bp' );
</span><span class="cx">         wp_cache_delete( 'groups_random_groups', 'bp' );
</span><ins>+        wp_cache_delete( 'bp_total_group_count', 'bp' );
</ins><span class="cx"> }
</span><del>-
-// List actions to clear object caches on
</del><span class="cx"> add_action( 'groups_group_deleted', 'groups_clear_group_object_cache' );
</span><span class="cx"> add_action( 'groups_settings_updated', 'groups_clear_group_object_cache' );
</span><span class="cx"> add_action( 'groups_details_updated', 'groups_clear_group_object_cache' );
</span><span class="cx"> add_action( 'groups_group_avatar_updated', 'groups_clear_group_object_cache' );
</span><ins>+add_action( 'groups_create_group_step_complete', 'groups_clear_group_object_cache' );
</ins><span class="cx">
</span><del>-// List actions to clear super cached pages on, if super cache is installed
</del><ins>+function groups_clear_group_user_object_cache( $group_id, $user_id ) {
+        wp_cache_delete( 'bp_total_groups_for_user_' . $user_id );
+}
+add_action( 'groups_join_group', 'groups_clear_group_user_object_cache', 10, 2 );
+add_action( 'groups_leave_group', 'groups_clear_group_user_object_cache', 10, 2 );
+add_action( 'groups_ban_member', 'groups_clear_group_user_object_cache', 10, 2 );
+add_action( 'groups_unban_member', 'groups_clear_group_user_object_cache', 10, 2 );
+
+/* List actions to clear super cached pages on, if super cache is installed */
</ins><span class="cx"> add_action( 'groups_join_group', 'bp_core_clear_cache' );
</span><span class="cx"> add_action( 'groups_leave_group', 'bp_core_clear_cache' );
</span><span class="cx"> add_action( 'groups_accept_invite', 'bp_core_clear_cache' );
</span></span></pre>
</div>
</div>
</body>
</html>