From fe91e2f31de584f0178e06d9b03c587cec700f8e Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Wed, 24 Aug 2011 06:41:33 -0400
Subject: [PATCH] Improve memcache connection and failure handling; add debug option to trace memcache activity for session storage

---
 program/include/rcube_session.php |   21 ++++++++++++++++-----
 1 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/program/include/rcube_session.php b/program/include/rcube_session.php
index bface28..14475ca 100644
--- a/program/include/rcube_session.php
+++ b/program/include/rcube_session.php
@@ -55,6 +55,7 @@
     $this->start = microtime(true);
     $this->ip = $_SERVER['REMOTE_ADDR'];
     $this->logging = $config->get('log_session', false);
+    $this->mc_debug = $config->get('memcache_debug', false);
 
     $lifetime = $config->get('session_lifetime', 1) * 60;
     $this->set_lifetime($lifetime);
@@ -255,8 +256,9 @@
    */
   public function mc_read($key)
   {
-    if ($value = $this->memcache->get($key)) {
-      $arr = unserialize($value);
+    $value = $this->memcache->get($key);
+    if ($this->mc_debug) write_log('memcache', "get($key): " . strlen($value));
+    if ($value && ($arr = unserialize($value))) {
       $this->changed = $arr['changed'];
       $this->ip      = $arr['ip'];
       $this->vars    = $arr['vars'];
@@ -289,8 +291,15 @@
 
     $newvars = $oldvars !== false ? $this->_fixvars($vars, $oldvars) : $vars;
     
-    if ($newvars !== $oldvars || $ts - $this->changed > $this->lifetime / 2)
-      return $this->memcache->set($key, serialize(array('changed' => time(), 'ip' => $this->ip, 'vars' => $newvars)), MEMCACHE_COMPRESSED, $this->lifetime);
+    if ($newvars !== $oldvars || $ts - $this->changed > $this->lifetime / 2) {
+      $value = serialize(array('changed' => time(), 'ip' => $this->ip, 'vars' => $newvars));
+      $ret = $this->memcache->set($key, $value, MEMCACHE_COMPRESSED, $this->lifetime);
+      if ($this->mc_debug) {
+        write_log('memcache', "set($key): " . strlen($value) . ": " . ($ret ? 'OK' : 'ERR'));
+        write_log('memcache', "... get($key): " . strlen($this->memcache->get($key)));
+      }
+      return $ret;
+    }
     
     return true;
   }
@@ -303,7 +312,9 @@
    */
   public function mc_destroy($key)
   {
-    return $this->memcache->delete($key);
+    $ret = $this->memcache->delete($key);
+    if ($this->mc_debug) write_log('memcache', "delete($key): " . ($ret ? 'OK' : 'ERR'));
+    return $ret;
   }
 
 

--
Gitblit v1.9.1