From 92459da0e298fd1eeb57ec4cc695aba073f95e31 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Tue, 12 May 2015 03:12:32 -0400
Subject: [PATCH] Fix possible memcache/apc cache data consistency issues (#1490390)
---
CHANGELOG | 1
program/lib/Roundcube/rcube_cache_shared.php | 28 ++++++--------
program/lib/Roundcube/rcube_cache.php | 36 ++++++-----------
3 files changed, 26 insertions(+), 39 deletions(-)
diff --git a/CHANGELOG b/CHANGELOG
index 9cb92f4..ccd3de8 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -17,6 +17,7 @@
- Fix bug where preview_pane setting wasn't always saved into user preferences (#1490362)
- Fix bug where messages count was not updated after message move/delete with skip_deleted=false (#1490372)
- Fix security issue in contact photo handling (#1490379)
+- Fix possible memcache/apc cache data consistency issues (#1490390)
RELEASE 1.1.1
-------------
diff --git a/program/lib/Roundcube/rcube_cache.php b/program/lib/Roundcube/rcube_cache.php
index 36b5504..d4a9188 100644
--- a/program/lib/Roundcube/rcube_cache.php
+++ b/program/lib/Roundcube/rcube_cache.php
@@ -260,7 +260,15 @@
}
if ($this->type != 'db') {
- if ($this->type == 'memcache') {
+ $this->load_index();
+
+ // Consistency check (#1490390)
+ if (!in_array($key, $this->index)) {
+ // we always check if the key exist in the index
+ // to have data in consistent state. Keeping the index consistent
+ // is needed for keys delete operation when we delete all keys or by prefix.
+ }
+ else if ($this->type == 'memcache') {
$data = $this->db->get($this->ckey($key));
}
else if ($this->type == 'apc') {
@@ -403,13 +411,7 @@
}
// Remove keys by name prefix
else if ($prefix_mode) {
- // handle data inconsistency: it may happen that index
- // contains not all existing cache entries, here we could
- // handle at least these that were used before the index was read
- $index = array_merge($this->index, array_keys($this->cache));
- $index = array_unique($index);
-
- foreach ($index as $k) {
+ foreach ($this->index as $k) {
if (strpos($k, $key) === 0) {
$this->delete_record($k);
}
@@ -445,13 +447,12 @@
/**
* 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 string $key Cache key name
+ * @param mixed $data Serialized cache data
*
* @param boolean True on success, False on failure
*/
- private function add_record($key, $data, $index=false)
+ private function add_record($key, $data)
{
if ($this->type == 'memcache') {
$result = $this->db->replace($key, $data, MEMCACHE_COMPRESSED, $this->ttl);
@@ -462,17 +463,6 @@
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;
diff --git a/program/lib/Roundcube/rcube_cache_shared.php b/program/lib/Roundcube/rcube_cache_shared.php
index 3f0f20e..fa29b54 100644
--- a/program/lib/Roundcube/rcube_cache_shared.php
+++ b/program/lib/Roundcube/rcube_cache_shared.php
@@ -255,7 +255,15 @@
}
if ($this->type != 'db') {
- if ($this->type == 'memcache') {
+ $this->load_index();
+
+ // Consistency check (#1490390)
+ if (!in_array($key, $this->index)) {
+ // we always check if the key exist in the index
+ // to have data in consistent state. Keeping the index consistent
+ // is needed for keys delete operation when we delete all keys or by prefix.
+ }
+ else if ($this->type == 'memcache') {
$data = $this->db->get($this->ckey($key));
}
else if ($this->type == 'apc') {
@@ -426,13 +434,12 @@
/**
* 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 string $key Cache key name
+ * @param mixed $data Serialized cache data
*
* @param boolean True on success, False on failure
*/
- private function add_record($key, $data, $index=false)
+ private function add_record($key, $data)
{
if ($this->type == 'memcache') {
$result = $this->db->replace($key, $data, MEMCACHE_COMPRESSED, $this->ttl);
@@ -445,17 +452,6 @@
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;
--
Gitblit v1.9.1