2005-11-07 16:56:03 -05:00
|
|
|
<?php
|
|
|
|
function wp_cache_add($key, $data, $flag = '', $expire = 0) {
|
|
|
|
global $wp_object_cache;
|
|
|
|
|
|
|
|
return $wp_object_cache->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;
|
2005-11-14 17:32:03 -05:00
|
|
|
|
2005-11-14 17:10:28 -05:00
|
|
|
return $wp_object_cache->flush();
|
2005-11-07 16:56:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2005-11-08 21:38:17 -05:00
|
|
|
define('CACHE_SERIAL_HEADER', "<?php\n//");
|
|
|
|
define('CACHE_SERIAL_FOOTER', "\n?".">");
|
|
|
|
|
2005-11-07 16:56:03 -05:00
|
|
|
class WP_Object_Cache {
|
|
|
|
var $cache_dir;
|
|
|
|
var $cache_enabled = false;
|
2005-12-31 17:34:58 -05:00
|
|
|
var $expiration_time = 900;
|
2005-11-07 16:56:03 -05:00
|
|
|
var $flock_filename = 'wp_object_cache.lock';
|
|
|
|
var $cache = array ();
|
|
|
|
var $dirty_objects = array ();
|
2005-11-14 17:32:03 -05:00
|
|
|
var $non_existant_objects = array ();
|
2005-12-19 14:14:22 -05:00
|
|
|
var $global_groups = array ('users', 'userlogins', 'usermeta');
|
2005-11-07 16:56:03 -05:00
|
|
|
var $blog_id;
|
|
|
|
var $cold_cache_hits = 0;
|
|
|
|
var $warm_cache_hits = 0;
|
|
|
|
var $cache_misses = 0;
|
|
|
|
|
|
|
|
function add($id, $data, $group = 'default', $expire = '') {
|
2005-11-14 17:32:03 -05:00
|
|
|
if (empty ($group))
|
2005-11-07 16:56:03 -05:00
|
|
|
$group = 'default';
|
|
|
|
|
2005-11-14 17:32:03 -05:00
|
|
|
if (false !== $this->get($id, $group, false))
|
2005-11-07 16:56:03 -05:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return $this->set($id, $data, $group, $expire);
|
|
|
|
}
|
|
|
|
|
2005-11-14 16:29:30 -05:00
|
|
|
function delete($id, $group = 'default', $force = false) {
|
2005-11-14 17:32:03 -05:00
|
|
|
if (empty ($group))
|
2005-11-07 16:56:03 -05:00
|
|
|
$group = 'default';
|
|
|
|
|
2005-11-14 17:32:03 -05:00
|
|
|
if (!$force && false === $this->get($id, $group, false))
|
2005-11-07 16:56:03 -05:00
|
|
|
return false;
|
|
|
|
|
|
|
|
unset ($this->cache[$group][$id]);
|
2005-11-09 06:10:34 -05:00
|
|
|
$this->non_existant_objects[$group][$id] = true;
|
2005-11-07 16:56:03 -05:00
|
|
|
$this->dirty_objects[$group][] = $id;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-11-14 17:10:28 -05:00
|
|
|
function flush() {
|
2005-11-16 14:01:58 -05:00
|
|
|
if ( !$this->cache_enabled )
|
|
|
|
return;
|
|
|
|
|
2006-01-04 21:56:42 -05:00
|
|
|
$this->rm_cache_dir();
|
2005-11-14 17:32:03 -05:00
|
|
|
$this->cache = array ();
|
|
|
|
$this->dirty_objects = array ();
|
|
|
|
$this->non_existant_objects = array ();
|
|
|
|
return true;
|
2005-11-14 17:10:28 -05:00
|
|
|
}
|
2005-11-14 17:32:03 -05:00
|
|
|
|
2005-11-09 06:10:34 -05:00
|
|
|
function get($id, $group = 'default', $count_hits = true) {
|
2005-11-14 17:32:03 -05:00
|
|
|
if (empty ($group))
|
2005-11-07 16:56:03 -05:00
|
|
|
$group = 'default';
|
|
|
|
|
|
|
|
if (isset ($this->cache[$group][$id])) {
|
2005-11-14 17:32:03 -05:00
|
|
|
if ($count_hits)
|
2005-11-09 06:10:34 -05:00
|
|
|
$this->warm_cache_hits += 1;
|
2005-11-07 16:56:03 -05:00
|
|
|
return $this->cache[$group][$id];
|
|
|
|
}
|
|
|
|
|
2005-11-14 17:32:03 -05:00
|
|
|
if (isset ($this->non_existant_objects[$group][$id]))
|
2005-11-09 06:10:34 -05:00
|
|
|
return false;
|
|
|
|
|
2005-11-07 16:56:03 -05:00
|
|
|
// 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];
|
|
|
|
}
|
|
|
|
|
2005-11-09 06:10:34 -05:00
|
|
|
$this->non_existant_objects[$group][$id] = true;
|
2005-11-07 16:56:03 -05:00
|
|
|
$this->cache_misses += 1;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-11-14 17:32:03 -05:00
|
|
|
$cache_file = $this->cache_dir.$this->get_group_dir($group)."/".md5($id.DB_PASSWORD).'.php';
|
2005-11-07 16:56:03 -05:00
|
|
|
if (!file_exists($cache_file)) {
|
2005-11-09 06:10:34 -05:00
|
|
|
$this->non_existant_objects[$group][$id] = true;
|
2005-11-07 16:56:03 -05:00
|
|
|
$this->cache_misses += 1;
|
|
|
|
return false;
|
|
|
|
}
|
2005-11-09 06:10:34 -05:00
|
|
|
|
|
|
|
// If the object has expired, remove it from the cache and return false to force
|
|
|
|
// a refresh.
|
|
|
|
$now = time();
|
2005-11-14 17:32:03 -05:00
|
|
|
if ((filemtime($cache_file) + $this->expiration_time) <= $now) {
|
2005-11-09 06:10:34 -05:00
|
|
|
$this->cache_misses += 1;
|
2005-11-14 16:29:30 -05:00
|
|
|
$this->delete($id, $group, true);
|
2005-11-09 06:10:34 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-11-08 21:38:17 -05:00
|
|
|
$this->cache[$group][$id] = unserialize(substr(@ file_get_contents($cache_file), strlen(CACHE_SERIAL_HEADER), -strlen(CACHE_SERIAL_FOOTER)));
|
2005-11-14 17:32:03 -05:00
|
|
|
if (false === $this->cache[$group][$id])
|
2005-11-07 16:56:03 -05:00
|
|
|
$this->cache[$group][$id] = '';
|
2005-11-09 06:10:34 -05:00
|
|
|
|
2005-11-07 16:56:03 -05:00
|
|
|
$this->cold_cache_hits += 1;
|
|
|
|
return $this->cache[$group][$id];
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_group_dir($group) {
|
2005-11-14 17:32:03 -05:00
|
|
|
if (false !== array_search($group, $this->global_groups))
|
2005-11-07 16:56:03 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2005-11-14 17:32:03 -05:00
|
|
|
} 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();
|
2005-11-07 16:56:03 -05:00
|
|
|
|
2005-11-17 17:46:18 -05:00
|
|
|
if ( ! $options )
|
|
|
|
return;
|
|
|
|
|
2005-11-14 17:32:03 -05:00
|
|
|
foreach ($options as $option) {
|
|
|
|
$this->cache['options'][$option->option_name] = $option->option_value;
|
|
|
|
}
|
2005-11-07 16:56:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function make_group_dir($group, $perms) {
|
|
|
|
$group_dir = $this->get_group_dir($group);
|
|
|
|
$make_dir = '';
|
2005-11-14 17:32:03 -05:00
|
|
|
foreach (split('/', $group_dir) as $subdir) {
|
2005-11-07 16:56:03 -05:00
|
|
|
$make_dir .= "$subdir/";
|
2005-11-14 17:32:03 -05:00
|
|
|
if (!file_exists($this->cache_dir.$make_dir)) {
|
2005-11-16 18:40:14 -05:00
|
|
|
if (! @ mkdir($this->cache_dir.$make_dir))
|
2005-11-14 17:32:03 -05:00
|
|
|
break;
|
|
|
|
@ chmod($this->cache_dir.$make_dir, $perms);
|
2005-11-07 16:56:03 -05:00
|
|
|
}
|
2005-11-08 17:15:06 -05:00
|
|
|
|
2005-11-14 17:32:03 -05:00
|
|
|
if (!file_exists($this->cache_dir.$make_dir."index.php")) {
|
2006-01-01 21:50:59 -05:00
|
|
|
$file_perms = $perms & 0000666;
|
2005-11-16 18:40:14 -05:00
|
|
|
@ touch($this->cache_dir.$make_dir."index.php");
|
2006-01-01 21:50:59 -05:00
|
|
|
@ chmod($this->cache_dir.$make_dir."index.php", $file_perms);
|
2005-11-08 17:15:06 -05:00
|
|
|
}
|
2005-11-07 16:56:03 -05:00
|
|
|
}
|
2005-11-14 17:32:03 -05:00
|
|
|
|
|
|
|
return $this->cache_dir."$group_dir/";
|
2005-11-07 16:56:03 -05:00
|
|
|
}
|
|
|
|
|
2006-01-04 21:56:42 -05:00
|
|
|
function rm_cache_dir() {
|
|
|
|
$dir = $this->cache_dir;
|
|
|
|
$dir = rtrim($dir, DIRECTORY_SEPARATOR);
|
2006-01-04 23:50:58 -05:00
|
|
|
$top_dir = $dir;
|
2006-01-04 21:56:42 -05:00
|
|
|
$stack = array($dir);
|
|
|
|
|
|
|
|
while (count($stack)) {
|
|
|
|
# Get last directory on stack
|
|
|
|
$dir = end($stack);
|
|
|
|
|
|
|
|
$dh = @ opendir($dir);
|
|
|
|
if (!$dh)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
while (($file = @ readdir($dh)) !== false) {
|
|
|
|
if ($file == '.' or $file == '..')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (@ is_dir($dir . DIRECTORY_SEPARATOR . $file))
|
|
|
|
$stack[] = $dir . DIRECTORY_SEPARATOR . $file;
|
|
|
|
else if (@ is_file($dir . DIRECTORY_SEPARATOR . $file))
|
|
|
|
@ unlink($dir . DIRECTORY_SEPARATOR . $file);
|
2005-11-14 17:32:03 -05:00
|
|
|
}
|
2006-01-04 21:56:42 -05:00
|
|
|
|
|
|
|
if (end($stack) == $dir) {
|
2006-01-04 23:50:58 -05:00
|
|
|
if ( $dir != $top_dir)
|
|
|
|
@ rmdir($dir);
|
2006-01-04 21:56:42 -05:00
|
|
|
array_pop($stack);
|
|
|
|
}
|
|
|
|
}
|
2005-11-14 17:10:28 -05:00
|
|
|
}
|
|
|
|
|
2005-11-07 16:56:03 -05:00
|
|
|
function replace($id, $data, $group = 'default', $expire = '') {
|
2005-11-14 17:32:03 -05:00
|
|
|
if (empty ($group))
|
2005-11-07 16:56:03 -05:00
|
|
|
$group = 'default';
|
|
|
|
|
2005-11-14 17:32:03 -05:00
|
|
|
if (false === $this->get($id, $group, false))
|
2005-11-07 16:56:03 -05:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return $this->set($id, $data, $group, $expire);
|
|
|
|
}
|
|
|
|
|
|
|
|
function set($id, $data, $group = 'default', $expire = '') {
|
2005-11-14 17:32:03 -05:00
|
|
|
if (empty ($group))
|
2005-11-07 16:56:03 -05:00
|
|
|
$group = 'default';
|
|
|
|
|
2005-11-14 17:32:03 -05:00
|
|
|
if (NULL == $data)
|
2005-11-09 06:10:34 -05:00
|
|
|
$data = '';
|
|
|
|
|
2005-11-07 16:56:03 -05:00
|
|
|
$this->cache[$group][$id] = $data;
|
2005-11-14 17:32:03 -05:00
|
|
|
unset ($this->non_existant_objects[$group][$id]);
|
2005-11-07 16:56:03 -05:00
|
|
|
$this->dirty_objects[$group][] = $id;
|
2005-11-09 06:10:34 -05:00
|
|
|
|
2005-11-07 16:56:03 -05:00
|
|
|
return true;
|
|
|
|
}
|
2005-11-09 06:10:34 -05:00
|
|
|
|
2005-11-07 16:56:03 -05:00
|
|
|
function save() {
|
2005-11-08 17:28:45 -05:00
|
|
|
//$this->stats();
|
2005-11-07 16:56:03 -05:00
|
|
|
|
|
|
|
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');
|
2006-01-01 21:50:59 -05:00
|
|
|
$dir_perms = $stat['mode'] & 0007777; // Get the permission bits.
|
|
|
|
$file_perms = $dir_perms & 0000666; // Remove execute bits for files.
|
2005-11-07 16:56:03 -05:00
|
|
|
|
|
|
|
// Make the base cache dir.
|
|
|
|
if (!file_exists($this->cache_dir)) {
|
2005-11-22 02:45:07 -05:00
|
|
|
if (! @ mkdir($this->cache_dir))
|
2005-11-07 16:56:03 -05:00
|
|
|
return;
|
|
|
|
@ chmod($this->cache_dir, $dir_perms);
|
|
|
|
}
|
2005-11-14 17:32:03 -05:00
|
|
|
|
|
|
|
if (!file_exists($this->cache_dir."index.php")) {
|
2005-11-16 18:40:14 -05:00
|
|
|
@ touch($this->cache_dir."index.php");
|
2006-01-01 21:50:59 -05:00
|
|
|
@ chmod($this->cache_dir."index.php", $file_perms);
|
2005-11-08 17:15:06 -05:00
|
|
|
}
|
2005-11-07 16:56:03 -05:00
|
|
|
|
2005-11-17 04:30:14 -05:00
|
|
|
// Acquire a write lock.
|
2005-11-19 17:52:57 -05:00
|
|
|
$mutex = @fopen($this->cache_dir.$this->flock_filename, 'w');
|
|
|
|
if ( false == $mutex)
|
|
|
|
return;
|
2005-11-17 04:30:14 -05:00
|
|
|
flock($mutex, LOCK_EX);
|
2005-11-07 16:56:03 -05:00
|
|
|
|
|
|
|
// 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) {
|
2005-11-14 17:32:03 -05:00
|
|
|
$cache_file = $group_dir.md5($id.DB_PASSWORD).'.php';
|
2005-11-09 06:10:34 -05:00
|
|
|
|
|
|
|
// Remove the cache file if the key is not set.
|
2005-11-14 17:32:03 -05:00
|
|
|
if (!isset ($this->cache[$group][$id])) {
|
|
|
|
if (file_exists($cache_file))
|
2005-11-09 06:10:34 -05:00
|
|
|
unlink($cache_file);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-11-07 16:56:03 -05:00
|
|
|
$temp_file = tempnam($group_dir, 'tmp');
|
2005-11-14 17:32:03 -05:00
|
|
|
$serial = CACHE_SERIAL_HEADER.serialize($this->cache[$group][$id]).CACHE_SERIAL_FOOTER;
|
2005-11-19 15:12:26 -05:00
|
|
|
$fd = @fopen($temp_file, 'w');
|
|
|
|
if ( false === $fd )
|
|
|
|
continue;
|
2005-11-07 16:56:03 -05:00
|
|
|
fputs($fd, $serial);
|
2005-11-14 17:32:03 -05:00
|
|
|
fclose($fd);
|
|
|
|
if (!@ rename($temp_file, $cache_file)) {
|
2005-11-16 18:40:14 -05:00
|
|
|
if (@ copy($temp_file, $cache_file)) {
|
|
|
|
@ unlink($temp_file);
|
2005-11-08 21:38:17 -05:00
|
|
|
}
|
|
|
|
}
|
2006-01-01 21:50:59 -05:00
|
|
|
@ chmod($cache_file, $file_perms);
|
2005-11-07 16:56:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release write lock.
|
2005-11-17 04:30:14 -05:00
|
|
|
flock($mutex, LOCK_UN);
|
|
|
|
fclose($mutex);
|
2005-11-07 16:56:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function stats() {
|
|
|
|
echo "<p>";
|
|
|
|
echo "<strong>Cold Cache Hits:</strong> {$this->cold_cache_hits}<br/>";
|
|
|
|
echo "<strong>Warm Cache Hits:</strong> {$this->warm_cache_hits}<br/>";
|
|
|
|
echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br/>";
|
|
|
|
echo "</p>";
|
|
|
|
|
|
|
|
foreach ($this->cache as $group => $cache) {
|
|
|
|
echo "<p>";
|
|
|
|
echo "<strong>Group:</strong> $group<br/>";
|
|
|
|
echo "<strong>Cache:</strong>";
|
|
|
|
echo "<pre>";
|
|
|
|
print_r($cache);
|
|
|
|
echo "</pre>";
|
2005-11-14 17:32:03 -05:00
|
|
|
if (isset ($this->dirty_objects[$group])) {
|
2005-11-07 16:56:03 -05:00
|
|
|
echo "<strong>Dirty Objects:</strong>";
|
|
|
|
echo "<pre>";
|
|
|
|
print_r(array_unique($this->dirty_objects[$group]));
|
|
|
|
echo "</pre>";
|
|
|
|
echo "</p>";
|
2005-11-14 17:32:03 -05:00
|
|
|
}
|
2005-11-07 16:56:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function WP_Object_Cache() {
|
|
|
|
global $blog_id;
|
|
|
|
|
2005-11-14 17:32:03 -05:00
|
|
|
if (defined('DISABLE_CACHE'))
|
2005-11-07 16:56:03 -05:00
|
|
|
return;
|
|
|
|
|
2005-11-14 17:32:03 -05:00
|
|
|
if (defined('CACHE_PATH'))
|
2005-11-07 16:56:03 -05:00
|
|
|
$this->cache_dir = CACHE_PATH;
|
|
|
|
else
|
|
|
|
$this->cache_dir = ABSPATH.'wp-content/cache/';
|
|
|
|
|
2005-11-16 21:24:52 -05:00
|
|
|
if (is_writable($this->cache_dir) && is_dir($this->cache_dir)) {
|
2005-11-07 16:56:03 -05:00
|
|
|
$this->cache_enabled = true;
|
2005-11-14 17:32:03 -05:00
|
|
|
} else
|
|
|
|
if (is_writable(ABSPATH.'wp-content')) {
|
|
|
|
$this->cache_enabled = true;
|
|
|
|
}
|
2005-11-09 06:10:34 -05:00
|
|
|
|
2005-11-14 17:32:03 -05:00
|
|
|
if (defined('CACHE_EXPIRATION_TIME'))
|
2005-11-09 06:10:34 -05:00
|
|
|
$this->expiration_time = CACHE_EXPIRATION_TIME;
|
|
|
|
|
2005-11-07 16:56:03 -05:00
|
|
|
$this->blog_id = md5($blog_id);
|
|
|
|
}
|
|
|
|
}
|
2005-12-19 14:14:22 -05:00
|
|
|
?>
|