2 function wp_cache_add($key, $data, $flag = '', $expire = 0) {
3 global $wp_object_cache;
4 $data = unserialize(serialize($data));
6 return $wp_object_cache->add($key, $data, $flag, $expire);
9 function wp_cache_close() {
10 global $wp_object_cache;
12 if ( ! isset($wp_object_cache) )
14 return $wp_object_cache->save();
17 function wp_cache_delete($id, $flag = '') {
18 global $wp_object_cache;
20 return $wp_object_cache->delete($id, $flag);
23 function wp_cache_flush() {
24 global $wp_object_cache;
26 return $wp_object_cache->flush();
29 function wp_cache_get($id, $flag = '') {
30 global $wp_object_cache;
32 return $wp_object_cache->get($id, $flag);
35 function wp_cache_init() {
36 $GLOBALS['wp_object_cache'] =& new WP_Object_Cache();
39 function wp_cache_replace($key, $data, $flag = '', $expire = 0) {
40 global $wp_object_cache;
41 $data = unserialize(serialize($data));
43 return $wp_object_cache->replace($key, $data, $flag, $expire);
46 function wp_cache_set($key, $data, $flag = '', $expire = 0) {
47 global $wp_object_cache;
48 $data = unserialize(serialize($data));
50 return $wp_object_cache->set($key, $data, $flag, $expire);
53 define('CACHE_SERIAL_HEADER', "<?php\n/*");
54 define('CACHE_SERIAL_FOOTER', "*/\n?".">");
56 class WP_Object_Cache {
58 var $cache_enabled = false;
59 var $expiration_time = 900;
60 var $flock_filename = 'wp_object_cache.lock';
62 var $cache = array ();
63 var $dirty_objects = array ();
64 var $non_existant_objects = array ();
65 var $global_groups = array ('users', 'userlogins', 'usermeta');
66 var $non_persistent_groups = array('comment');
68 var $cold_cache_hits = 0;
69 var $warm_cache_hits = 0;
70 var $cache_misses = 0;
73 function acquire_lock() {
74 // Acquire a write lock.
75 $this->mutex = @fopen($this->cache_dir.$this->flock_filename, 'w');
76 if ( false == $this->mutex)
78 flock($this->mutex, LOCK_EX);
82 function add($id, $data, $group = 'default', $expire = '') {
86 if (false !== $this->get($id, $group, false))
89 return $this->set($id, $data, $group, $expire);
92 function delete($id, $group = 'default', $force = false) {
96 if (!$force && false === $this->get($id, $group, false))
99 unset ($this->cache[$group][$id]);
100 $this->non_existant_objects[$group][$id] = true;
101 $this->dirty_objects[$group][] = $id;
106 if ( !$this->cache_enabled )
109 if ( ! $this->acquire_lock() )
112 $this->rm_cache_dir();
113 $this->cache = array ();
114 $this->dirty_objects = array ();
115 $this->non_existant_objects = array ();
117 $this->release_lock();
122 function get($id, $group = 'default', $count_hits = true) {
126 if (isset ($this->cache[$group][$id])) {
128 $this->warm_cache_hits += 1;
129 return $this->cache[$group][$id];
132 if (isset ($this->non_existant_objects[$group][$id]))
135 // If caching is not enabled, we have to fall back to pulling from the DB.
136 if (!$this->cache_enabled) {
137 if (!isset ($this->cache[$group]))
138 $this->load_group_from_db($group);
140 if (isset ($this->cache[$group][$id])) {
141 $this->cold_cache_hits += 1;
142 return $this->cache[$group][$id];
145 $this->non_existant_objects[$group][$id] = true;
146 $this->cache_misses += 1;
150 $cache_file = $this->cache_dir.$this->get_group_dir($group)."/".$this->hash($id).'.php';
151 if (!file_exists($cache_file)) {
152 $this->non_existant_objects[$group][$id] = true;
153 $this->cache_misses += 1;
157 // If the object has expired, remove it from the cache and return false to force
160 if ((filemtime($cache_file) + $this->expiration_time) <= $now) {
161 $this->cache_misses += 1;
162 $this->delete($id, $group, true);
166 $this->cache[$group][$id] = unserialize(base64_decode(substr(@ file_get_contents($cache_file), strlen(CACHE_SERIAL_HEADER), -strlen(CACHE_SERIAL_FOOTER))));
167 if (false === $this->cache[$group][$id])
168 $this->cache[$group][$id] = '';
170 $this->cold_cache_hits += 1;
171 return $this->cache[$group][$id];
174 function get_group_dir($group) {
175 if (false !== array_search($group, $this->global_groups))
178 return "{$this->blog_id}/$group";
181 function hash($data) {
182 if ( function_exists('hash_hmac') ) {
183 return hash_hmac('md5', $data, $this->secret);
185 return md5($data . $this->secret);
189 function load_group_from_db($group) {
193 function make_group_dir($group, $perms) {
194 $group_dir = $this->get_group_dir($group);
196 foreach (split('/', $group_dir) as $subdir) {
197 $make_dir .= "$subdir/";
198 if (!file_exists($this->cache_dir.$make_dir)) {
199 if (! @ mkdir($this->cache_dir.$make_dir))
201 @ chmod($this->cache_dir.$make_dir, $perms);
204 if (!file_exists($this->cache_dir.$make_dir."index.php")) {
205 $file_perms = $perms & 0000666;
206 @ touch($this->cache_dir.$make_dir."index.php");
207 @ chmod($this->cache_dir.$make_dir."index.php", $file_perms);
211 return $this->cache_dir."$group_dir/";
214 function rm_cache_dir() {
215 $dir = $this->cache_dir;
216 $dir = rtrim($dir, DIRECTORY_SEPARATOR);
218 $stack = array($dir);
221 while ($index < count($stack)) {
222 # Get indexed directory from stack
223 $dir = $stack[$index];
225 $dh = @ opendir($dir);
229 while (($file = @ readdir($dh)) !== false) {
230 if ($file == '.' or $file == '..')
233 if (@ is_dir($dir . DIRECTORY_SEPARATOR . $file))
234 $stack[] = $dir . DIRECTORY_SEPARATOR . $file;
235 else if (@ is_file($dir . DIRECTORY_SEPARATOR . $file))
236 @ unlink($dir . DIRECTORY_SEPARATOR . $file);
242 $stack = array_reverse($stack); // Last added dirs are deepest
243 foreach($stack as $dir) {
244 if ( $dir != $top_dir)
250 function release_lock() {
251 // Release write lock.
252 flock($this->mutex, LOCK_UN);
253 fclose($this->mutex);
256 function replace($id, $data, $group = 'default', $expire = '') {
260 if (false === $this->get($id, $group, false))
263 return $this->set($id, $data, $group, $expire);
266 function set($id, $data, $group = 'default', $expire = '') {
273 $this->cache[$group][$id] = $data;
274 unset ($this->non_existant_objects[$group][$id]);
275 $this->dirty_objects[$group][] = $id;
283 if (!$this->cache_enabled)
286 if (empty ($this->dirty_objects))
289 // Give the new dirs the same perms as wp-content.
290 $stat = stat(ABSPATH.'wp-content');
291 $dir_perms = $stat['mode'] & 0007777; // Get the permission bits.
292 $file_perms = $dir_perms & 0000666; // Remove execute bits for files.
294 // Make the base cache dir.
295 if (!file_exists($this->cache_dir)) {
296 if (! @ mkdir($this->cache_dir))
298 @ chmod($this->cache_dir, $dir_perms);
301 if (!file_exists($this->cache_dir."index.php")) {
302 @ touch($this->cache_dir."index.php");
303 @ chmod($this->cache_dir."index.php", $file_perms);
306 if ( ! $this->acquire_lock() )
309 // Loop over dirty objects and save them.
311 foreach ($this->dirty_objects as $group => $ids) {
312 if ( in_array($group, $this->non_persistent_groups) )
315 $group_dir = $this->make_group_dir($group, $dir_perms);
317 $ids = array_unique($ids);
318 foreach ($ids as $id) {
319 $cache_file = $group_dir.$this->hash($id).'.php';
321 // Remove the cache file if the key is not set.
322 if (!isset ($this->cache[$group][$id])) {
323 if (file_exists($cache_file))
324 @ unlink($cache_file);
328 $temp_file = tempnam($group_dir, 'tmp');
329 $serial = CACHE_SERIAL_HEADER.base64_encode(serialize($this->cache[$group][$id])).CACHE_SERIAL_FOOTER;
330 $fd = @fopen($temp_file, 'w');
331 if ( false === $fd ) {
337 if (!@ rename($temp_file, $cache_file)) {
338 if (!@ copy($temp_file, $cache_file))
340 @ unlink($temp_file);
342 @ chmod($cache_file, $file_perms);
346 $this->dirty_objects = array();
348 $this->release_lock();
358 echo "<strong>Cold Cache Hits:</strong> {$this->cold_cache_hits}<br />";
359 echo "<strong>Warm Cache Hits:</strong> {$this->warm_cache_hits}<br />";
360 echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />";
363 foreach ($this->cache as $group => $cache) {
365 echo "<strong>Group:</strong> $group<br />";
366 echo "<strong>Cache:</strong>";
370 if (isset ($this->dirty_objects[$group])) {
371 echo "<strong>Dirty Objects:</strong>";
373 print_r(array_unique($this->dirty_objects[$group]));
380 function WP_Object_Cache() {
381 return $this->__construct();
384 function __construct() {
387 register_shutdown_function(array(&$this, "__destruct"));
389 if (defined('DISABLE_CACHE'))
392 if ( ! defined('ENABLE_CACHE') )
395 // Disable the persistent cache if safe_mode is on.
396 if ( ini_get('safe_mode') && ! defined('ENABLE_CACHE') )
399 if (defined('CACHE_PATH'))
400 $this->cache_dir = CACHE_PATH;
402 // Using the correct separator eliminates some cache flush errors on Windows
403 $this->cache_dir = ABSPATH.'wp-content'.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR;
405 if (is_writable($this->cache_dir) && is_dir($this->cache_dir)) {
406 $this->cache_enabled = true;
408 if (is_writable(ABSPATH.'wp-content')) {
409 $this->cache_enabled = true;
413 if (defined('CACHE_EXPIRATION_TIME'))
414 $this->expiration_time = CACHE_EXPIRATION_TIME;
416 if ( defined('WP_SECRET') )
417 $this->secret = WP_SECRET;
419 $this->secret = DB_PASSWORD . DB_USER . DB_NAME . DB_HOST . ABSPATH;
421 $this->blog_id = $this->hash($blog_id);
424 function __destruct() {