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