From cc95700b58f31f04470db8271a09d6e52ba9a63d Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Sun, 05 Feb 2006 10:38:51 -0500
Subject: [PATCH] Added message cache garbage collector

---
 program/include/rcube_shared.inc |   33 ++++++++++++++++
 program/include/session.inc      |    4 +
 program/include/main.inc         |   20 +++++++++
 config/main.inc.php.dist         |    4 ++
 4 files changed, 59 insertions(+), 2 deletions(-)

diff --git a/config/main.inc.php.dist b/config/main.inc.php.dist
index e1cb3ef..55d0138 100644
--- a/config/main.inc.php.dist
+++ b/config/main.inc.php.dist
@@ -22,6 +22,10 @@
 // this is recommended if the IMAP server does not run on the same machine
 $rcmail_config['enable_caching'] = TRUE;
 
+// lifetime of message cache
+// possible units: s, m, h, d, w
+$rcmail_config['message_cache_lifetime'] = '10d';
+
 // automatically create a new RoundCube user when log-in the first time.
 // a new user will be created once the IMAP login succeeds.
 // set to false if only registered users can use this service
diff --git a/program/include/main.inc b/program/include/main.inc
index 3c07836..ac612cd 100644
--- a/program/include/main.inc
+++ b/program/include/main.inc
@@ -697,6 +697,22 @@
   }
 
 
+// remove all expired message cache records
+function rcmail_message_cache_gc()
+  {
+  global $DB, $CONFIG;
+  
+  // no cache lifetime configured
+  if (empty($CONFIG['message_cache_lifetime']))
+    return;
+  
+  // get target timestamp
+  $ts = get_offset_time($CONFIG['message_cache_lifetime'], -1);
+  
+  $DB->query("DELETE FROM ".get_table_name('messages')."
+             WHERE  created < ".$DB->fromunixtime($ts));
+  }
+
 
 // convert a string from one charset to another
 // this function is not complete and not tested well
@@ -709,7 +725,7 @@
     return $str;
     
   // convert charset using iconv module  
-  if (0 && function_exists('iconv') && $from!='UTF-7' && $to!='UTF-7') {
+  if (function_exists('iconv') && $from!='UTF-7' && $to!='UTF-7') {
     return iconv($from, $to, $str);
     }
 
@@ -1506,6 +1522,8 @@
   }
 
 
+/****** debugging function ********/
+
 function rcube_timer()
   {
   list($usec, $sec) = explode(" ", microtime());
diff --git a/program/include/rcube_shared.inc b/program/include/rcube_shared.inc
index a36458b..fb200de 100644
--- a/program/include/rcube_shared.inc
+++ b/program/include/rcube_shared.inc
@@ -1353,4 +1353,37 @@
   }
 
 
+// create a unix timestamp with a specified offset from now
+function get_offset_time($offset_str, $factor=1)
+  {
+  if (preg_match('/^([0-9]+)\s*([smhdw])/i', $offset_str, $regs))
+    {
+    $amount = (int)$regs[1];
+    $unit = strtolower($regs[2]);
+    }
+  else
+    {
+    $amount = (int)$offset_str;
+    $unit = 's';
+    }
+    
+  $ts = mktime();
+  switch ($unit)
+    {
+    case 'w':
+      $amount *= 7;
+    case 'd':
+      $amount *= 24;
+    case 'h':
+      $amount *= 60;
+    case 'h':
+      $amount *= 60;
+    case 's':
+      $ts += $amount * $factor;
+    }
+
+  return $ts;
+  }
+
+
 ?>
\ No newline at end of file
diff --git a/program/include/session.inc b/program/include/session.inc
index f10a2b3..dc362f8 100644
--- a/program/include/session.inc
+++ b/program/include/session.inc
@@ -106,7 +106,6 @@
               $key);
 
   rcmail_clear_session_temp($key);
-
   return TRUE;
   }
 
@@ -142,6 +141,9 @@
   foreach ($a_exp_sessions as $key)
     rcmail_clear_session_temp($key);
 
+  // also run message cache GC
+  rcmail_message_cache_gc();
+
   return TRUE;
   }
 

--
Gitblit v1.9.1