Guest

Computer Guru

By: a guest on Sep 20th, 2007  |  syntax: PHP  |  size: 3.48 KB  |  hits: 393  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. <?php
  2.  
  3. /*
  4. Name: eAccelerator for WordPress
  5. Description: eAccelerator backend for the WP Object Cache.
  6. Version: 0.5.1
  7. URI: http://neosmart.net/dl.php?id=13
  8. Author: Computer Guru
  9. Author URI: http://neosmart.net/blog/
  10.  
  11. * Install this file to /wp-content/object-cache.php
  12. * If on Windows, restart IIS after installing for best results
  13.  
  14. Thanks to Ryan Boren for his original memcached code.
  15.  
  16. */
  17.  
  18. // Gracefully revert to default cache if eAccelerator is not installed
  19. if ( !function_exists('eaccelerator_get') )
  20. {
  21.     include_once(ABSPATH . WPINC . '/cache.php');
  22. }
  23. else
  24. {
  25.  
  26. function wp_cache_add($key, $data, $flag = '', $expire = 0)
  27. {
  28.         return wp_cache_set($key, $data, $flag, $expire);
  29. }
  30.  
  31. function wp_cache_close()
  32. {
  33.         return true;
  34. }
  35.  
  36. function wp_cache_delete($id, $flag = '')
  37. {
  38.         global $wp_object_cache;
  39.  
  40.         return $wp_object_cache->delete($id, $flag);
  41. }
  42.  
  43. function wp_cache_flush()
  44. {
  45.         global $wp_object_cache;
  46.  
  47.         return $wp_object_cache->flush();
  48. }
  49.  
  50. function wp_cache_get($id, $flag = '')
  51. {
  52.         global $wp_object_cache;
  53.  
  54.         return $wp_object_cache->get($id, $flag);
  55. }
  56.  
  57. function wp_cache_init()
  58. {
  59.         global $wp_object_cache;
  60.  
  61.         $wp_object_cache = new WP_Object_Cache();
  62. }
  63.  
  64. function wp_cache_replace($key, $data, $flag = '', $expire = 0)
  65. {
  66.         return wp_cache_set($key, $data, $flag, $expire);
  67. }
  68.  
  69. function wp_cache_set($key, $data, $flag = '', $expire = 0)
  70. {
  71.         global $wp_object_cache;
  72.  
  73.         return $wp_object_cache->set($key, $data, $flag, $expire);
  74. }
  75.  
  76. class WP_Object_Cache {
  77.         var $global_groups = array ('users', 'userlogins', 'usermeta');
  78.         var $cache = array();
  79.  
  80.         function delete($id, $group = 'default')
  81.         {
  82.                 $key = $this->key($id, $group);
  83.                 $result = eaccelerator_rm($key);
  84.                 if ( $result )
  85.                         unset($this->cache[$key]);
  86.                 return $result;
  87.         }
  88.  
  89.         function flush()
  90.         {
  91.                 eaccelerator_clear();
  92.                 return true;
  93.         }
  94.  
  95.         function get($id, $group = 'default')
  96.         {
  97.                 $key = $this->key($id, $group);
  98.  
  99.                 if ( isset($this->cache[$key]) )
  100.                         $value = $this->cache[$key];
  101.                 else
  102.                         $value = eaccelerator_get($key);
  103.  
  104.                 $value = maybe_unserialize($value);
  105.  
  106.                 /* echo "Cache key: $key<br/>";
  107.                 echo 'Cache value:<br/>';
  108.                 var_dump($value);
  109.                 echo '<br/>'; */
  110.                 if ( NULL === $value )
  111.                         $value = false;
  112.  
  113.                 $this->cache[$key] = $value;
  114.  
  115.                 return $value;
  116.         }
  117.  
  118.         function set($id, $data, $group = 'default', $expire = 0)
  119.         {
  120.                 $key = $this->key($id, $group);
  121.                 if ( is_resource($data) )
  122.                         return false;
  123.  
  124.                 $data = maybe_serialize($data);
  125.  
  126.                 $result = eaccelerator_put($key, $data, $expire);
  127.                 if ( $result )
  128.                         $this->cache[$key] = $data;
  129.  
  130.                 return $result;
  131.         }
  132.  
  133.         function key($key, $group)
  134.         {
  135.                 global $blog_id;
  136.  
  137.                 if ( empty($group) )
  138.                                 $group = 'default';
  139.  
  140.                 if (false !== array_search($group, $this->global_groups))
  141.                                 $prefix = '';
  142.                 else
  143.                                 $prefix = $blog_id . ':';
  144.  
  145.                 return md5(ABSPATH . "$prefix$group:$key");
  146.         }
  147.  
  148.         function stats()
  149.         {
  150.                 // Note that this is the total eAccelerator stats, not just WP but also any other apps using eAccelerator var storage
  151.                 $eaccelerator_info = eaccelerator_info();
  152.                 echo "<p>\n";
  153.                 echo "<strong>Cached Variables:</strong> {$eaccelerator_info['cachedKeys']}<br/>\n";
  154.                 echo "<strong>Cached Scripts:&nbsp;&nbsp;</strong> {$eaccelerator_info['cachedScripts']}<br/>\n";
  155.                 echo "</p>\n";
  156.  
  157.                 if ( !empty($this->cache) )
  158.                 {
  159.                         echo "<pre>\n\r";
  160.                         print_r($this->cache);
  161.                         echo "</pre>\n\r";
  162.                 }
  163.         }
  164.  
  165.         function WP_Object_Cache()
  166.         {
  167.                 // Empty Constructor
  168.         }
  169. }
  170. } //End Else
  171. ?>