WordPress.org

Make WordPress Core

Opened 8 years ago

Closed 7 years ago

Last modified 5 years ago

#2753 closed enhancement (wontfix)

Use shared memory caching facilities

Reported by: dammit Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.1
Component: Optimization Keywords:
Focuses: Cc:

Description

Index: wp-includes/cache.php
===================================================================
--- wp-includes/cache.php       (revision 3809)
+++ wp-includes/cache.php       (working copy)
@@ -31,8 +31,13 @@
 
 function wp_cache_init() {
        global $wp_object_cache;
-
-       $wp_object_cache = new WP_Object_Cache();
+       if (defined('DISABLE_CACHE') or !defined('ENABLE_CACHE')) {
+               $wp_object_cache = new WP_Object_Cache_Void();
+       } elseif (function_exists('apc_store')) {
+               $wp_object_cache = new WP_Object_Cache_APC();
+       } else {
+               $wp_object_cache = new WP_Object_Cache();
+       }
 }
 
 function wp_cache_replace($key, $data, $flag = '', $expire = 0) {
@@ -417,4 +422,56 @@
                $this->blog_id = md5($blog_id);
        }
 }
+
+/**
+ * Base class for shared-memory enabled caches, like APC, Turck, etc
+ */
+class WP_Object_Cache_Void {
+       /* May be reused by children */
+       function add($id, $data, $group='default', $expire=0) { 
+               if (empty($group))
+                       $group = 'default';
+               if ($this->get($id, $group))
+                       return false;
+               return $this->set($id, $data, $group, $expire);
+       }
+       /* May be reused */
+       function replace($id, $data, $group = 'default', $expire = 0) {
+               if (empty ($group))
+                       $group = 'default';
+
+               if (false === $this->get($id, $group))
+                       return false;
+
+               return $this->set($id, $data, $group, $expire);
+       }
+
+       /* Just a common useful function :) */
+       function getKey($id,$group) {
+               global $blog_id;
+               return "{$blog_id}:{$id}:{$group}";
+       }
+
+       function save() { return true; }
+       function delete($id, $group = 'default', $force = false) { return true; }
+       function flush() { return true; }
+       function get($id, $group = 'default') { return false; }
+       function set($id, $data, $group = 'default', $expire = 0) { return true;}
+}
+
+class WP_Object_Cache_APC extends WP_Object_Cache_Void {
+       function get($id,$group='default') {
+               if (empty ($group)) $group = 'default';
+               return apc_fetch($this->getKey($id,$group));
+       }
+       function set($id,$data,$group='default',$expire=0) {
+               if (empty ($group)) $group = 'default';
+               return apc_store($this->getkey($id,$group),$data,$expire);
+       }
+       function delete($id,$group='default', $forse = false) {
+               if (empty ($group)) $group = 'default';
+               return apc_delete($this->getkey($id,$group));
+       }
+}
+
 ?>

Change History (3)

comment:1 markjaquith8 years ago

For APC, you'll need to put ABSPATH into the key, or multiple blogs on the same server will interfere with each other. Also, I've had problems with APC (using my own object cache backend) returning the wrong category (e.g. :category:1 will return data from :category:112 sometimes). I'm still investigating it... my solution for now has been to md5 the key, to ensure that I don't get some sort of partial match.

comment:2 dammit8 years ago

right, adding md5sum( ABSPATH . dbname . blogid) would be probably a good way to go.

there is something yet not complete here, as it failed to work on my main blog, I'll try testing in 'lab'.

comment:3 Nazgul7 years ago

  • Resolution set to wontfix
  • Status changed from new to closed

No traction in over a year, so closing as wontfix.

Feel free to reopen if you have patches/suggestions/...

Note: See TracTickets for help on using tickets.