Skip to:
Content

BuddyPress.org

Changeset 5460


Ignore:
Timestamp:
12/08/2011 02:57:38 AM (13 years ago)
Author:
boonebgorges
Message:

Introduces automatic meta cache features.
Introduces bp_update_meta_cache(), for collecting all metadata associated with an object or with multiple objects in one fell swoop, and adding it to the WP cache for quicker access later
Introduces bp_groups_update_meta_cache(), for collecting groupmeta at the beginning of a group loop
Modifies the BP_Groups_Group object so that groupmeta is fetched in the populate() and get() methods
Fixes #3799

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-core/bp-core-cache.php

    r4961 r5460  
    4949add_action( 'bp_core_render_notice', 'bp_core_clear_cache' );
    5050
     51/**
     52 * Update the metadata cache for the specified objects.
     53 *
     54 * @since 1.6
     55 * @uses $wpdb WordPress database object for queries.
     56 * @uses $bp BuddyPress global object.
     57 *
     58 * @param array $args See $defaults definition for more details
     59 * @return mixed Metadata cache for the specified objects, or false on failure.
     60 */
     61function bp_update_meta_cache( $args = array() ) {
     62    global $bp, $wpdb;
     63   
     64    $defaults = array(
     65        'object_ids'       => array(), // Comma-separated list or array of item ids
     66        'object_type'      => '',      // Canonical component id: groups, members, etc
     67        'meta_table'       => '',      // Name of the table containing the metadata
     68        'object_column'    => '',      // DB column for the object ids (group_id, etc)
     69        'cache_key_prefix' => ''       // Prefix to use when creating cache key names. Eg
     70                           //    'bp_groups_groupmeta'
     71    );
     72    $r = wp_parse_args( $args, $defaults );
     73    extract( $r );
     74       
     75    if ( empty( $object_ids ) || empty( $object_type ) || empty( $meta_table ) ) {
     76        return false;
     77    }
     78   
     79    if ( empty( $cache_key_prefix ) ) {
     80        $cache_key_prefix = $meta_table;
     81    }
     82   
     83    if ( empty( $object_column ) ) {
     84        $object_column = $object_type . '_id';
     85    }
     86
     87    if ( !is_array( $object_ids ) ) {
     88        $object_ids = preg_replace( '|[^0-9,]|', '', $object_ids );
     89        $object_ids = explode( ',', $object_ids );
     90    }
     91
     92    $object_ids = array_map( 'intval', $object_ids );
     93
     94    $cache = array();
     95   
     96    // Get meta info
     97    $id_list   = join( ',', $object_ids );
     98    $meta_list = $wpdb->get_results( $wpdb->prepare( "SELECT $object_column, meta_key, meta_value FROM $meta_table WHERE group_id IN ($id_list)" ), ARRAY_A );
     99
     100    if ( !empty( $meta_list ) ) {
     101        foreach ( $meta_list as $metarow ) {
     102            $mpid = intval( $metarow[$object_column] );
     103            $mkey = $metarow['meta_key'];
     104            $mval = $metarow['meta_value'];
     105
     106            // Force subkeys to be array type:
     107            if ( !isset( $cache[$mpid] ) || !is_array( $cache[$mpid] ) )
     108                $cache[$mpid] = array();
     109            if ( !isset( $cache[$mpid][$mkey] ) || !is_array( $cache[$mpid][$mkey] ) )
     110                $cache[$mpid][$mkey] = array();
     111
     112            // Add a value to the current pid/key:
     113            $cache[$mpid][$mkey][] = $mval;
     114        }
     115    }
     116   
     117    foreach ( $object_ids as $id ) {
     118        if ( ! isset($cache[$id]) )
     119            $cache[$id] = array();
     120   
     121        foreach( $cache[$id] as $meta_key => $meta_value ) {
     122            wp_cache_set( $cache_key_prefix . '_' . $id . '_' . $meta_key, $meta_value, 'bp' );
     123        }
     124    }
     125
     126    return $cache;
     127}
     128
    51129?>
  • trunk/bp-groups/bp-groups-cache.php

    r4961 r5460  
    1212// Exit if accessed directly
    1313if ( !defined( 'ABSPATH' ) ) exit;
     14
     15/**
     16 * Slurps up groupmeta
     17 *
     18 * This function is called in two places in the BP_Groups_Group class:
     19 *   - in the populate() method, when single group objects are populated
     20 *   - in the get() method, when multiple groups are queried
     21 *
     22 * It grabs all groupmeta associated with all of the groups passed in $group_ids and adds it to
     23 * the WP cache. This improves efficiency when using groupmeta inline
     24 *
     25 * @param int|str|array $group_ids Accepts a single group_id, or a comma-separated list or array of
     26 *    group ids
     27 */
     28function bp_groups_update_meta_cache( $group_ids = false ) {
     29    global $bp;
     30   
     31    $cache_args = array(
     32        'object_ids'       => $group_ids,
     33        'object_type'      => $bp->groups->id,
     34        'object_column'    => 'group_id',
     35        'meta_table'       => $bp->groups->table_name_groupmeta,
     36        'cache_key_prefix' => 'bp_groups_groupmeta'
     37    );
     38   
     39    bp_update_meta_cache( $cache_args );
     40}
    1441
    1542function groups_clear_group_object_cache( $group_id ) {
  • trunk/bp-groups/bp-groups-classes.php

    r5431 r5460  
    3131        global $wpdb, $bp;
    3232
    33         if ( $group = $wpdb->get_row( $wpdb->prepare( "SELECT g.*, gm.meta_value as last_activity, gm2.meta_value as total_member_count FROM {$bp->groups->table_name} g, {$bp->groups->table_name_groupmeta} gm, {$bp->groups->table_name_groupmeta} gm2 WHERE g.id = gm.group_id AND g.id = gm2.group_id AND gm.meta_key = 'last_activity' AND gm2.meta_key = 'total_member_count' AND g.id = %d", $this->id ) ) ) {
     33        if ( $group = $wpdb->get_row( $wpdb->prepare( "SELECT g.* FROM {$bp->groups->table_name} g WHERE g.id = %d", $this->id ) ) ) {         
     34            bp_groups_update_meta_cache( $this->id );
     35                       
    3436            $this->id                 = $group->id;
    3537            $this->creator_id         = $group->creator_id;
     
    4042            $this->enable_forum       = $group->enable_forum;
    4143            $this->date_created       = $group->date_created;
    42             $this->last_activity      = $group->last_activity;
    43             $this->total_member_count = $group->total_member_count;
     44            $this->last_activity      = groups_get_groupmeta( $this->id, 'last_activity' );
     45            $this->total_member_count = groups_get_groupmeta( $this->id, 'total_member_count' );
    4446            $this->is_member          = BP_Groups_Member::check_is_member( bp_loggedin_user_id(), $this->id );
    4547
     
    368370        $total_groups     = $wpdb->get_var( $total_groups_sql );
    369371
     372        $group_ids = array();
     373        foreach ( (array)$paged_groups as $group ) {
     374            $group_ids[] = $group->id;
     375        }
     376       
    370377        /* Populate some extra information instead of querying each time in the loop */
    371378        if ( !empty( $populate_extras ) ) {
    372             $group_ids = array();
    373             foreach ( (array)$paged_groups as $group ) $group_ids[] = $group->id;
    374379            $group_ids = $wpdb->escape( join( ',', (array)$group_ids ) );
    375380            $paged_groups = BP_Groups_Group::get_group_extras( $paged_groups, $group_ids, $type );
    376381        }
     382       
     383        // Grab all groupmeta
     384        bp_groups_update_meta_cache( $group_ids );
    377385
    378386        unset( $sql, $total_sql );
  • trunk/bp-groups/bp-groups-functions.php

    r5454 r5460  
    4747    if ( !$group = wp_cache_get( $cache_key, 'bp' ) ) {
    4848        $group = new BP_Groups_Group( $group_id, true, $load_users );
    49         wp_cache_set( $cache_key, $group, 'bp' );
     49        wp_cache_set( $cache_key, $group, 'bp' );   
    5050    }
    5151
     
    919919        $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
    920920
    921         $metas = wp_cache_get( 'bp_groups_groupmeta_' . $group_id . '_' . $meta_key, 'bp' );       
     921        $metas = wp_cache_get( 'bp_groups_groupmeta_' . $group_id . '_' . $meta_key, 'bp' );
    922922        if ( false === $metas ) {
    923923            $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) );
Note: See TracChangeset for help on using the changeset viewer.