From 85723b8944eac05f31b38e00eff7cd312745868c Mon Sep 17 00:00:00 2001
From: ryan
Date: Mon, 7 Nov 2005 21:56:03 +0000
Subject: [PATCH] Object caching, round one.
git-svn-id: http://svn.automattic.com/wordpress/trunk@3011 1a063a9b-81f0-0310-95a4-ce76da25c4cd
---
wp-admin/admin-db.php | 9 +
wp-includes/cache.php | 296 ++++++++++++++++++++
wp-includes/classes.php | 23 +-
wp-includes/functions-post.php | 6 +-
wp-includes/functions.php | 185 ++++++------
wp-includes/pluggable-functions.php | 28 +-
wp-includes/registration-functions.php | 14 +-
wp-includes/template-functions-category.php | 12 +-
wp-settings.php | 20 +-
9 files changed, 454 insertions(+), 139 deletions(-)
create mode 100644 wp-includes/cache.php
diff --git a/wp-admin/admin-db.php b/wp-admin/admin-db.php
index a7a1f9b165..eb4f77864f 100644
--- a/wp-admin/admin-db.php
+++ b/wp-admin/admin-db.php
@@ -118,9 +118,12 @@ function wp_insert_category($catarr) {
$wpdb->query( "UPDATE $wpdb->categories SET category_nicename = '$category_nicename' WHERE cat_ID = '$cat_ID'" );
}
+ wp_cache_set($cat_ID, get_category($cat_ID), 'category');
+
if ($update) {
do_action('edit_category', $cat_ID);
} else {
+ wp_cache_delete('all_category_ids', 'category');
do_action('create_category', $cat_ID);
do_action('add_category', $cat_ID);
}
@@ -167,6 +170,9 @@ function wp_delete_category($cat_ID) {
// TODO: Only set categories to general if they're not in another category already
$wpdb->query("UPDATE $wpdb->post2cat SET category_id='1' WHERE category_id='$cat_ID'");
+ wp_cache_delete($cat_ID, 'category');
+ wp_cache_delete('all_category_ids', 'category');
+
do_action('delete_category', $cat_ID);
return 1;
@@ -233,6 +239,9 @@ function wp_delete_user($id, $reassign = 'novalue') {
// FINALLY, delete user
$wpdb->query("DELETE FROM $wpdb->users WHERE ID = $id");
+ wp_cache_delete($id, 'users');
+ // TODO: Need to delete username keyed cache object.
+
do_action('delete_user', $id);
return true;
diff --git a/wp-includes/cache.php b/wp-includes/cache.php
new file mode 100644
index 0000000000..c152880219
--- /dev/null
+++ b/wp-includes/cache.php
@@ -0,0 +1,296 @@
+add($key, $data, $flag, $expire);
+}
+
+function wp_cache_close() {
+ global $wp_object_cache;
+
+ return $wp_object_cache->save();
+}
+
+function wp_cache_delete($id, $flag = '') {
+ global $wp_object_cache;
+
+ return $wp_object_cache->delete($id, $flag);
+}
+
+function wp_cache_flush() {
+ global $wp_object_cache;
+}
+
+function wp_cache_get($id, $flag = '') {
+ global $wp_object_cache;
+
+ return $wp_object_cache->get($id, $flag);
+}
+
+function wp_cache_init() {
+ global $wp_object_cache;
+
+ $wp_object_cache = new WP_Object_Cache();
+}
+
+function wp_cache_replace($key, $data, $flag = '', $expire = 0) {
+ global $wp_object_cache;
+
+ return $wp_object_cache->replace($key, $data, $flag, $expire);
+}
+
+function wp_cache_set($key, $data, $flag = '', $expire = 0) {
+ global $wp_object_cache;
+
+ return $wp_object_cache->set($key, $data, $flag, $expire);
+}
+
+class WP_Object_Cache {
+ var $cache_dir;
+ var $cache_enabled = false;
+ var $use_flock = false;
+ var $flock_filename = 'wp_object_cache.lock';
+ var $sem_id = 5454;
+ var $mutex;
+ var $cache = array ();
+ var $dirty_objects = array ();
+ var $global_groups = array('users', 'usermeta');
+ var $blog_id;
+ var $cold_cache_hits = 0;
+ var $warm_cache_hits = 0;
+ var $cache_misses = 0;
+
+ function add($id, $data, $group = 'default', $expire = '') {
+ if ( empty($group) )
+ $group = 'default';
+
+ if (isset ($this->cache[$group][$id]))
+ return false;
+
+ return $this->set($id, $data, $group, $expire);
+ }
+
+ function delete($id, $group = 'default') {
+ if ( empty($group) )
+ $group = 'default';
+
+ if (!isset ($this->cache[$group][$id]))
+ return false;
+
+ unset ($this->cache[$group][$id]);
+ $this->dirty_objects[$group][] = $id;
+ return true;
+ }
+
+ function get($id, $group = 'default') {
+ if ( empty($group) )
+ $group = 'default';
+
+ if (isset ($this->cache[$group][$id])) {
+ $this->warm_cache_hits += 1;
+ return $this->cache[$group][$id];
+ }
+
+ // If caching is not enabled, we have to fall back to pulling from the DB.
+ if (!$this->cache_enabled) {
+ if (!isset ($this->cache[$group]))
+ $this->load_group_from_db($group);
+
+ if (isset ($this->cache[$group][$id])) {
+ $this->cold_cache_hits += 1;
+ return $this->cache[$group][$id];
+ }
+
+ $this->cache_misses += 1;
+ return false;
+ }
+
+ $cache_file = $this->cache_dir . $this->get_group_dir($group) . "/" . md5($id);
+ if (!file_exists($cache_file)) {
+ $this->cache_misses += 1;
+ return false;
+ }
+ $this->cache[$group][$id] = unserialize(@ file_get_contents($cache_file));
+ if ( false === $this->cache[$group][$id])
+ $this->cache[$group][$id] = '';
+ $this->cold_cache_hits += 1;
+ return $this->cache[$group][$id];
+ }
+
+ function get_group_dir($group) {
+ if ( false !== array_search($group, $this->global_groups) )
+ return $group;
+
+ return "{$this->blog_id}/$group";
+ }
+
+ function load_group_from_db($group) {
+ global $wpdb;
+
+ if ('category' == $group) {
+ $this->cache['category'] = array ();
+ if ($dogs = $wpdb->get_results("SELECT * FROM $wpdb->categories")) {
+ foreach ($dogs as $catt)
+ $this->cache['category'][$catt->cat_ID] = $catt;
+
+ foreach ($this->cache['category'] as $catt) {
+ $curcat = $catt->cat_ID;
+ $fullpath = '/'.$this->cache['category'][$catt->cat_ID]->category_nicename;
+ while ($this->cache['category'][$curcat]->category_parent != 0) {
+ $curcat = $this->cache['category'][$curcat]->category_parent;
+ $fullpath = '/'.$this->cache['category'][$curcat]->category_nicename.$fullpath;
+ }
+ $this->cache['category'][$catt->cat_ID]->fullpath = $fullpath;
+ }
+ }
+ } else if ( 'options' == $group ) {
+ $wpdb->hide_errors();
+ if ( !$options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'") ) {
+ $options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options");
+ }
+ $wpdb->show_errors();
+
+ foreach ($options as $option) {
+ $this->cache['options'][$option->option_name] = $option->option_value;
+ }
+ }
+ }
+
+ function make_group_dir($group, $perms) {
+ $group_dir = $this->get_group_dir($group);
+ $make_dir = '';
+ foreach ( split('/', $group_dir) as $subdir) {
+ $make_dir .= "$subdir/";
+ if (!file_exists($this->cache_dir . $make_dir)) {
+ if (!mkdir($this->cache_dir . $make_dir))
+ break;
+ @ chmod($this->cache_dir . $make_dir, $perms);
+ }
+ }
+
+ return $this->cache_dir . "$group_dir/";
+ }
+
+ function replace($id, $data, $group = 'default', $expire = '') {
+ if ( empty($group) )
+ $group = 'default';
+
+ if (!isset ($this->cache[$group][$id]))
+ return false;
+
+ return $this->set($id, $data, $group, $expire);
+ }
+
+ function set($id, $data, $group = 'default', $expire = '') {
+ if ( empty($group) )
+ $group = 'default';
+
+ $this->cache[$group][$id] = $data;
+ $this->dirty_objects[$group][] = $id;
+ return true;
+ }
+
+ function save() {
+ //$this->stats();
+
+ if (!$this->cache_enabled)
+ return;
+
+ if (empty ($this->dirty_objects))
+ return;
+
+ // Give the new dirs the same perms as wp-content.
+ $stat = stat(ABSPATH.'wp-content');
+ $dir_perms = $stat['mode'] & 0000777; // Get the permission bits.
+
+ // Make the base cache dir.
+ if (!file_exists($this->cache_dir)) {
+ if (!mkdir($this->cache_dir))
+ return;
+ @ chmod($this->cache_dir, $dir_perms);
+ }
+
+ // Acquire a write lock. Semaphore preferred. Fallback to flock.
+ if (function_exists('sem_get')) {
+ $this->use_flock = false;
+ $mutex = sem_get($this->sem_id, 1, 0644 | IPC_CREAT, 1);
+ sem_acquire($mutex);
+ } else {
+ $this->use_flock = true;
+ $mutex = fopen($this->cache_dir.$this->flock_filename, 'w');
+ flock($mutex, LOCK_EX);
+ }
+
+ // Loop over dirty objects and save them.
+ foreach ($this->dirty_objects as $group => $ids) {
+ $group_dir = $this->make_group_dir($group, $dir_perms);
+
+ $ids = array_unique($ids);
+ foreach ($ids as $id) {
+ // TODO: If the id is no longer in the cache, it was deleted and
+ // the file should be removed.
+ $cache_file = $group_dir . md5($id);
+ $temp_file = tempnam($group_dir, 'tmp');
+ $serial = serialize($this->cache[$group][$id]);
+ $fd = fopen($temp_file, 'w');
+ fputs($fd, $serial);
+ fclose($fd);
+ rename($temp_file, $cache_file);
+ }
+ }
+
+ // Release write lock.
+ if ($this->use_flock)
+ flock($mutex, LOCK_UN);
+ else
+ sem_release($mutex);
+ }
+
+ function stats() {
+ echo "";
+ echo "Cold Cache Hits: {$this->cold_cache_hits}
";
+ echo "Warm Cache Hits: {$this->warm_cache_hits}
";
+ echo "Cache Misses: {$this->cache_misses}
";
+ echo "
";
+
+ foreach ($this->cache as $group => $cache) {
+ echo "";
+ echo "Group: $group
";
+ echo "Cache:";
+ echo "
";
+ print_r($cache);
+ echo "
";
+ if ( isset($this->dirty_objects[$group]) ) {
+ echo "Dirty Objects:";
+ echo "";
+ print_r(array_unique($this->dirty_objects[$group]));
+ echo "
";
+ echo "
";
+ }
+ }
+ }
+
+ function WP_Object_Cache() {
+ global $blog_id;
+
+ if ( defined('DISABLE_CACHE') )
+ return;
+
+ if ( defined('CACHE_PATH') )
+ $this->cache_dir = CACHE_PATH;
+ else
+ $this->cache_dir = ABSPATH.'wp-content/cache/';
+
+ if ( is_dir($this->cache_dir) ) {
+ if ( is_writable($this->cache_dir) )
+ $this->cache_enabled = true;
+ } else if (is_writable(ABSPATH.'wp-content')) {
+ $this->cache_enabled = true;
+ }
+
+ $this->blog_id = md5($blog_id);
+ }
+}
+?>
+
+
diff --git a/wp-includes/classes.php b/wp-includes/classes.php
index e19734cb9f..c57263a851 100644
--- a/wp-includes/classes.php
+++ b/wp-includes/classes.php
@@ -443,21 +443,16 @@ class WP_Query {
foreach($cat_paths as $pathdir)
$cat_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir);
- $q['cat'] = array_reduce(
- $cache_categories,
- create_function('$a, $b', 'return ($b->fullpath == "'.$cat_path.'") ? $b->cat_ID : $a;'),
- 0
- );
-
- // If full path not found, look for last dir as category ignoring parent
- if($q['cat'] == 0) {
- $q['cat'] = array_reduce(
- $cache_categories,
- create_function('$a, $b', 'return ($b->category_nicename == "'.$q['category_name'].'") ? $b->cat_ID : $a;'),
- 0
- );
+ $all_cat_ids = get_all_category_ids();
+ $q['cat'] = 0;
+ foreach ( $all_cat_ids as $cat_id ) {
+ $cat = get_category($cat_id);
+ if ( $cat->fullpath == $cat_path ) {
+ $q['cat'] = $cat_id;
+ break;
+ }
}
-
+
$tables = ", $wpdb->post2cat, $wpdb->categories";
$join = " LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) LEFT JOIN $wpdb->categories ON ($wpdb->post2cat.category_id = $wpdb->categories.cat_ID) ";
$whichcat = " AND (category_id = '" . $q['cat'] . "'";
diff --git a/wp-includes/functions-post.php b/wp-includes/functions-post.php
index e90ddedcc7..9f368eeb42 100644
--- a/wp-includes/functions-post.php
+++ b/wp-includes/functions-post.php
@@ -137,10 +137,12 @@ function wp_insert_post($postarr = array()) {
wp_set_post_cats('', $post_ID, $post_category);
- if ( 'static' == $post_status )
+ if ( 'static' == $post_status ) {
clean_page_cache($post_ID);
- else
+ wp_cache_delete($post_ID, 'pages');
+ } else {
clean_post_cache($post_ID);
+ }
// Set GUID
if ( ! $update )
diff --git a/wp-includes/functions.php b/wp-includes/functions.php
index 686dfbd411..8fac88d518 100644
--- a/wp-includes/functions.php
+++ b/wp-includes/functions.php
@@ -260,38 +260,29 @@ function url_to_postid($url) {
/* Options functions */
function get_settings($setting) {
- global $wpdb, $cache_settings, $cache_nonexistantoptions;
+ global $wpdb;
if ( strstr($_SERVER['REQUEST_URI'], 'wp-admin/install.php') || defined('WP_INSTALLING') )
return false;
- if ( empty($cache_settings) )
- $cache_settings = get_alloptions();
+ $value = wp_cache_get($setting, 'options');
- if ( empty($cache_nonexistantoptions) )
- $cache_nonexistantoptions = array();
+ if ( false === $value ) {
+ $value = $wpdb->get_var("SELECT option_value FROM $wpdb->options WHERE option_name = '$setting'");
+ wp_cache_add($setting, $value, 'options');
+ }
- if ( 'home' == $setting && '' == $cache_settings->home )
- return apply_filters('option_' . $setting, $cache_settings->siteurl);
+ // If home is not set use siteurl.
+ if ( 'home' == $setting && '' == $value )
+ return get_settings('siteurl');
- if ( isset($cache_settings->$setting) ) :
- return apply_filters('option_' . $setting, $cache_settings->$setting);
- else :
- // for these cases when we're asking for an unknown option
- if ( isset($cache_nonexistantoptions[$setting]) )
- return false;
+ if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting )
+ $value = preg_replace('|/+$|', '', $value);
- $option = $wpdb->get_var("SELECT option_value FROM $wpdb->options WHERE option_name = '$setting'");
-
- if (!$option) :
- $cache_nonexistantoptions[$setting] = true;
- return false;
- endif;
-
- @ $kellogs = unserialize($option);
- if ( $kellogs !== FALSE )
- return apply_filters('option_' . $setting, $kellogs);
- else return apply_filters('option_' . $setting, $option);
- endif;
+ @ $kellogs = unserialize($value);
+ if ( $kellogs !== FALSE )
+ return apply_filters('option_' . $setting, $kellogs);
+ else
+ return apply_filters('option_' . $setting, $value);
}
function get_option($option) {
@@ -338,7 +329,7 @@ function get_alloptions() {
}
function update_option($option_name, $newvalue) {
- global $wpdb, $cache_settings;
+ global $wpdb;
if ( is_string($newvalue) )
$newvalue = trim($newvalue);
@@ -354,10 +345,11 @@ function update_option($option_name, $newvalue) {
if ( !$wpdb->get_var("SELECT option_name FROM $wpdb->options WHERE option_name = '$option_name'") )
add_option($option_name);
+ wp_cache_set($option_name, $newvalue, 'options');
+
$newvalue = $wpdb->escape($newvalue);
- $option_name = $wpdb->escape( $option_name );
+ $option_name = $wpdb->escape($option_name);
$wpdb->query("UPDATE $wpdb->options SET option_value = '$newvalue' WHERE option_name = '$option_name'");
- $cache_settings = get_alloptions(); // Re cache settings
return true;
}
@@ -370,27 +362,23 @@ function update_user_option( $user_id, $option_name, $newvalue, $global = false
// thx Alex Stapleton, http://alex.vort-x.net/blog/
function add_option($name, $value = '', $description = '', $autoload = 'yes') {
- global $wpdb, $cache_settings;
+ global $wpdb;
// Make sure the option doesn't already exist
- if ( isset($cache_settings->$name) )
+ if ( false !== get_option($name, 'options') )
return;
$original = $value;
if ( is_array($value) || is_object($value) )
$value = serialize($value);
- if ( !$wpdb->get_var("SELECT option_name FROM $wpdb->options WHERE option_name = '$name'") ) {
- $name = $wpdb->escape($name);
- $value = $wpdb->escape($value);
- $description = $wpdb->escape($description);
- $wpdb->query("INSERT INTO $wpdb->options (option_name, option_value, option_description, autoload) VALUES ('$name', '$value', '$description', '$autoload')");
+ wp_cache_add($name, $value, 'options');
+
+ $name = $wpdb->escape($name);
+ $value = $wpdb->escape($value);
+ $description = $wpdb->escape($description);
+ $wpdb->query("INSERT INTO $wpdb->options (option_name, option_value, option_description, autoload) VALUES ('$name', '$value', '$description', '$autoload')");
- if ( $wpdb->insert_id ) {
- global $cache_settings;
- $cache_settings->{$name} = $original;
- }
- }
return;
}
@@ -400,6 +388,7 @@ function delete_option($name) {
$option_id = $wpdb->get_var("SELECT option_id FROM $wpdb->options WHERE option_name = '$name'");
if ( !$option_id ) return false;
$wpdb->query("DELETE FROM $wpdb->options WHERE option_name = '$name'");
+ wp_cache_delete($name, 'options');
return true;
}
@@ -588,26 +577,28 @@ function &get_post(&$post, $output = OBJECT) {
// Retrieves page data given a page ID or page object.
// Handles page caching.
function &get_page(&$page, $output = OBJECT) {
- global $page_cache, $wpdb;
+ global $wpdb;
if ( empty($page) ) {
- if ( isset($GLOBALS['page']) )
+ if ( isset($GLOBALS['page']) ) {
$_page = & $GLOBALS['page'];
- else
+ wp_cache_add($_page->ID, $_page, 'pages');
+ } else {
$_page = null;
+ }
} elseif ( is_object($page) ) {
- if ( !isset($page_cache[$page->ID]) )
- $page_cache[$page->ID] = &$page;
- $_page = & $page_cache[$page->ID];
+ wp_cache_add($page->ID, $page, 'pages');
+ $_page = $page;
} else {
- if ( isset($GLOBALS['page']) && ($page == $GLOBALS['page']->ID) )
+ if ( isset($GLOBALS['page']) && ($page == $GLOBALS['page']->ID) ) {
$_page = & $GLOBALS['page'];
- elseif ( isset($page_cache[$page]) )
- $_page = & $page_cache[$page];
- else {
+ wp_cache_add($_page->ID, $_page, 'pages');
+ } elseif ( $_page = wp_cache_get($page, 'pages') ) {
+ // Got it.
+ } else {
$query = "SELECT * FROM $wpdb->posts WHERE ID= '$page'";
- $page_cache[$page] = & $wpdb->get_row($query);
- $_page = & $page_cache[$page];
+ $_page = & $wpdb->get_row($query);
+ wp_cache_add($_page->ID, $_page, 'pages');
}
}
@@ -622,30 +613,43 @@ function &get_page(&$page, $output = OBJECT) {
}
}
+function set_category_path($cat) {
+ $cat->fullpath = '/' . $cat->category_nicename;
+ $path = $cat->fullpath;
+ $curcat = $cat;
+ while ($curcat->category_parent != 0) {
+ $curcat = get_category($curcat->category_parent);
+ $path = '/' . $curcat->category_nicename . $path;
+ }
+
+ $cat->fullpath = $path;
+
+ return $cat;
+}
+
// Retrieves category data given a category ID or category object.
// Handles category caching.
function &get_category(&$category, $output = OBJECT) {
- global $cache_categories, $wpdb;
+ global $wpdb;
if ( empty($category) )
return null;
- if ( !isset($cache_categories) )
- update_category_cache();
-
if ( is_object($category) ) {
- if ( !isset($cache_categories[$category->cat_ID]) )
- $cache_categories[$category->cat_ID] = &$category;
- $_category = & $cache_categories[$category->cat_ID];
+ wp_cache_add($category->cat_ID, $category, 'category');
+ $_category = $category;
} else {
- if ( !isset($cache_categories[$category]) ) {
+ if ( ! $_category = wp_cache_get($category, 'category') ) {
$_category = $wpdb->get_row("SELECT * FROM $wpdb->categories WHERE cat_ID = '$category'");
- $cache_categories[$category->cat_ID] = & $_category;
- } else {
- $_category = & $cache_categories[$category];
+ wp_cache_add($category, $_category, 'category');
}
}
+ if ( !isset($_category->fullpath) ) {
+ $_category = set_category_path($_category);
+ wp_cache_replace($_category->cat_ID, $_category, 'category');
+ }
+
if ( $output == OBJECT ) {
return $_category;
} elseif ( $output == ARRAY_A ) {
@@ -694,6 +698,17 @@ function get_catname($cat_ID) {
return $category->cat_name;
}
+function get_all_category_ids() {
+ global $wpdb;
+
+ if ( ! $cat_ids = wp_cache_get('all_category_ids', 'category') ) {
+ $cat_ids = $wpdb->get_col("SELECT cat_ID FROM $wpdb->categories");
+ wp_cache_add('all_category_ids', $cat_ids, 'category');
+ }
+
+ return $cat_ids;
+}
+
function gzip_compression() {
if ( strstr($_SERVER['PHP_SELF'], 'wp-admin') ) return false;
if ( !get_settings('gzipcompression') ) return false;
@@ -1231,6 +1246,7 @@ function update_page_cache(&$pages) {
for ($i = 0; $i < count($pages); $i++) {
$page_cache[$pages[$i]->ID] = &$pages[$i];
+ wp_cache_add($pages[$i]->ID, $pages[$i], 'pages');
}
}
@@ -1243,7 +1259,7 @@ function clean_page_cache($id) {
}
function update_post_category_cache($post_ids) {
- global $wpdb, $category_cache, $cache_categories;
+ global $wpdb, $category_cache;
if ( empty($post_ids) )
return;
@@ -1255,14 +1271,11 @@ function update_post_category_cache($post_ids) {
post_id, cat_ID FROM $wpdb->categories, $wpdb->post2cat
WHERE category_id = cat_ID AND post_id IN ($post_ids)");
- if ( !isset($cache_categories) )
- update_category_cache();
-
- if ( !empty($dogs) ) {
- foreach ($dogs as $catt) {
- $category_cache[$catt->post_id][$catt->cat_ID] = &$cache_categories[$catt->cat_ID];
- }
- }
+ if ( empty($dogs) )
+ return;
+
+ foreach ($dogs as $catt)
+ $category_cache[$catt->post_id][$catt->cat_ID] = &get_category($catt->cat_ID);
}
function update_post_caches(&$posts) {
@@ -1322,30 +1335,7 @@ function update_post_caches(&$posts) {
}
function update_category_cache() {
- global $cache_categories, $wpdb;
- if ( $dogs = $wpdb->get_results("SELECT * FROM $wpdb->categories") ):
- foreach ($dogs as $catt)
- $cache_categories[$catt->cat_ID] = $catt;
-
- foreach ($cache_categories as $catt) {
- $curcat = $catt->cat_ID;
- $cache_categories[$catt->cat_ID]->fullpath = '/' . $cache_categories[$catt->cat_ID]->category_nicename;
- while ($cache_categories[$curcat]->category_parent != 0) {
- $curcat = $cache_categories[$curcat]->category_parent;
- $cache_categories[$catt->cat_ID]->fullpath = '/' . $cache_categories[$curcat]->category_nicename . $cache_categories[$catt->cat_ID]->fullpath;
- }
- }
- return true;
- else :
- return false;
- endif;
-}
-
-function clean_user_cache($id) {
- global $cache_userdata;
-
- if ( isset( $cache_userdata[$id] ) )
- unset( $cache_userdata[$id] );
+ return true;
}
function wp_head() {
@@ -2123,6 +2113,9 @@ function update_usermeta( $user_id, $meta_key, $meta_value ) {
}
if ( $cur->meta_value != $meta_value )
$wpdb->query("UPDATE $wpdb->usermeta SET meta_value = '$meta_value' WHERE user_id = '$user_id' AND meta_key = '$meta_key'");
+
+ wp_cache_delete($user_id, 'users');
+ // FIXME: Need to delete username keyed cache object.
}
function register_activation_hook($file, $function) {
diff --git a/wp-includes/pluggable-functions.php b/wp-includes/pluggable-functions.php
index 7c81359b82..fb1d91ed09 100644
--- a/wp-includes/pluggable-functions.php
+++ b/wp-includes/pluggable-functions.php
@@ -27,16 +27,18 @@ endif;
if ( !function_exists('get_userdata') ) :
function get_userdata( $user_id ) {
- global $wpdb, $cache_userdata;
+ global $wpdb;
$user_id = (int) $user_id;
if ( $user_id == 0 )
return false;
- if ( isset( $cache_userdata[$user_id] ) )
- return $cache_userdata[$user_id];
+ $user = wp_cache_get($user_id, 'users');
+
+ if ( $user )
+ return $user;
if ( !$user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE ID = '$user_id'") )
- return $cache_userdata[$user_id] = false;
+ return false;
$metavalues = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE user_id = '$user_id'");
@@ -51,10 +53,10 @@ function get_userdata( $user_id ) {
$user->user_level = $meta->meta_value;
}
- $cache_userdata[$user_id] = $user;
- $cache_userdata[$cache_userdata[$user_id]->user_login] =& $cache_userdata[$user_id];
-
- return $cache_userdata[$user_id];
+ wp_cache_add($user_id, $user, 'users');
+ wp_cache_add($user->user_login, $user, 'users');
+
+ return $user;
}
endif;
@@ -95,9 +97,10 @@ function get_userdatabylogin($user_login) {
if ( empty( $user_login ) )
return false;
-
- if ( isset( $cache_userdata[$user_login] ) )
- return $cache_userdata[$user_login];
+
+ $userdata = wp_cache_get($user_login, 'users');
+ if ( $userdata )
+ return $userdata;
if ( !$user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE user_login = '$user_login'") )
return $cache_userdata[$user_login] = false;
@@ -147,7 +150,8 @@ function wp_login($username, $password, $already_md5 = false) {
return false;
}
- $login = $wpdb->get_row("SELECT ID, user_login, user_pass FROM $wpdb->users WHERE user_login = '$username'");
+ $login = get_userdatabylogin($username);
+ //$login = $wpdb->get_row("SELECT ID, user_login, user_pass FROM $wpdb->users WHERE user_login = '$username'");
if (!$login) {
$error = __('Error: Wrong username.');
diff --git a/wp-includes/registration-functions.php b/wp-includes/registration-functions.php
index 35e2cb4b68..bafce8590e 100644
--- a/wp-includes/registration-functions.php
+++ b/wp-includes/registration-functions.php
@@ -3,9 +3,11 @@
function username_exists( $username ) {
global $wpdb;
$username = sanitize_user( $username );
- $query = "SELECT user_login FROM $wpdb->users WHERE user_login = '$username'";
- $query = apply_filters('username_exists', $query);
- return $wpdb->get_var( $query );
+ $user = get_userdatabylogin($username);
+ if ( $user )
+ return $user->user_login;
+
+ return null;
}
function wp_insert_user($userdata) {
@@ -49,9 +51,6 @@ function wp_insert_user($userdata) {
$user_id = $wpdb->insert_id;
}
- clean_user_cache($user_id);
- clean_user_cache($user_login);
-
update_usermeta( $user_id, 'first_name', $first_name);
update_usermeta( $user_id, 'last_name', $last_name);
update_usermeta( $user_id, 'nickname', $nickname );
@@ -69,6 +68,9 @@ function wp_insert_user($userdata) {
$user = new WP_User($user_id);
$user->set_role(get_settings('default_role'));
}
+
+ wp_cache_delete($user_id, 'users');
+ wp_cache_delete($user_login, 'users');
if ( $update )
do_action('profile_update', $user_id);
diff --git a/wp-includes/template-functions-category.php b/wp-includes/template-functions-category.php
index 8193e3e9a8..6af7e0cbc0 100644
--- a/wp-includes/template-functions-category.php
+++ b/wp-includes/template-functions-category.php
@@ -126,14 +126,12 @@ function get_category_parents($id, $link = FALSE, $separator = '/', $nicename =
}
function get_category_children($id, $before = '/', $after = '') {
- global $cache_categories;
+ $cat_ids = get_all_category_ids();
+ foreach ( $cat_ids as $cat_id ) {
+ if ( $cat_id == $id)
+ continue;
- if ( !isset($cache_categories) )
- update_category_cache();
-
- $c_cache = $cache_categories; // Can't do recursive foreach on a global, have to make a copy
- $chain = '';
- foreach ( $c_cache as $category ) {
+ $category = get_category($cat_id);
if ( $category->category_parent == $id ) {
$chain .= $before.$category->cat_ID.$after;
$chain .= get_category_children($category->cat_ID, $before, $after);
diff --git a/wp-settings.php b/wp-settings.php
index df1f3d918a..6d9c4a8463 100644
--- a/wp-settings.php
+++ b/wp-settings.php
@@ -21,6 +21,9 @@ unregister_GLOBALS();
$HTTP_USER_AGENT = getenv('HTTP_USER_AGENT');
unset( $wp_filter, $cache_userdata, $cache_lastcommentmodified, $cache_lastpostdate, $cache_settings, $category_cache, $cache_categories );
+if ( ! isset($blog_id) )
+ $blog_id = 1;
+
// Fix for IIS, which doesn't set REQUEST_URI
if ( empty( $_SERVER['REQUEST_URI'] ) ) {
$_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME']; // Does this work under CGI?
@@ -74,7 +77,7 @@ if ( defined('CUSTOM_USER_TABLE') )
$wpdb->users = CUSTOM_USER_TABLE;
if ( defined('CUSTOM_USER_META_TABLE') )
$wpdb->usermeta = CUSTOM_USER_META_TABLE;
-
+
// We're going to need to keep this around for a few months even though we're not using it internally
$tableposts = $wpdb->posts;
@@ -87,6 +90,17 @@ $tablelinkcategories = $wpdb->linkcategories;
$tableoptions = $wpdb->options;
$tablepostmeta = $wpdb->postmeta;
+if ( file_exists(ABSPATH . 'wp-content/object-cache.php') )
+ require (ABSPATH . 'wp-content/object-cache.php');
+else
+ require (ABSPATH . WPINC . '/cache.php');
+
+// For now, disable persistent caching by default. To enable, comment out
+// the following line.
+define('DISABLE_CACHE', true);
+
+wp_cache_init();
+
$wp_filters = array();
require (ABSPATH . WPINC . '/functions.php');
@@ -94,7 +108,8 @@ require (ABSPATH . WPINC . '/default-filters.php');
require_once (ABSPATH . WPINC . '/wp-l10n.php');
$wpdb->hide_errors();
-if ( !update_category_cache() && (!strstr($_SERVER['PHP_SELF'], 'install.php') && !defined('WP_INSTALLING')) ) {
+$db_check = $wpdb->get_var("SELECT option_value FROM $wpdb->options WHERE option_name = 'siteurl'");
+if ( !$db_check && (!strstr($_SERVER['PHP_SELF'], 'install.php') && !defined('WP_INSTALLING')) ) {
if ( strstr($_SERVER['PHP_SELF'], 'wp-admin') )
$link = 'install.php';
else
@@ -194,6 +209,7 @@ if ( file_exists(TEMPLATEPATH . "/functions.php") )
include(TEMPLATEPATH . "/functions.php");
function shutdown_action_hook() {
+ wp_cache_close();
do_action('shutdown');
}
register_shutdown_function('shutdown_action_hook');