Changeset 619740
- Timestamp:
- 11/01/2012 08:17:20 AM (12 years ago)
- Location:
- wp-cache-users/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
wp-cache-users/trunk/readme.txt
r84127 r619740 2 2 Tags: performance, speed, mysql, queries, cache, caching, faster, _ck_ 3 3 Contributors: _ck_ 4 Requires at least: 2.35 Tested up to: 2.5.14 Requires at least: 5 Tested up to: 6 6 Stable tag: trunk 7 7 Donate link: http://bbshowcase.org/donate/ … … 13 13 Caches multiple users automatically on post and comment pages to radically reduce mysql queries for plugins that get_userdata. 14 14 If you find that your query count has become excessive (25+) on your post or comment pages, this plugin may help. 15 Tested up to WordPress 2.5(might be compatibile with newer versions). Feedback requested.15 Tested up to WordPress (might be compatibile with newer versions). Feedback requested. 16 16 17 17 == Installation == … … 28 28 * http://bbshowcase.org/donate/ 29 29 30 == History==30 == == 31 31 32 32 = Version 0.0.1 (2009-01-02) = … … 34 34 * first public alpha release 35 35 36 37 38 39 -
wp-cache-users/trunk/wp-cache-users.php
r84127 r619740 6 6 Author: _ck_ 7 7 Author URI: http://bbShowcase.org 8 Version: 0.0. 18 Version: 0.0. 9 9 */ 10 10 … … 13 13 14 14 function wp_cache_users_from_posts($posts) { 15 if (!empty($posts)) {16 foreach ($posts as $post) {if (!empty($post->post_author)) {$ids[$post->post_author]=$post->post_author;}}17 wp_cache_users($ids);18 }19 return $posts;15 if (!empty($posts)) { 16 foreach ($posts as $post) {if (!empty($post->post_author)) {$ids[$post->post_author]=$post->post_author;}} 17 wp_cache_users($ids); 18 } 19 return $posts; 20 20 } 21 21 22 22 function wp_cache_users_from_comments($comments) { 23 if (!empty($comments)) {24 foreach ($comments as $comment) {if (!empty($comment->user_id)) {$ids[$comment->user_id]=$comment->user_id;}}25 wp_cache_users($ids);26 }27 return $comments;23 if (!empty($comments)) { 24 foreach ($comments as $comment) {if (!empty($comment->user_id)) {$ids[$comment->user_id]=$comment->user_id;}} 25 wp_cache_users($ids); 26 } 27 return $comments; 28 28 } 29 29 30 function wp_cache_users($ids) { // does the heavy lifting, load and cache as many users as you want 31 if (empty($ids) || !is_array($ids)) {return;} 32 global $wpdb,$wp_object_cache; 33 $ids=array_unique($ids); $ids = array_map('intval', $ids); 34 foreach ($ids as $key=>$id) {if (isset($wp_object_cache->cache['users'][$id])) {unset($ids[$key]);}} // don't load what's already there 35 if (empty($ids)) {return;} 36 $sids = join(',', $ids); 37 $users = $wpdb->get_results("SELECT * FROM $wpdb->users WHERE ID IN ($sids)"); 38 if (empty($users)) {return;} 39 foreach ($users as $user) {$temp[$user->ID]=$user;} $users=$temp; unset($temp); // make key user_id 40 $metas = $wpdb->get_results("SELECT user_id,meta_key, meta_value FROM $wpdb->usermeta WHERE user_id IN ($sids)"); 41 if (!empty($metas)) { 42 foreach ( $metas as $meta ) { 43 $value = maybe_unserialize($meta->meta_value); 44 $users[$meta->user_id]->{$meta->meta_key} = $value; 45 } 30 function wp_cache_users($ids) { 31 if (empty($ids)) {return;} 32 global $wpdb, $wp_object_cache; $limit=0; $in=''; 33 foreach ($ids as $key=>$id) {if (!isset($wp_object_cache->cache['users'][$id])) {$limit++; $in.=$id.',';}} 34 if ($limit) { 35 $in=substr($in,0,-1); 36 $users=$wpdb->get_results("SELECT * FROM $wpdb->users WHERE ID IN ($in) LIMIT $limit"); 37 if (!empty($users)) { _fill_many_users($users); } 38 } 46 39 } 47 foreach ($users as $user) {48 $level = $wpdb->prefix . 'user_level';49 if ( isset( $user->{$level} ) ) { $user->user_level = $user->{$level};}50 // For backwards compat.51 if ( isset($user->first_name) ) {$user->user_firstname = $user->first_name;}52 if ( isset($user->last_name) ) {$user->user_lastname = $user->last_name;}53 if ( isset($user->description) ) {$user->user_description = $user->description;}54 wp_cache_add($user->ID, $user, 'users');55 wp_cache_add($user->user_login, $user->ID, 'userlogins');56 wp_cache_add($user->user_email, $user->ID, 'useremail');57 }58 }59 ?>
Note: See TracChangeset
for help on using the changeset viewer.