From 8edb3d9405c7d409bcabe38274cafbd5438575f6 Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Wed, 18 May 2011 08:37:00 -0400
Subject: [PATCH] - Add APC support in rcube_cache

---
 CHANGELOG                       |    2 +-
 program/include/rcube_cache.php |   40 +++++++++++++++++++++++++++++++++-------
 program/include/rcube_imap.php  |    2 +-
 program/include/rcmail.php      |    2 +-
 config/main.inc.php.dist        |    2 +-
 5 files changed, 37 insertions(+), 11 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 37f4cf4..e3afea7 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,7 +1,7 @@
 CHANGELOG Roundcube Webmail
 ===========================
 
-- Added general rcube_cache class with memcache support
+- Added general rcube_cache class with Memcache and APC support
 - Improved caching performance by skipping writes of unchanged data
 - Option enable_caching replaced by imap_cache and messages_cache options
 - Add forward-as-attachment feature
diff --git a/config/main.inc.php.dist b/config/main.inc.php.dist
index 169a3a0..f63a181 100644
--- a/config/main.inc.php.dist
+++ b/config/main.inc.php.dist
@@ -109,7 +109,7 @@
 // Optional IMAP authentication password to be used for imap_auth_cid
 $rcmail_config['imap_auth_pw'] = null;
 
-// Type of IMAP indexes cache. Supported values: 'db' and 'memcache'.
+// Type of IMAP indexes cache. Supported values: 'db', 'apc' and 'memcache'.
 $rcmail_config['imap_cache'] = null;
 
 // Enables messages cache. Only 'db' cache is supported.
diff --git a/program/include/rcmail.php b/program/include/rcmail.php
index e2ce1bf..98f3c3e 100644
--- a/program/include/rcmail.php
+++ b/program/include/rcmail.php
@@ -356,7 +356,7 @@
    * Initialize and get cache object
    *
    * @param string $name Cache identifier
-   * @param string $type Cache type ('db' or 'memcache')
+   * @param string $type Cache type ('db', 'apc' or 'memcache')
    *
    * @return rcube_cache Cache object
    */
diff --git a/program/include/rcube_cache.php b/program/include/rcube_cache.php
index dda0988..2fe5903 100644
--- a/program/include/rcube_cache.php
+++ b/program/include/rcube_cache.php
@@ -51,17 +51,22 @@
     /**
      * Object constructor.
      *
-     * @param string $type   Engine type ('db' or 'memcache')
+     * @param string $type   Engine type ('db' or 'memcache' or 'apc')
      * @param int    $userid User identifier
      * @param string $prefix Key name prefix
      */
     function __construct($type, $userid, $prefix='')
     {
         $rcmail = rcmail::get_instance();
+        $type   = strtolower($type);
     
-        if (strtolower($type) == 'memcache') {
+        if ($type == 'memcache') {
             $this->type = 'memcache';
             $this->db   = $rcmail->get_memcache();
+        }
+        else if ($type == 'apc') {
+            $this->type = 'apc';
+            $this->db   = function_exists('apc_exists'); // APC 3.1.4 required
         }
         else {
             $this->type = 'db';
@@ -208,7 +213,17 @@
         }
 
         if ($this->type == 'memcache') {
-            $data = $this->db->get($this->mc_key($key));
+            $data = $this->db->get($this->ckey($key));
+	        
+            if ($data) {
+                $this->cache_sums[$key] = md5($data);
+                $data = unserialize($data);
+            }
+            return $this->cache[$key] = $data;
+        }
+
+        if ($this->type == 'apc') {
+            $data = apc_fetch($this->ckey($key));
 	        
             if ($data) {
                 $this->cache_sums[$key] = md5($data);
@@ -263,11 +278,18 @@
         }
 
         if ($this->type == 'memcache') {
-            $key = $this->mc_key($key);
+            $key = $this->ckey($key);
             $result = $this->db->replace($key, $data, MEMCACHE_COMPRESSED);
             if (!$result)
                 $result = $this->db->set($key, $data, MEMCACHE_COMPRESSED);
             return $result;
+        }
+
+        if ($this->type == 'apc') {
+            $key = $this->ckey($key);
+            if (apc_exists($key))
+                apc_delete($key);
+            return apc_store($key, $data);
         }
 
         // update existing cache record
@@ -314,7 +336,11 @@
         }
 
         if ($this->type == 'memcache') {
-            return $this->db->delete($this->mc_key($key));
+            return $this->db->delete($this->ckey($key));
+        }
+
+        if ($this->type == 'apc') {
+            return apc_delete($this->ckey($key));
         }
 
         $this->db->query(
@@ -328,12 +354,12 @@
 
 
     /**
-     * Creates per-user Memcache key
+     * Creates per-user cache key (for memcache and apc)
      *
      * @param string $key Cache key
      * @access private
      */
-    private function mc_key($key)
+    private function ckey($key)
     {
         return sprintf('[%d]%s', $this->userid, $key);
     }
diff --git a/program/include/rcube_imap.php b/program/include/rcube_imap.php
index 413a672..882834e 100644
--- a/program/include/rcube_imap.php
+++ b/program/include/rcube_imap.php
@@ -3738,7 +3738,7 @@
     /**
      * Enable or disable indexes caching
      *
-     * @param boolean $type Cache type (memcache' or 'db')
+     * @param boolean $type Cache type (@see rcmail::get_cache)
      * @access public
      */
     function set_caching($type)

--
Gitblit v1.9.1