root/tags/2.1.1/wp-includes/cache.php

Revision 4857, 10.8 kB (checked in by markjaquith, 2 years ago)

make sure nothing going into the object cache is being passed by reference. fixes #3726

  • Property svn:eol-style set to native
Line 
1 <?php
2 function wp_cache_add($key, $data, $flag = '', $expire = 0) {
3     global $wp_object_cache;
4     $data = unserialize(serialize($data));
5
6     return $wp_object_cache->add($key, $data, $flag, $expire);
7 }
8
9 function wp_cache_close() {
10     global $wp_object_cache;
11
12     if ( ! isset($wp_object_cache) )
13         return;
14     return $wp_object_cache->save();
15 }
16
17 function wp_cache_delete($id, $flag = '') {
18     global $wp_object_cache;
19
20     return $wp_object_cache->delete($id, $flag);
21 }
22
23 function wp_cache_flush() {
24     global $wp_object_cache;
25
26     return $wp_object_cache->flush();
27 }
28
29 function wp_cache_get($id, $flag = '') {
30     global $wp_object_cache;
31
32     return $wp_object_cache->get($id, $flag);
33 }
34
35 function wp_cache_init() {
36     $GLOBALS['wp_object_cache'] =& new WP_Object_Cache();
37 }
38
39 function wp_cache_replace($key, $data, $flag = '', $expire = 0) {
40     global $wp_object_cache;
41     $data = unserialize(serialize($data));
42
43     return $wp_object_cache->replace($key, $data, $flag, $expire);
44 }
45
46 function wp_cache_set($key, $data, $flag = '', $expire = 0) {
47     global $wp_object_cache;
48     $data = unserialize(serialize($data));
49
50     return $wp_object_cache->set($key, $data, $flag, $expire);
51 }
52
53 define('CACHE_SERIAL_HEADER', "<?php\n/*");
54 define('CACHE_SERIAL_FOOTER', "*/\n?".">");
55
56 class WP_Object_Cache {
57     var $cache_dir;
58     var $cache_enabled = false;
59     var $expiration_time = 900;
60     var $flock_filename = 'wp_object_cache.lock';
61     var $mutex;
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 $blog_id;
67     var $cold_cache_hits = 0;
68     var $warm_cache_hits = 0;
69     var $cache_misses = 0;
70     var $secret = '';
71
72     function acquire_lock() {
73         // Acquire a write lock.
74         $this->mutex = @fopen($this->cache_dir.$this->flock_filename, 'w');
75         if ( false == $this->mutex)
76             return false;
77         flock($this->mutex, LOCK_EX);
78         return true;
79     }
80
81     function add($id, $data, $group = 'default', $expire = '') {
82         if (empty ($group))
83             $group = 'default';
84
85         if (false !== $this->get($id, $group, false))
86             return false;
87
88         return $this->set($id, $data, $group, $expire);
89     }
90
91     function delete($id, $group = 'default', $force = false) {
92         if (empty ($group))
93             $group = 'default';
94
95         if (!$force && false === $this->get($id, $group, false))
96             return false;
97
98         unset ($this->cache[$group][$id]);
99         $this->non_existant_objects[$group][$id] = true;
100         $this->dirty_objects[$group][] = $id;
101         return true;
102     }
103
104     function flush() {
105         if ( !$this->cache_enabled )
106             return true;
107
108         if ( ! $this->acquire_lock() )
109             return false;
110
111         $this->rm_cache_dir();
112         $this->cache = array ();
113         $this->dirty_objects = array ();
114         $this->non_existant_objects = array ();
115
116         $this->release_lock();
117
118         return true;
119     }
120
121     function get($id, $group = 'default', $count_hits = true) {
122         if (empty ($group))
123             $group = 'default';
124
125         if (isset ($this->cache[$group][$id])) {
126             if ($count_hits)
127                 $this->warm_cache_hits += 1;
128             return $this->cache[$group][$id];
129         }
130
131         if (isset ($this->non_existant_objects[$group][$id]))
132             return false;
133
134         //  If caching is not enabled, we have to fall back to pulling from the DB.
135         if (!$this->cache_enabled) {
136             if (!isset ($this->cache[$group]))
137                 $this->load_group_from_db($group);
138
139             if (isset ($this->cache[$group][$id])) {
140                 $this->cold_cache_hits += 1;
141                 return $this->cache[$group][$id];
142             }
143
144             $this->non_existant_objects[$group][$id] = true;
145             $this->cache_misses += 1;
146             return false;
147         }
148
149         $cache_file = $this->cache_dir.$this->get_group_dir($group)."/".$this->hash($id).'.php';
150         if (!file_exists($cache_file)) {
151             $this->non_existant_objects[$group][$id] = true;
152             $this->cache_misses += 1;
153             return false;
154         }
155
156         // If the object has expired, remove it from the cache and return false to force
157         // a refresh.
158         $now = time();
159         if ((filemtime($cache_file) + $this->expiration_time) <= $now) {
160             $this->cache_misses += 1;
161             $this->delete($id, $group, true);
162             return false;
163         }
164
165         $this->cache[$group][$id] = unserialize(base64_decode(substr(@ file_get_contents($cache_file), strlen(CACHE_SERIAL_HEADER), -strlen(CACHE_SERIAL_FOOTER))));
166         if (false === $this->cache[$group][$id])
167             $this->cache[$group][$id] = '';
168
169         $this->cold_cache_hits += 1;
170         return $this->cache[$group][$id];
171     }
172
173     function get_group_dir($group) {
174         if (false !== array_search($group, $this->global_groups))
175             return $group;
176
177         return "{$this->blog_id}/$group";
178     }
179
180     function hash($data) {
181         if ( function_exists('hash_hmac') ) {
182             return hash_hmac('md5', $data, $this->secret);
183         } else {
184             return md5($data . $this->secret);
185         }
186     }
187
188     function load_group_from_db($group) {
189         global $wpdb;
190
191         if ('category' == $group) {
192             $this->cache['category'] = array ();
193             if ($dogs = $wpdb->get_results("SELECT * FROM $wpdb->categories")) {
194                 foreach ($dogs as $catt)
195                     $this->cache['category'][$catt->cat_ID] = $catt;
196             }
197         } else
198             if ('options' == $group) {
199                 $wpdb->hide_errors();
200                 if (!$options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'")) {
201                     $options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options");
202                 }
203                 $wpdb->show_errors();
204
205                 if ( ! $options )
206                     return;
207
208                 foreach ($options as $option) {
209                     $this->cache['options'][$option->option_name] = $option->option_value;
210                 }
211             }
212     }
213
214     function make_group_dir($group, $perms) {
215         $group_dir = $this->get_group_dir($group);
216         $make_dir = '';
217         foreach (split('/', $group_dir) as $subdir) {
218             $make_dir .= "$subdir/";
219             if (!file_exists($this->cache_dir.$make_dir)) {
220                 if (! @ mkdir($this->cache_dir.$make_dir))
221                     break;
222                 @ chmod($this->cache_dir.$make_dir, $perms);
223             }
224
225             if (!file_exists($this->cache_dir.$make_dir."index.php")) {
226                 $file_perms = $perms & 0000666;
227                 @ touch($this->cache_dir.$make_dir."index.php");
228                 @ chmod($this->cache_dir.$make_dir."index.php", $file_perms);
229             }
230         }
231
232         return $this->cache_dir."$group_dir/";
233     }
234
235     function rm_cache_dir() {
236         $dir = $this->cache_dir;
237         $dir = rtrim($dir, DIRECTORY_SEPARATOR);
238         $top_dir = $dir;
239         $stack = array($dir);
240         $index = 0;
241
242         while ($index < count($stack)) {
243             # Get indexed directory from stack
244             $dir = $stack[$index];
245
246             $dh = @ opendir($dir);
247             if (!$dh)
248                 return false;
249
250             while (($file = @ readdir($dh)) !== false) {
251                 if ($file == '.' or $file == '..')
252                     continue;
253
254                 if (@ is_dir($dir . DIRECTORY_SEPARATOR . $file))
255                     $stack[] = $dir . DIRECTORY_SEPARATOR . $file;
256                 else if (@ is_file($dir . DIRECTORY_SEPARATOR . $file))
257                     @ unlink($dir . DIRECTORY_SEPARATOR . $file);
258             }
259
260             $index++;
261         }
262
263         $stack = array_reverse($stack);  // Last added dirs are deepest
264         foreach($stack as $dir) {
265             if ( $dir != $top_dir)
266                 @ rmdir($dir);
267         }
268
269     }
270
271     function release_lock() {
272         // Release write lock.
273         flock($this->mutex, LOCK_UN);
274         fclose($this->mutex);
275     }
276
277     function replace($id, $data, $group = 'default', $expire = '') {
278         if (empty ($group))
279             $group = 'default';
280
281         if (false === $this->get($id, $group, false))
282             return false;
283
284         return $this->set($id, $data, $group, $expire);
285     }
286
287     function set($id, $data, $group = 'default', $expire = '') {
288         if (empty ($group))
289             $group = 'default';
290
291         if (NULL == $data)
292             $data = '';
293
294         $this->cache[$group][$id] = $data;
295         unset ($this->non_existant_objects[$group][$id]);
296         $this->dirty_objects[$group][] = $id;
297
298         return true;
299     }
300
301     function save() {
302         //$this->stats();
303
304         if (!$this->cache_enabled)
305             return true;
306
307         if (empty ($this->dirty_objects))
308             return true;
309
310         // Give the new dirs the same perms as wp-content.
311         $stat = stat(ABSPATH.'wp-content');
312         $dir_perms = $stat['mode'] & 0007777; // Get the permission bits.
313         $file_perms = $dir_perms & 0000666; // Remove execute bits for files.
314
315         // Make the base cache dir.
316         if (!file_exists($this->cache_dir)) {
317             if (! @ mkdir($this->cache_dir))
318                 return false;
319             @ chmod($this->cache_dir, $dir_perms);
320         }
321
322         if (!file_exists($this->cache_dir."index.php")) {
323             @ touch($this->cache_dir."index.php");
324             @ chmod($this->cache_dir."index.php", $file_perms);
325         }
326
327         if ( ! $this->acquire_lock() )
328             return false;
329
330         // Loop over dirty objects and save them.
331         $errors = 0;
332         foreach ($this->dirty_objects as $group => $ids) {
333             $group_dir = $this->make_group_dir($group, $dir_perms);
334
335             $ids = array_unique($ids);
336             foreach ($ids as $id) {
337                 $cache_file = $group_dir.$this->hash($id).'.php';
338
339                 // Remove the cache file if the key is not set.
340                 if (!isset ($this->cache[$group][$id])) {
341                     if (file_exists($cache_file))
342                         @ unlink($cache_file);
343                     continue;
344                 }
345
346                 $temp_file = tempnam($group_dir, 'tmp');
347                 $serial = CACHE_SERIAL_HEADER.base64_encode(serialize($this->cache[$group][$id])).CACHE_SERIAL_FOOTER;
348                 $fd = @fopen($temp_file, 'w');
349                 if ( false === $fd ) {
350                     $errors++;
351                     continue;
352                 }
353                 fputs($fd, $serial);
354                 fclose($fd);
355                 if (!@ rename($temp_file, $cache_file)) {
356                     if (@ copy($temp_file, $cache_file))
357                         @ unlink($temp_file);
358                     else
359                         $errors++;
360                 }
361                 @ chmod($cache_file, $file_perms);
362             }
363         }
364
365         $this->dirty_objects = array();
366
367         $this->release_lock();
368
369         if ( $errors )
370             return false;
371
372         return true;
373     }
374
375     function stats() {
376         echo "<p>";
377         echo "<strong>Cold Cache Hits:</strong> {$this->cold_cache_hits}<br/>";
378         echo "<strong>Warm Cache Hits:</strong> {$this->warm_cache_hits}<br/>";
379         echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br/>";
380         echo "</p>";
381
382         foreach ($this->cache as $group => $cache) {
383             echo "<p>";
384             echo "<strong>Group:</strong> $group<br/>";
385             echo "<strong>Cache:</strong>";
386             echo "<pre>";
387             print_r($cache);
388             echo "</pre>";
389             if (isset ($this->dirty_objects[$group])) {
390                 echo "<strong>Dirty Objects:</strong>";
391                 echo "<pre>";
392                 print_r(array_unique($this->dirty_objects[$group]));
393                 echo "</pre>";
394                 echo "</p>";
395             }
396         }
397     }
398
399     function WP_Object_Cache() {
400         return $this->__construct();
401     }
402     
403     function __construct() {
404         global $blog_id;
405
406         register_shutdown_function(array(&$this, "__destruct"));
407
408         if (defined('DISABLE_CACHE'))
409             return;
410
411         if ( ! defined('ENABLE_CACHE') )
412             return;
413
414         // Disable the persistent cache if safe_mode is on.
415         if ( ini_get('safe_mode') && ! defined('ENABLE_CACHE') )
416             return;
417
418         if (defined('CACHE_PATH'))
419             $this->cache_dir = CACHE_PATH;
420         else
421             // Using the correct separator eliminates some cache flush errors on Windows
422             $this->cache_dir = ABSPATH.'wp-content'.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR;
423
424         if (is_writable($this->cache_dir) && is_dir($this->cache_dir)) {
425                 $this->cache_enabled = true;
426         } else {
427             if (is_writable(ABSPATH.'wp-content')) {
428                 $this->cache_enabled = true;
429             }
430         }
431
432         if (defined('CACHE_EXPIRATION_TIME'))
433             $this->expiration_time = CACHE_EXPIRATION_TIME;
434
435         if ( defined('WP_SECRET') )
436             $this->secret = WP_SECRET;
437         else
438             $this->secret = DB_PASSWORD . DB_USER . DB_NAME . DB_HOST . ABSPATH;
439
440         $this->blog_id = $this->hash($blog_id);
441     }
442
443     function __destruct() {
444         $this->save();
445         return true;   
446     }
447 }
448 ?>
449
Note: See TracBrowser for help on using the browser.