Skip to:
Content

BuddyPress.org


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

File:
1 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?>
Note: See TracChangeset for help on using the changeset viewer.