From b120d42f5b944a7b4ff801b82f048b71ec2bd288 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Thu, 14 May 2015 04:41:00 -0400
Subject: [PATCH] Improved cache index changes detection (again)

---
 program/lib/Roundcube/rcube_cache_shared.php |   49 +++++++++++++-----------
 program/lib/Roundcube/rcube_cache.php        |   55 +++++++++++++++------------
 2 files changed, 57 insertions(+), 47 deletions(-)

diff --git a/program/lib/Roundcube/rcube_cache.php b/program/lib/Roundcube/rcube_cache.php
index 649dd93..c74c50c 100644
--- a/program/lib/Roundcube/rcube_cache.php
+++ b/program/lib/Roundcube/rcube_cache.php
@@ -355,8 +355,16 @@
             $result = $this->add_record($this->ckey($key), $data);
 
             // make sure index will be updated
-            if ($result && !array_key_exists($key, $this->cache_sums)) {
-                $this->cache_sums[$key] = true;
+            if ($result) {
+                if (!array_key_exists($key, $this->cache_sums)) {
+                    $this->cache_sums[$key] = true;
+                }
+
+                $this->load_index();
+
+                if (!$this->index_changed && !in_array($key, $this->index)) {
+                    $this->index_changed = true;
+                }
             }
 
             return $result;
@@ -420,22 +428,29 @@
             // Remove all keys
             if ($key === null) {
                 foreach ($this->index as $key) {
-                    $this->delete_record($key, false);
+                    $this->delete_record($this->ckey($key));
                 }
+
                 $this->index = array();
             }
             // Remove keys by name prefix
             else if ($prefix_mode) {
-                foreach ($this->index as $k) {
+                foreach ($this->index as $idx => $k) {
                     if (strpos($k, $key) === 0) {
-                        $this->delete_record($k);
+                        $this->delete_record($this->ckey($k));
+                        unset($this->index[$idx]);
                     }
                 }
             }
             // Remove one key by name
             else {
-                $this->delete_record($key);
+                $this->delete_record($this->ckey($key));
+                if (($idx = array_search($key, $this->index)) !== false) {
+                    unset($this->index[$idx]);
+                }
             }
+
+            $this->index_changed = true;
 
             return;
         }
@@ -488,42 +503,32 @@
             $this->debug('set', $key, $data, $result);
         }
 
-        if ($result) {
-            $this->index_changed = true;
-        }
-
         return $result;
     }
 
 
     /**
      * Deletes entry from memcache/apc DB.
+     *
+     * @param string $key Cache key name
+     *
+     * @param boolean True on success, False on failure
      */
-    private function delete_record($key, $index=true)
+    private function delete_record($key)
     {
-        $ckey = $this->ckey($key);
-
         if ($this->type == 'memcache') {
             // #1488592: use 2nd argument
-            $result = $this->db->delete($ckey, 0);
+            $result = $this->db->delete($key, 0);
         }
         else {
-            $result = apc_delete($ckey);
+            $result = apc_delete($key);
         }
 
         if ($this->debug) {
-            $this->debug('delete', $ckey, null, $result);
+            $this->debug('delete', $key, null, $result);
         }
 
-        if ($result) {
-            $this->index_changed = true;
-        }
-
-        if ($index) {
-            if (($idx = array_search($key, $this->index)) !== false) {
-                unset($this->index[$idx]);
-            }
-        }
+        return $result;
     }
 
 
diff --git a/program/lib/Roundcube/rcube_cache_shared.php b/program/lib/Roundcube/rcube_cache_shared.php
index 21a99c0..bd5fb6e 100644
--- a/program/lib/Roundcube/rcube_cache_shared.php
+++ b/program/lib/Roundcube/rcube_cache_shared.php
@@ -349,8 +349,16 @@
             $result = $this->add_record($this->ckey($key), $data);
 
             // make sure index will be updated
-            if ($result && !array_key_exists($key, $this->cache_sums)) {
-                $this->cache_sums[$key] = true;
+            if ($result) {
+                if (!array_key_exists($key, $this->cache_sums)) {
+                    $this->cache_sums[$key] = true;
+                }
+
+                $this->load_index();
+
+                if (!$this->index_changed && !in_array($key, $this->index)) {
+                    $this->index_changed = true;
+                }
             }
 
             return $result;
@@ -409,22 +417,29 @@
             // Remove all keys
             if ($key === null) {
                 foreach ($this->index as $key) {
-                    $this->delete_record($key, false);
+                    $this->delete_record($this->ckey($key));
                 }
+
                 $this->index = array();
             }
             // Remove keys by name prefix
             else if ($prefix_mode) {
-                foreach ($this->index as $k) {
+                foreach ($this->index as $idx => $k) {
                     if (strpos($k, $key) === 0) {
-                        $this->delete_record($k);
+                        $this->delete_record($this->ckey($k));
+                        unset($this->index[$idx]);
                     }
                 }
             }
             // Remove one key by name
             else {
-                $this->delete_record($key);
+                $this->delete_record($this->ckey($key));
+                if (($idx = array_search($key, $this->index)) !== false) {
+                    unset($this->index[$idx]);
+                }
             }
+
+            $this->index_changed = true;
 
             return;
         }
@@ -475,21 +490,19 @@
             $this->debug('set', $key, $data, $result);
         }
 
-        if ($result) {
-            $this->index_changed = true;
-        }
-
         return $result;
     }
 
 
     /**
      * Deletes entry from memcache/apc DB.
+     *
+     * @param string $key Cache key name
+     *
+     * @param boolean True on success, False on failure
      */
-    private function delete_record($key, $index=true)
+    private function delete_record($key)
     {
-        $ckey = $this->ckey($key);
-
         if ($this->type == 'memcache') {
             // #1488592: use 2nd argument
             $result = $this->db->delete($ckey, 0);
@@ -502,15 +515,7 @@
             $this->debug('delete', $ckey, null, $result);
         }
 
-        if ($result) {
-            $this->index_changed = true;
-        }
-
-        if ($index) {
-            if (($idx = array_search($key, $this->index)) !== false) {
-                unset($this->index[$idx]);
-            }
-        }
+        return $result;
     }
 
 

--
Gitblit v1.9.1