<!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" />
<title>[BuddyPress][7843] trunk: Modify bp_update_meta_cache() for new metadata cache schema</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { 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 #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#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>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://buddypress.trac.wordpress.org/changeset/7843">7843</a></dd>
<dt>Author</dt> <dd>boonebgorges</dd>
<dt>Date</dt> <dd>2014-02-11 04:51:43 +0000 (Tue, 11 Feb 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Modify bp_update_meta_cache() for new metadata cache schema

Now that we are using the WP metadata API, our cache keys have changed, and
the logic for priming meta caches must mirror these changes.

See <a href="http://buddypress.trac.wordpress.org/ticket/4551">#4551</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbpcorebpcorecachephp">trunk/bp-core/bp-core-cache.php</a></li>
<li><a href="#trunkbpgroupsbpgroupscachephp">trunk/bp-groups/bp-groups-cache.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkteststestcasesgroupscachephp">trunk/tests/testcases/groups/cache.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbpcorebpcorecachephp"></a>
<div class="modfile"><h4>Modified: trunk/bp-core/bp-core-cache.php (7842 => 7843)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-core/bp-core-cache.php  2014-02-11 04:11:07 UTC (rev 7842)
+++ trunk/bp-core/bp-core-cache.php     2014-02-11 04:51:43 UTC (rev 7843)
</span><span class="lines">@@ -179,6 +179,7 @@
</span><span class="cx">  $defaults = array(
</span><span class="cx">          'object_ids'       => array(), // Comma-separated list or array of item ids
</span><span class="cx">          'object_type'      => '',      // Canonical component id: groups, members, etc
</span><ins>+               'cache_group'      => '',      // Cache group
</ins><span class="cx">           'meta_table'       => '',      // Name of the table containing the metadata
</span><span class="cx">          'object_column'    => '',      // DB column for the object ids (group_id, etc)
</span><span class="cx">          'cache_key_prefix' => ''       // Prefix to use when creating cache key names. Eg
</span><span class="lines">@@ -187,7 +188,7 @@
</span><span class="cx">  $r = wp_parse_args( $args, $defaults );
</span><span class="cx">  extract( $r );
</span><span class="cx"> 
</span><del>-       if ( empty( $object_ids ) || empty( $object_type ) || empty( $meta_table ) ) {
</del><ins>+        if ( empty( $object_ids ) || empty( $object_type ) || empty( $meta_table ) || empty( $cache_group ) ) {
</ins><span class="cx">           return false;
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="lines">@@ -199,37 +200,39 @@
</span><span class="cx">          $object_column = $object_type . '_id';
</span><span class="cx">  }
</span><span class="cx"> 
</span><del>-       $object_ids = wp_parse_id_list( $object_ids );
</del><ins>+        if ( ! $cache_group ) {
+               return false;
+       }
</ins><span class="cx"> 
</span><ins>+       $object_ids   = wp_parse_id_list( $object_ids );
+       $uncached_ids = bp_get_non_cached_ids( $object_ids, $cache_group );
+
</ins><span class="cx">   $cache = array();
</span><span class="cx"> 
</span><span class="cx">  // Get meta info
</span><del>-       $id_list   = join( ',', $object_ids );
-       $meta_list = $wpdb->get_results( $wpdb->prepare( "SELECT {$object_column}, meta_key, meta_value FROM {$meta_table} WHERE {$object_column} IN ($id_list)", $object_type ), ARRAY_A );
</del><ins>+        if ( ! empty( $uncached_ids ) ) {
+               $id_list   = join( ',', $object_ids );
+               $meta_list = $wpdb->get_results( $wpdb->prepare( "SELECT {$object_column}, meta_key, meta_value FROM {$meta_table} WHERE {$object_column} IN ($id_list)", $object_type ), ARRAY_A );
</ins><span class="cx"> 
</span><del>-       if ( !empty( $meta_list ) ) {
-               foreach ( $meta_list as $metarow ) {
-                       $mpid = intval( $metarow[$object_column] );
-                       $mkey = $metarow['meta_key'];
-                       $mval = $metarow['meta_value'];
</del><ins>+                if ( ! empty( $meta_list ) ) {
+                       foreach ( $meta_list as $metarow ) {
+                               $mpid = intval( $metarow[$object_column] );
+                               $mkey = $metarow['meta_key'];
+                               $mval = $metarow['meta_value'];
</ins><span class="cx"> 
</span><del>-                       // Force subkeys to be array type:
-                       if ( !isset( $cache[$mpid] ) || !is_array( $cache[$mpid] ) )
-                               $cache[$mpid] = array();
-                       if ( !isset( $cache[$mpid][$mkey] ) || !is_array( $cache[$mpid][$mkey] ) )
-                               $cache[$mpid][$mkey] = array();
</del><ins>+                                // Force subkeys to be array type:
+                               if ( !isset( $cache[$mpid] ) || !is_array( $cache[$mpid] ) )
+                                       $cache[$mpid] = array();
+                               if ( !isset( $cache[$mpid][$mkey] ) || !is_array( $cache[$mpid][$mkey] ) )
+                                       $cache[$mpid][$mkey] = array();
</ins><span class="cx"> 
</span><del>-                       // Add a value to the current pid/key:
-                       $cache[$mpid][$mkey][] = $mval;
</del><ins>+                                // Add a value to the current pid/key:
+                               $cache[$mpid][$mkey][] = $mval;
+                       }
</ins><span class="cx">           }
</span><del>-       }
</del><span class="cx"> 
</span><del>-       foreach ( $object_ids as $id ) {
-               if ( ! isset($cache[$id]) )
-                       $cache[$id] = array();
-
-               foreach( $cache[$id] as $meta_key => $meta_value ) {
-                       wp_cache_set( $cache_key_prefix . '_' . $id . '_' . $meta_key, $meta_value, 'bp' );
</del><ins>+                foreach ( $cache as $object_id => $cache_val ) {
+                       wp_cache_set( $object_id, $cache_val, $cache_group );
</ins><span class="cx">           }
</span><span class="cx">  }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkbpgroupsbpgroupscachephp"></a>
<div class="modfile"><h4>Modified: trunk/bp-groups/bp-groups-cache.php (7842 => 7843)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-groups/bp-groups-cache.php      2014-02-11 04:11:07 UTC (rev 7842)
+++ trunk/bp-groups/bp-groups-cache.php 2014-02-11 04:51:43 UTC (rev 7843)
</span><span class="lines">@@ -33,6 +33,7 @@
</span><span class="cx">  $cache_args = array(
</span><span class="cx">          'object_ids'       => $group_ids,
</span><span class="cx">          'object_type'      => $bp->groups->id,
</span><ins>+               'cache_group'      => 'group_meta',
</ins><span class="cx">           'object_column'    => 'group_id',
</span><span class="cx">          'meta_table'       => $bp->groups->table_name_groupmeta,
</span><span class="cx">          'cache_key_prefix' => 'bp_groups_groupmeta'
</span></span></pre></div>
<a id="trunkteststestcasesgroupscachephp"></a>
<div class="addfile"><h4>Added: trunk/tests/testcases/groups/cache.php (0 => 7843)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/testcases/groups/cache.php                           (rev 0)
+++ trunk/tests/testcases/groups/cache.php      2014-02-11 04:51:43 UTC (rev 7843)
</span><span class="lines">@@ -0,0 +1,104 @@
</span><ins>+<?php
+
+/**
+ * @group groups
+ * @group cache
+ */
+class BP_Tests_Group_Cache extends BP_UnitTestCase {
+       /**
+        * @group bp_groups_update_meta_cache
+        */
+       public function test_bp_groups_update_meta_cache() {
+               $g1 = $this->factory->group->create();
+               $g2 = $this->factory->group->create();
+
+               $time = bp_core_current_time();
+
+               // Set up some data
+               groups_update_groupmeta( $g1, 'total_member_count', 4 );
+               groups_update_groupmeta( $g1, 'last_activity', $time );
+               groups_update_groupmeta( $g1, 'foo', 'bar' );
+
+               groups_update_groupmeta( $g2, 'total_member_count', 81 );
+               groups_update_groupmeta( $g2, 'last_activity', $time );
+               groups_update_groupmeta( $g2, 'foo', 'baz' );
+
+               // Prime the cache for $g1
+               groups_update_groupmeta( $g1, 'foo', 'bar' );
+               groups_get_groupmeta( $g1, 'foo' );
+
+               // Ensure an empty cache for $g2
+               wp_cache_delete( $g2, 'group_meta' );
+
+               bp_groups_update_meta_cache( array( $g1, $g2 ) );
+
+               $expected = array(
+                       $g1 => array(
+                               'total_member_count' => array(
+                                       4,
+                               ),
+                               'last_activity' => array(
+                                       $time,
+                               ),
+                               'foo' => array(
+                                       'bar',
+                               ),
+                       ),
+                       $g2 => array(
+                               'total_member_count' => array(
+                                       81,
+                               ),
+                               'last_activity' => array(
+                                       $time,
+                               ),
+                               'foo' => array(
+                                       'baz',
+                               ),
+                       ),
+               );
+
+               $found = array(
+                       $g1 => wp_cache_get( $g1, 'group_meta' ),
+                       $g2 => wp_cache_get( $g2, 'group_meta' ),
+               );
+
+               $this->assertEquals( $expected, $found );
+       }
+
+       /**
+        * @group groups_update_groupmeta
+        * @group groups_delete_group_cache_on_metadata_change
+        */
+       public function test_bp_groups_delete_group_cache_on_metadata_add() {
+               $g = $this->factory->group->create();
+
+               // Prime cache
+               groups_get_group( array( 'group_id' => $g ) );
+
+               $this->assertNotEmpty( wp_cache_get( 'bp_groups_group_' . $g . '_noload_users', 'bp' ) );
+
+               // Trigger flush
+               groups_update_groupmeta( $g, 'foo', 'bar' );
+
+               $this->assertFalse( wp_cache_get( 'bp_groups_group_' . $g . '_noload_users', 'bp' ) );
+       }
+
+       /**
+        * @group groups_update_groupmeta
+        * @group groups_delete_group_cache_on_metadata_change
+        */
+       public function test_bp_groups_delete_group_cache_on_metadata_change() {
+               $g = $this->factory->group->create();
+
+               // Prime cache
+               groups_update_groupmeta( $g, 'foo', 'bar' );
+               groups_get_group( array( 'group_id' => $g ) );
+
+               $this->assertNotEmpty( wp_cache_get( 'bp_groups_group_' . $g . '_noload_users', 'bp' ) );
+
+               // Trigger flush
+               groups_update_groupmeta( $g, 'foo', 'baz' );
+               $this->assertFalse( wp_cache_get( 'bp_groups_group_' . $g . '_noload_users', 'bp' ) );
+       }
+
+}
</ins></span></pre>
</div>
</div>

</body>
</html>