From 50abd557184326f173357b50e7593dc3f452be91 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Sat, 25 May 2013 09:37:46 -0400
Subject: [PATCH] Added shared (cross-user) cache

---
 program/lib/Roundcube/rcube.php              |   31 ++
 CHANGELOG                                    |    1 
 program/lib/Roundcube/rcube_cache_shared.php |  544 +++++++++++++++++++++++++++++++++++++++++
 SQL/postgres/2013052500.sql                  |    8 
 SQL/mysql/2013052500.sql                     |    7 
 SQL/postgres.initial.sql                     |   16 +
 SQL/mysql.initial.sql                        |   13 
 SQL/sqlite.initial.sql                       |   64 ++--
 SQL/mssql.initial.sql                        |   19 +
 SQL/mssql/2013052500.sql                     |   17 +
 SQL/sqlite/2013052500.sql                    |    8 
 11 files changed, 688 insertions(+), 40 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 2709373..18f26ba 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,7 @@
 CHANGELOG Roundcube Webmail
 ===========================
 
+- Implemented shared cache (rcube_cache_shared)
 - Fix handling of comma when adding contact from contacts widget (#1489107)
 - Fix bug where a message was opened in both preview pane and new window on double-click (#1489122)
 - Fix fatal error when xdebug.max_nesting_level was exceeded in rcube_washtml (#1489110)
diff --git a/SQL/mssql.initial.sql b/SQL/mssql.initial.sql
index d43676f..23e7c7d 100644
--- a/SQL/mssql.initial.sql
+++ b/SQL/mssql.initial.sql
@@ -6,6 +6,13 @@
 ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
 GO
 
+CREATE TABLE [dbo].[cache_shared] (
+	[cache_key] [varchar] (255) COLLATE Latin1_General_CI_AI NOT NULL ,
+	[created] [datetime] NOT NULL ,
+	[data] [text] COLLATE Latin1_General_CI_AI NOT NULL 
+) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
+GO
+
 CREATE TABLE [dbo].[cache_index] (
 	[user_id] [int] NOT NULL ,
 	[mailbox] [varchar] (128) COLLATE Latin1_General_CI_AI NOT NULL ,
@@ -213,6 +220,16 @@
 CREATE  INDEX [IX_cache_created] ON [dbo].[cache]([created]) ON [PRIMARY]
 GO
 
+ALTER TABLE [dbo].[cache_shared] ADD 
+	CONSTRAINT [DF_cache_shared_created] DEFAULT (getdate()) FOR [created]
+GO
+
+CREATE  INDEX [IX_cache_shared_cache_key] ON [dbo].[cache_shared]([cache_key]) ON [PRIMARY]
+GO
+
+CREATE  INDEX [IX_cache_shared_created] ON [dbo].[cache_shared]([created]) ON [PRIMARY]
+GO
+
 ALTER TABLE [dbo].[cache_index] ADD 
 	CONSTRAINT [DF_cache_index_changed] DEFAULT (getdate()) FOR [changed],
 	CONSTRAINT [DF_cache_index_valid] DEFAULT ('0') FOR [valid]
@@ -371,6 +388,6 @@
     WHERE [contact_id] IN (SELECT [contact_id] FROM deleted)
 GO
 
-INSERT INTO [dbo].[system] ([name], [value]) VALUES ('roundcube-version', '2013042700')
+INSERT INTO [dbo].[system] ([name], [value]) VALUES ('roundcube-version', '2013052500')
 GO
 
\ No newline at end of file
diff --git a/SQL/mssql/2013052500.sql b/SQL/mssql/2013052500.sql
new file mode 100644
index 0000000..6a7e31d
--- /dev/null
+++ b/SQL/mssql/2013052500.sql
@@ -0,0 +1,17 @@
+CREATE TABLE [dbo].[cache_shared] (
+	[cache_key] [varchar] (255) COLLATE Latin1_General_CI_AI NOT NULL ,
+	[created] [datetime] NOT NULL ,
+	[data] [text] COLLATE Latin1_General_CI_AI NOT NULL 
+) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
+GO
+
+ALTER TABLE [dbo].[cache_shared] ADD 
+	CONSTRAINT [DF_cache_shared_created] DEFAULT (getdate()) FOR [created]
+GO
+
+CREATE  INDEX [IX_cache_shared_cache_key] ON [dbo].[cache_shared]([cache_key]) ON [PRIMARY]
+GO
+
+CREATE  INDEX [IX_cache_shared_created] ON [dbo].[cache_shared]([created]) ON [PRIMARY]
+GO
+
diff --git a/SQL/mysql.initial.sql b/SQL/mysql.initial.sql
index 5a86dd8..75268ca 100644
--- a/SQL/mysql.initial.sql
+++ b/SQL/mysql.initial.sql
@@ -45,6 +45,17 @@
 ) /*!40000 ENGINE=INNODB */ /*!40101 CHARACTER SET utf8 COLLATE utf8_general_ci */;
 
 
+-- Table structure for table `cache_shared`
+
+CREATE TABLE `cache_shared` (
+ `cache_key` varchar(255) /*!40101 CHARACTER SET ascii COLLATE ascii_general_ci */ NOT NULL ,
+ `created` datetime NOT NULL DEFAULT '1000-01-01 00:00:00',
+ `data` longtext NOT NULL,
+ INDEX `created_index` (`created`),
+ INDEX `cache_key_index` (`cache_key`)
+) /*!40000 ENGINE=INNODB */ /*!40101 CHARACTER SET utf8 COLLATE utf8_general_ci */;
+
+
 -- Table structure for table `cache_index`
 
 CREATE TABLE `cache_index` (
@@ -196,4 +207,4 @@
 
 /*!40014 SET FOREIGN_KEY_CHECKS=1 */;
 
-INSERT INTO system (name, value) VALUES ('roundcube-version', '2013042700');
+INSERT INTO system (name, value) VALUES ('roundcube-version', '2013052500');
diff --git a/SQL/mysql/2013052500.sql b/SQL/mysql/2013052500.sql
new file mode 100644
index 0000000..b5f72b8
--- /dev/null
+++ b/SQL/mysql/2013052500.sql
@@ -0,0 +1,7 @@
+CREATE TABLE `cache_shared` (
+ `cache_key` varchar(255) /*!40101 CHARACTER SET ascii COLLATE ascii_general_ci */ NOT NULL,
+ `created` datetime NOT NULL DEFAULT '1000-01-01 00:00:00',
+ `data` longtext NOT NULL,
+ INDEX `created_index` (`created`),
+ INDEX `cache_key_index` (`cache_key`)
+) /*!40000 ENGINE=INNODB */ /*!40101 CHARACTER SET utf8 COLLATE utf8_general_ci */;
diff --git a/SQL/postgres.initial.sql b/SQL/postgres.initial.sql
index f3b368f..c54f05f 100644
--- a/SQL/postgres.initial.sql
+++ b/SQL/postgres.initial.sql
@@ -174,6 +174,20 @@
 CREATE INDEX cache_created_idx ON "cache" (created);
 
 --
+-- Table "cache_shared"
+-- Name: cache_shared; Type: TABLE; Schema: public; Owner: postgres
+--
+
+CREATE TABLE "cache_shared" (
+    cache_key varchar(255) NOT NULL,
+    created timestamp with time zone DEFAULT now() NOT NULL,
+    data text NOT NULL
+);
+
+CREATE INDEX cache_shared_cache_key_idx ON "cache_shared" (cache_key);
+CREATE INDEX cache_shared_created_idx ON "cache_shared" (created);
+
+--
 -- Table "cache_index"
 -- Name: cache_index; Type: TABLE; Schema: public; Owner: postgres
 --
@@ -274,4 +288,4 @@
     value text
 );
 
-INSERT INTO system (name, value) VALUES ('roundcube-version', '2013042700');
+INSERT INTO system (name, value) VALUES ('roundcube-version', '2013052500');
diff --git a/SQL/postgres/2013052500.sql b/SQL/postgres/2013052500.sql
new file mode 100644
index 0000000..471e571
--- /dev/null
+++ b/SQL/postgres/2013052500.sql
@@ -0,0 +1,8 @@
+CREATE TABLE "cache_shared" (
+    cache_key varchar(255) NOT NULL,
+    created timestamp with time zone DEFAULT now() NOT NULL,
+    data text NOT NULL
+);
+
+CREATE INDEX cache_shared_cache_key_idx ON "cache_shared" (cache_key);
+CREATE INDEX cache_shared_created_idx ON "cache_shared" (created);
diff --git a/SQL/sqlite.initial.sql b/SQL/sqlite.initial.sql
index d9bafb7..1b5b62b 100644
--- a/SQL/sqlite.initial.sql
+++ b/SQL/sqlite.initial.sql
@@ -1,23 +1,6 @@
 -- Roundcube Webmail initial database structure
 
 -- 
--- Table structure for table cache
--- 
-
-CREATE TABLE cache (
-  user_id integer NOT NULL default 0,
-  cache_key varchar(128) NOT NULL default '',
-  created datetime NOT NULL default '0000-00-00 00:00:00',
-  data text NOT NULL
-);
-
-CREATE INDEX ix_cache_user_cache_key ON cache(user_id, cache_key);
-CREATE INDEX ix_cache_created ON cache(created);
-
-
--- --------------------------------------------------------
-
--- 
 -- Table structure for table contacts and related
 -- 
 
@@ -57,9 +40,6 @@
 
 CREATE INDEX ix_contactgroupmembers_contact_id ON contactgroupmembers (contact_id);
 
-
--- --------------------------------------------------------
-
 -- 
 -- Table structure for table identities
 -- 
@@ -82,9 +62,6 @@
 CREATE INDEX ix_identities_user_id ON identities(user_id, del);
 CREATE INDEX ix_identities_email ON identities(email, del);
 
-
--- --------------------------------------------------------
-
 -- 
 -- Table structure for table users
 -- 
@@ -101,8 +78,6 @@
 
 CREATE UNIQUE INDEX ix_users_username ON users(username, mail_host);
 
--- --------------------------------------------------------
-
 -- 
 -- Table structure for table session
 -- 
@@ -117,8 +92,6 @@
 
 CREATE INDEX ix_session_changed ON session (changed);
 
--- --------------------------------------------------------
-
 --
 -- Table structure for table dictionary
 --
@@ -130,8 +103,6 @@
 );
 
 CREATE UNIQUE INDEX ix_dictionary_user_language ON dictionary (user_id, "language");
-
--- --------------------------------------------------------
 
 --
 -- Table structure for table searches
@@ -147,7 +118,32 @@
 
 CREATE UNIQUE INDEX ix_searches_user_type_name ON searches (user_id, type, name);
 
--- --------------------------------------------------------
+-- 
+-- Table structure for table cache
+-- 
+
+CREATE TABLE cache (
+  user_id integer NOT NULL default 0,
+  cache_key varchar(128) NOT NULL default '',
+  created datetime NOT NULL default '0000-00-00 00:00:00',
+  data text NOT NULL
+);
+
+CREATE INDEX ix_cache_user_cache_key ON cache(user_id, cache_key);
+CREATE INDEX ix_cache_created ON cache(created);
+
+-- 
+-- Table structure for table cache_shared
+-- 
+
+CREATE TABLE cache_shared (
+  cache_key varchar(255) NOT NULL,
+  created datetime NOT NULL default '0000-00-00 00:00:00',
+  data text NOT NULL
+);
+
+CREATE INDEX ix_cache_shared_cache_key ON cache_shared(cache_key);
+CREATE INDEX ix_cache_shared_created ON cache_shared(created);
 
 --
 -- Table structure for table cache_index
@@ -164,8 +160,6 @@
 
 CREATE INDEX ix_cache_index_changed ON cache_index (changed);
 
--- --------------------------------------------------------
-
 --
 -- Table structure for table cache_thread
 --
@@ -179,8 +173,6 @@
 );
 
 CREATE INDEX ix_cache_thread_changed ON cache_thread (changed);
-
--- --------------------------------------------------------
 
 --
 -- Table structure for table cache_messages
@@ -198,8 +190,6 @@
 
 CREATE INDEX ix_cache_messages_changed ON cache_messages (changed);
 
--- --------------------------------------------------------
-
 --
 -- Table structure for table system
 --
@@ -209,4 +199,4 @@
   value text NOT NULL
 );
 
-INSERT INTO system (name, value) VALUES ('roundcube-version', '2013042700');
+INSERT INTO system (name, value) VALUES ('roundcube-version', '2013052500');
diff --git a/SQL/sqlite/2013052500.sql b/SQL/sqlite/2013052500.sql
new file mode 100644
index 0000000..19ae1b1
--- /dev/null
+++ b/SQL/sqlite/2013052500.sql
@@ -0,0 +1,8 @@
+CREATE TABLE cache_shared (
+  cache_key varchar(255) NOT NULL,
+  created datetime NOT NULL default '0000-00-00 00:00:00',
+  data text NOT NULL
+);
+
+CREATE INDEX ix_cache_shared_cache_key ON cache_shared(cache_key);
+CREATE INDEX ix_cache_shared_created ON cache_shared(created);
diff --git a/program/lib/Roundcube/rcube.php b/program/lib/Roundcube/rcube.php
index eea2fde..a57cad5 100644
--- a/program/lib/Roundcube/rcube.php
+++ b/program/lib/Roundcube/rcube.php
@@ -258,6 +258,37 @@
 
 
     /**
+     * Initialize and get shared cache object
+     *
+     * @param string $name   Cache identifier
+     * @param bool   $packed Enables/disables data serialization
+     *
+     * @return rcube_cache_shared Cache object
+     */
+    public function get_cache_shared($name, $packed=true)
+    {
+        $shared_name = "shared_$name";
+
+        if (!isset($this->caches[$shared_name])) {
+            $opt  = strtolower($name) . '_cache';
+            $type = $this->config->get($opt);
+            $ttl  = $this->config->get($opt . '_ttl');
+
+            if (!$type) {
+                $type = $this->config->get('shared_cache');
+            }
+            if ($ttl === null) {
+                $ttl = $this->config->get('shared_cache_ttl');
+            }
+
+            $this->caches[$shared_name] = new rcube_cache_shared($type, $name, $ttl, $packed);
+        }
+
+        return $this->caches[$shared_name];
+    }
+
+
+    /**
      * Create SMTP object and connect to server
      *
      * @param boolean True if connection should be established
diff --git a/program/lib/Roundcube/rcube_cache_shared.php b/program/lib/Roundcube/rcube_cache_shared.php
new file mode 100644
index 0000000..bef9e85
--- /dev/null
+++ b/program/lib/Roundcube/rcube_cache_shared.php
@@ -0,0 +1,544 @@
+<?php
+
+/*
+ +-----------------------------------------------------------------------+
+ | This file is part of the Roundcube Webmail client                     |
+ | Copyright (C) 2011-2013, The Roundcube Dev Team                       |
+ | Copyright (C) 2011-2013, Kolab Systems AG                             |
+ |                                                                       |
+ | Licensed under the GNU General Public License version 3 or            |
+ | any later version with exceptions for skins & plugins.                |
+ | See the README file for a full license statement.                     |
+ |                                                                       |
+ | PURPOSE:                                                              |
+ |   Shared (cross-user) caching engine                                  |
+ +-----------------------------------------------------------------------+
+ | Author: Thomas Bruederli <roundcube@gmail.com>                        |
+ | Author: Aleksander Machniak <alec@alec.pl>                            |
+ +-----------------------------------------------------------------------+
+*/
+
+
+/**
+ * Interface class for accessing Roundcube shared cache
+ *
+ * @package    Framework
+ * @subpackage Cache
+ * @author     Thomas Bruederli <roundcube@gmail.com>
+ * @author     Aleksander Machniak <alec@alec.pl>
+ */
+class rcube_cache
+{
+    /**
+     * Instance of database handler
+     *
+     * @var rcube_db|Memcache|bool
+     */
+    private $db;
+    private $type;
+    private $prefix;
+    private $ttl;
+    private $packed;
+    private $index;
+    private $table;
+    private $cache         = array();
+    private $cache_changes = array();
+    private $cache_sums    = array();
+
+
+    /**
+     * Object constructor.
+     *
+     * @param string $type   Engine type ('db' or 'memcache' or 'apc')
+     * @param string $prefix Key name prefix
+     * @param string $ttl    Expiration time of memcache/apc items
+     * @param bool   $packed Enables/disabled data serialization.
+     *                       It's possible to disable data serialization if you're sure
+     *                       stored data will be always a safe string
+     */
+    function __construct($type, $prefix='', $ttl=0, $packed=true)
+    {
+        $rcube = rcube::get_instance();
+        $type  = strtolower($type);
+
+        if ($type == 'memcache') {
+            $this->type = 'memcache';
+            $this->db   = $rcube->get_memcache();
+        }
+        else if ($type == 'apc') {
+            $this->type = 'apc';
+            $this->db   = function_exists('apc_exists'); // APC 3.1.4 required
+        }
+        else {
+            $this->type  = 'db';
+            $this->db    = $rcube->get_dbh();
+            $this->table = $this->db->table_name('cache_shared');
+        }
+
+        // convert ttl string to seconds
+        $ttl = get_offset_sec($ttl);
+        if ($ttl > 2592000) $ttl = 2592000;
+
+        $this->ttl       = $ttl;
+        $this->packed    = $packed;
+        $this->prefix    = $prefix;
+    }
+
+
+    /**
+     * Returns cached value.
+     *
+     * @param string $key Cache key name
+     *
+     * @return mixed Cached value
+     */
+    function get($key)
+    {
+        if (!array_key_exists($key, $this->cache)) {
+            return $this->read_record($key);
+        }
+
+        return $this->cache[$key];
+    }
+
+
+    /**
+     * Sets (add/update) value in cache.
+     *
+     * @param string $key  Cache key name
+     * @param mixed  $data Cache data
+     */
+    function set($key, $data)
+    {
+        $this->cache[$key]         = $data;
+        $this->cache_changed       = true;
+        $this->cache_changes[$key] = true;
+    }
+
+
+    /**
+     * Returns cached value without storing it in internal memory.
+     *
+     * @param string $key Cache key name
+     *
+     * @return mixed Cached value
+     */
+    function read($key)
+    {
+        if (array_key_exists($key, $this->cache)) {
+            return $this->cache[$key];
+        }
+
+        return $this->read_record($key, true);
+    }
+
+
+    /**
+     * Sets (add/update) value in cache and immediately saves
+     * it in the backend, no internal memory will be used.
+     *
+     * @param string $key  Cache key name
+     * @param mixed  $data Cache data
+     *
+     * @param boolean True on success, False on failure
+     */
+    function write($key, $data)
+    {
+        return $this->write_record($key, $this->packed ? serialize($data) : $data);
+    }
+
+
+    /**
+     * Clears the cache.
+     *
+     * @param string  $key         Cache key name or pattern
+     * @param boolean $prefix_mode Enable it to clear all keys starting
+     *                             with prefix specified in $key
+     */
+    function remove($key=null, $prefix_mode=false)
+    {
+        // Remove all keys
+        if ($key === null) {
+            $this->cache         = array();
+            $this->cache_changed = false;
+            $this->cache_changes = array();
+            $this->cache_sums    = array();
+        }
+        // Remove keys by name prefix
+        else if ($prefix_mode) {
+            foreach (array_keys($this->cache) as $k) {
+                if (strpos($k, $key) === 0) {
+                    $this->cache[$k] = null;
+                    $this->cache_changes[$k] = false;
+                    unset($this->cache_sums[$k]);
+                }
+            }
+        }
+        // Remove one key by name
+        else {
+            $this->cache[$key] = null;
+            $this->cache_changes[$key] = false;
+            unset($this->cache_sums[$key]);
+        }
+
+        // Remove record(s) from the backend
+        $this->remove_record($key, $prefix_mode);
+    }
+
+
+    /**
+     * Remove cache records older than ttl
+     */
+    function expunge()
+    {
+        if ($this->type == 'db' && $this->db) {
+            $this->db->query(
+                "DELETE FROM " . $this->table
+                . " WHERE cache_key LIKE ?"
+                . " AND " . $this->db->unixtimestamp('created') . " < ?",
+                $this->prefix . '.%',
+                time() - $this->ttl);
+        }
+    }
+
+
+    /**
+     * Writes the cache back to the DB.
+     */
+    function close()
+    {
+        if (!$this->cache_changed) {
+            return;
+        }
+
+        foreach ($this->cache as $key => $data) {
+            // The key has been used
+            if ($this->cache_changes[$key]) {
+                // Make sure we're not going to write unchanged data
+                // by comparing current md5 sum with the sum calculated on DB read
+                $data = $this->packed ? serialize($data) : $data;
+
+                if (!$this->cache_sums[$key] || $this->cache_sums[$key] != md5($data)) {
+                    $this->write_record($key, $data);
+                }
+            }
+        }
+
+        $this->write_index();
+    }
+
+
+    /**
+     * Reads cache entry.
+     *
+     * @param string  $key     Cache key name
+     * @param boolean $nostore Enable to skip in-memory store
+     *
+     * @return mixed Cached value
+     */
+    private function read_record($key, $nostore=false)
+    {
+        if (!$this->db) {
+            return null;
+        }
+
+        if ($this->type != 'db') {
+            if ($this->type == 'memcache') {
+                $data = $this->db->get($this->ckey($key));
+            }
+            else if ($this->type == 'apc') {
+                $data = apc_fetch($this->ckey($key));
+            }
+
+            if ($data) {
+                $md5sum = md5($data);
+                $data   = $this->packed ? unserialize($data) : $data;
+
+                if ($nostore) {
+                    return $data;
+                }
+
+                $this->cache_sums[$key] = $md5sum;
+                $this->cache[$key]      = $data;
+            }
+            else {
+                $this->cache[$key] = null;
+            }
+        }
+        else {
+            $sql_result = $this->db->limitquery(
+                "SELECT data, cache_key".
+                " FROM " . $this->table .
+                " WHERE cache_key = ?".
+                // for better performance we allow more records for one key
+                // get the newer one
+                " ORDER BY created DESC",
+                0, 1, $this->prefix . '.' . $key);
+
+            if ($sql_arr = $this->db->fetch_assoc($sql_result)) {
+                $md5sum = $sql_arr['data'] ? md5($sql_arr['data']) : null;
+                if ($sql_arr['data']) {
+                    $data = $this->packed ? unserialize($sql_arr['data']) : $sql_arr['data'];
+                }
+
+                if ($nostore) {
+                    return $data;
+                }
+
+                $this->cache[$key]      = $data;
+                $this->cache_sums[$key] = $md5sum;
+            }
+            else {
+                $this->cache[$key] = null;
+            }
+        }
+
+        return $this->cache[$key];
+    }
+
+
+    /**
+     * Writes single cache record into DB.
+     *
+     * @param string $key  Cache key name
+     * @param mxied  $data Serialized cache data 
+     *
+     * @param boolean True on success, False on failure
+     */
+    private function write_record($key, $data)
+    {
+        if (!$this->db) {
+            return false;
+        }
+
+        if ($this->type == 'memcache' || $this->type == 'apc') {
+            return $this->add_record($this->ckey($key), $data);
+        }
+
+        $key_exists = array_key_exists($key, $this->cache_sums);
+        $key        = $this->prefix . '.' . $key;
+
+        // Remove NULL rows (here we don't need to check if the record exist)
+        if ($data == 'N;') {
+            $this->db->query("DELETE FROM " . $this->table . " WHERE cache_key = ?", $key);
+            return true;
+        }
+
+        // update existing cache record
+        if ($key_exists) {
+            $result = $this->db->query(
+                "UPDATE " . $this->table .
+                " SET created = " . $this->db->now() . ", data = ?" .
+                " WHERE cache_key = ?",
+                $data, $key);
+        }
+        // add new cache record
+        else {
+            // for better performance we allow more records for one key
+            // so, no need to check if record exist (see rcube_cache::read_record())
+            $result = $this->db->query(
+                "INSERT INTO ".$this->table.
+                " (created, cache_key, data)".
+                " VALUES (".$this->db->now().", ?, ?)",
+                $key, $data);
+        }
+
+        return $this->db->affected_rows($result);
+    }
+
+
+    /**
+     * Deletes the cache record(s).
+     *
+     * @param string  $key         Cache key name or pattern
+     * @param boolean $prefix_mode Enable it to clear all keys starting
+     *                             with prefix specified in $key
+     */
+    private function remove_record($key=null, $prefix_mode=false)
+    {
+        if (!$this->db) {
+            return;
+        }
+
+        if ($this->type != 'db') {
+            $this->load_index();
+
+            // Remove all keys
+            if ($key === null) {
+                foreach ($this->index as $key) {
+                    $this->delete_record($key, false);
+                }
+                $this->index = array();
+            }
+            // Remove keys by name prefix
+            else if ($prefix_mode) {
+                foreach ($this->index as $k) {
+                    if (strpos($k, $key) === 0) {
+                        $this->delete_record($k);
+                    }
+                }
+            }
+            // Remove one key by name
+            else {
+                $this->delete_record($key);
+            }
+
+            return;
+        }
+
+        // Remove all keys (in specified cache)
+        if ($key === null) {
+            $where = " WHERE cache_key LIKE " . $this->db->quote($this->prefix.'.%');
+        }
+        // Remove keys by name prefix
+        else if ($prefix_mode) {
+            $where = " WHERE cache_key LIKE " . $this->db->quote($this->prefix.'.'.$key.'%');
+        }
+        // Remove one key by name
+        else {
+            $where = " WHERE cache_key = " . $this->db->quote($this->prefix.'.'.$key);
+        }
+
+        $this->db->query("DELETE FROM " . $this->table . $where);
+    }
+
+
+    /**
+     * Adds entry into memcache/apc DB.
+     *
+     * @param string  $key   Cache key name
+     * @param mxied   $data  Serialized cache data
+     * @param bollean $index Enables immediate index update
+     *
+     * @param boolean True on success, False on failure
+     */
+    private function add_record($key, $data, $index=false)
+    {
+        if ($this->type == 'memcache') {
+            $result = $this->db->replace($key, $data, MEMCACHE_COMPRESSED, $this->ttl);
+            if (!$result) {
+                $result = $this->db->set($key, $data, MEMCACHE_COMPRESSED, $this->ttl);
+            }
+        }
+        else if ($this->type == 'apc') {
+            if (apc_exists($key)) {
+                apc_delete($key);
+            }
+            $result = apc_store($key, $data, $this->ttl);
+        }
+
+        // Update index
+        if ($index && $result) {
+            $this->load_index();
+
+            if (array_search($key, $this->index) === false) {
+                $this->index[] = $key;
+                $data = serialize($this->index);
+                $this->add_record($this->ikey(), $data);
+            }
+        }
+
+        return $result;
+    }
+
+
+    /**
+     * Deletes entry from memcache/apc DB.
+     */
+    private function delete_record($key, $index=true)
+    {
+        if ($this->type == 'memcache') {
+            // #1488592: use 2nd argument
+            $this->db->delete($this->ckey($key), 0);
+        }
+        else {
+            apc_delete($this->ckey($key));
+        }
+
+        if ($index) {
+            if (($idx = array_search($key, $this->index)) !== false) {
+                unset($this->index[$idx]);
+            }
+        }
+    }
+
+
+    /**
+     * Writes the index entry into memcache/apc DB.
+     */
+    private function write_index()
+    {
+        if (!$this->db) {
+            return;
+        }
+
+        if ($this->type == 'db') {
+            return;
+        }
+
+        $this->load_index();
+
+        // Make sure index contains new keys
+        foreach ($this->cache as $key => $value) {
+            if ($value !== null) {
+                if (array_search($key, $this->index) === false) {
+                    $this->index[] = $key;
+                }
+            }
+        }
+
+        $data = serialize($this->index);
+        $this->add_record($this->ikey(), $data);
+    }
+
+
+    /**
+     * Gets the index entry from memcache/apc DB.
+     */
+    private function load_index()
+    {
+        if (!$this->db) {
+            return;
+        }
+
+        if ($this->index !== null) {
+            return;
+        }
+
+        $index_key = $this->ikey();
+
+        if ($this->type == 'memcache') {
+            $data = $this->db->get($index_key);
+        }
+        else if ($this->type == 'apc') {
+            $data = apc_fetch($index_key);
+        }
+
+        $this->index = $data ? unserialize($data) : array();
+    }
+
+
+    /**
+     * Creates cache key name (for memcache and apc)
+     *
+     * @param string $key Cache key name
+     *
+     * @return string Cache key
+     */
+    private function ckey($key)
+    {
+        return $this->prefix . ':' . $key;
+    }
+
+
+    /**
+     * Creates index cache key name (for memcache and apc)
+     *
+     * @return string Cache key
+     */
+    private function ikey()
+    {
+        // This way each cache will have its own index
+        return $this->prefix . 'INDEX';
+    }
+}

--
Gitblit v1.9.1