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

Revision 1125, 10.7 kB (checked in by donncha, 1 year ago)

Merge with WordPress?, rev 6285 and untested

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', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details');
66     var $non_persistent_groups = array('comment');
67     var $blog_id;
68     var $cold_cache_hits = 0;
69     var $warm_cache_hits = 0;
70     var $cache_misses = 0;
71     var $secret = '';
72
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)
77             return false;
78         flock($this->mutex, LOCK_EX);
79         return true;
80     }
81
82     function key($key, $group) {
83         global $blog_id;
84
85         if ( empty($group) )
86             $group = 'default';
87
88         if (false !== array_search($group, $this->global_groups))
89             $prefix = '';
90         else
91             $prefix = $blog_id . '-';
92
93         if( '' != $key )
94             $key = '-' . $key;
95
96         return "$prefix$group$key";
97     }
98
99
100     function add($id, $data, $group = 'default', $expire = '') {
101         if (empty ($group))
102             $group = 'default';
103
104         if (false !== $this->get($id, $group, false))
105             return false;
106
107         return $this->set($id, $data, $group, $expire);
108     }
109
110     function delete($id, $group = 'default', $force = false) {
111         $hash = $this->key($id, $group);
112         if (empty ($group))
113             $group = 'default';
114
115         if (!$force && false === $this->get($id, $group, false))
116             return false;
117
118         unset ($this->cache[$hash]);
119         $this->non_existant_objects[$hash] = true;
120         $this->dirty_objects[$this->key( '', $group )][] = $id;
121         return true;
122     }
123
124     function flush() {
125         if ( !$this->cache_enabled )
126             return true;
127
128         if ( ! $this->acquire_lock() )
129             return false;
130
131         $this->rm_cache_dir();
132         $this->cache = array ();
133         $this->dirty_objects = array ();
134         $this->non_existant_objects = array ();
135
136         $this->release_lock();
137
138         return true;
139     }
140
141     function get($id, $group = 'default', $count_hits = true) {
142         $hash = $this->key($id, $group);
143         $group_key = $this->key( '', $group );
144         if (empty ($group))
145             $group = 'default';
146
147         if (isset ($this->cache[$hash])) {
148             if ($count_hits)
149                 $this->warm_cache_hits += 1;
150             return $this->cache[$hash];
151         }
152
153         if (isset ($this->non_existant_objects[$hash]))
154             return false;
155
156         //  If caching is not enabled, we have to fall back to pulling from the DB.
157         if (!$this->cache_enabled) {
158             if (!isset ($this->cache[$group_key]))
159                 $this->load_group_from_db($group_key);
160
161             if (isset ($this->cache[$hash])) {
162                 $this->cold_cache_hits += 1;
163                 return $this->cache[$hash];
164             }
165
166             $this->non_existant_objects[$hash] = true;
167             $this->cache_misses += 1;
168             return false;
169         }
170
171         $cache_file = $this->cache_dir.$this->get_group_dir($group_key)."/".$this->hash($id).'.php';
172         if (!file_exists($cache_file)) {
173             $this->non_existant_objects[$hash] = true;
174             $this->cache_misses += 1;
175             return false;
176         }
177
178         // If the object has expired, remove it from the cache and return false to force
179         // a refresh.
180         $now = time();
181         if ((filemtime($cache_file) + $this->expiration_time) <= $now) {
182             $this->cache_misses += 1;
183             $this->delete($id, $group, true);
184             return false;
185         }
186
187         $this->cache[$hash] = unserialize(base64_decode(substr(@ file_get_contents($cache_file), strlen(CACHE_SERIAL_HEADER), -strlen(CACHE_SERIAL_FOOTER))));
188         if (false === $this->cache[$hash])
189             $this->cache[$hash] = '';
190
191         $this->cold_cache_hits += 1;
192         return $this->cache[$hash];
193     }
194
195     function get_group_dir($group) {
196         if (false !== array_search($group, $this->global_groups))
197             return $group;
198
199         return "{$this->blog_id}/$group";
200     }
201
202     function hash($data) {
203         if ( function_exists('hash_hmac') ) {
204             return hash_hmac('md5', $data, $this->secret);
205         } else {
206             return md5($data . $this->secret);
207         }
208     }
209
210     function load_group_from_db($group) {
211         return;
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         $hash = $this->key($id, $group);
289         if (empty ($group))
290             $group = 'default';
291
292         if (NULL === $data)
293             $data = '';
294
295         $this->cache[$hash] = $data;
296         unset ($this->non_existant_objects[$hash]);
297         $this->dirty_objects[$this->key( '', $group )][] = $id;
298
299         return true;
300     }
301
302     function save() {
303         //$this->stats();
304
305         if (!$this->cache_enabled)
306             return true;
307
308         if (empty ($this->dirty_objects))
309             return true;
310
311         // Give the new dirs the same perms as wp-content.
312         $stat = stat(ABSPATH.'wp-content');
313         $dir_perms = $stat['mode'] & 0007777; // Get the permission bits.
314         $file_perms = $dir_perms & 0000666; // Remove execute bits for files.
315
316         // Make the base cache dir.
317         if (!file_exists($this->cache_dir)) {
318             if (! @ mkdir($this->cache_dir))
319                 return false;
320             @ chmod($this->cache_dir, $dir_perms);
321         }
322
323         if (!file_exists($this->cache_dir."index.php")) {
324             @ touch($this->cache_dir."index.php");
325             @ chmod($this->cache_dir."index.php", $file_perms);
326         }
327
328         if ( ! $this->acquire_lock() )
329             return false;
330
331         // Loop over dirty objects and save them.
332         $errors = 0;
333         foreach ($this->dirty_objects as $group => $ids) {
334             if ( in_array($group, $this->non_persistent_groups) )
335                 continue;
336
337             $group_dir = $this->make_group_dir($group, $dir_perms);
338
339             $ids = array_unique($ids);
340             foreach ($ids as $id) {
341                 $cache_file = $group_dir.$this->hash($group.'-'.$id).'.php';
342
343                 // Remove the cache file if the key is not set.
344                 if (!isset ($this->cache[$group.'-'.$id])) {
345                     if (file_exists($cache_file))
346                         @ unlink($cache_file);
347                     continue;
348                 }
349
350                 $temp_file = tempnam($group_dir, 'tmp');
351                 $serial = CACHE_SERIAL_HEADER.base64_encode(serialize($this->cache[$group.'-'.$id])).CACHE_SERIAL_FOOTER;
352                 $fd = @fopen($temp_file, 'w');
353                 if ( false === $fd ) {
354                     $errors++;
355                     continue;
356                 }
357                 fputs($fd, $serial);
358                 fclose($fd);
359                 if (!@ rename($temp_file, $cache_file)) {
360                     if (!@ copy($temp_file, $cache_file))
361                         $errors++;
362                     @ unlink($temp_file);
363                 }
364                 @ chmod($cache_file, $file_perms);
365             }
366         }
367
368         $this->dirty_objects = array();
369
370         $this->release_lock();
371
372         if ( $errors )
373             return false;
374
375         return true;
376     }
377
378     function stats() {
379         echo "<p>";
380         echo "<strong>Cold Cache Hits:</strong> {$this->cold_cache_hits}<br />";
381         echo "<strong>Warm Cache Hits:</strong> {$this->warm_cache_hits}<br />";
382         echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />";
383         echo "</p>";
384
385         foreach ($this->cache as $group => $cache) {
386             echo "<p>";
387             echo "<strong>Group:</strong> $group<br />";
388             echo "<strong>Cache:</strong>";
389             echo "<pre>";
390             print_r($cache);
391             echo "</pre>";
392             if (isset ($this->dirty_objects[$group])) {
393                 echo "<strong>Dirty Objects:</strong>";
394                 echo "<pre>";
395                 print_r(array_unique($this->dirty_objects[$group]));
396                 echo "</pre>";
397                 echo "</p>";
398             }
399         }
400     }
401
402     function WP_Object_Cache() {
403         return $this->__construct();
404     }
405
406     function __construct() {
407         global $blog_id;
408
409         register_shutdown_function(array(&$this, "__destruct"));
410
411         if (defined('DISABLE_CACHE'))
412             return;
413
414         if ( ! defined('ENABLE_CACHE') )
415             return;
416
417         // Disable the persistent cache if safe_mode is on.
418         if ( ini_get('safe_mode') && ! defined('ENABLE_CACHE') )
419             return;
420
421         if (defined('CACHE_PATH'))
422             $this->cache_dir = CACHE_PATH;
423         else
424             // Using the correct separator eliminates some cache flush errors on Windows
425             $this->cache_dir = ABSPATH.'wp-content'.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR;
426
427         if (is_writable($this->cache_dir) && is_dir($this->cache_dir)) {
428                 $this->cache_enabled = true;
429         } else {
430             if (is_writable(ABSPATH.'wp-content')) {
431                 $this->cache_enabled = true;
432             }
433         }
434
435         if (defined('CACHE_EXPIRATION_TIME'))
436             $this->expiration_time = CACHE_EXPIRATION_TIME;
437
438         if ( defined('WP_SECRET') )
439             $this->secret = WP_SECRET;
440         else
441             $this->secret = DB_PASSWORD . DB_USER . DB_NAME . DB_HOST . ABSPATH;
442
443         $this->blog_id = $this->hash($blog_id);
444     }
445
446     function __destruct() {
447         $this->save();
448         return true;
449     }
450 }
451 ?>
452
Note: See TracBrowser for help on using the browser.