[Svn] r6103 - trunk/roundcubemail/program/include

trac at roundcube.net trac at roundcube.net
Thu Apr 19 09:42:20 CEST 2012


Author: alec
Date: 2012-04-19 02:42:19 -0500 (Thu, 19 Apr 2012)
New Revision: 6103

Modified:
   trunk/roundcubemail/program/include/rcube.php
   trunk/roundcubemail/program/include/rcube_cache.php
   trunk/roundcubemail/program/include/rcube_imap.php
   trunk/roundcubemail/program/include/rcube_shared.inc
Log:
- Improved ttl values handling


Modified: trunk/roundcubemail/program/include/rcube.php
===================================================================
--- trunk/roundcubemail/program/include/rcube.php	2012-04-19 07:02:14 UTC (rev 6102)
+++ trunk/roundcubemail/program/include/rcube.php	2012-04-19 07:42:19 UTC (rev 6103)
@@ -236,7 +236,7 @@
    *
    * @param string $name   Cache identifier
    * @param string $type   Cache type ('db', 'apc' or 'memcache')
-   * @param int    $ttl    Expiration time for cache items in seconds
+   * @param string $ttl    Expiration time for cache items
    * @param bool   $packed Enables/disables data serialization
    *
    * @return rcube_cache Cache object

Modified: trunk/roundcubemail/program/include/rcube_cache.php
===================================================================
--- trunk/roundcubemail/program/include/rcube_cache.php	2012-04-19 07:02:14 UTC (rev 6102)
+++ trunk/roundcubemail/program/include/rcube_cache.php	2012-04-19 07:42:19 UTC (rev 6103)
@@ -59,7 +59,7 @@
      * @param string $type   Engine type ('db' or 'memcache' or 'apc')
      * @param int    $userid User identifier
      * @param string $prefix Key name prefix
-     * @param int    $ttl    Expiration time of memcache/apc items in seconds (max.2592000)
+     * @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
@@ -82,8 +82,12 @@
             $this->db   = $rcube->get_dbh();
         }
 
+        // convert ttl string to seconds
+        $ttl = get_offset_sec($ttl);
+        if ($ttl > 2592000) $ttl = 2592000;
+
         $this->userid    = (int) $userid;
-        $this->ttl       = (int) $ttl;
+        $this->ttl       = $ttl;
         $this->packed    = $packed;
         $this->prefix    = $prefix;
     }

Modified: trunk/roundcubemail/program/include/rcube_imap.php
===================================================================
--- trunk/roundcubemail/program/include/rcube_imap.php	2012-04-19 07:02:14 UTC (rev 6102)
+++ trunk/roundcubemail/program/include/rcube_imap.php	2012-04-19 07:42:19 UTC (rev 6103)
@@ -3532,8 +3532,6 @@
         if ($this->caching && !$this->cache) {
             $rcube = rcube::get_instance();
             $ttl = $rcube->config->get('message_cache_lifetime', '10d');
-            $ttl = get_offset_time($ttl) - time();
-
             $this->cache = $rcube->get_cache('IMAP', $this->caching, $ttl);
         }
 

Modified: trunk/roundcubemail/program/include/rcube_shared.inc
===================================================================
--- trunk/roundcubemail/program/include/rcube_shared.inc	2012-04-19 07:02:14 UTC (rev 6102)
+++ trunk/roundcubemail/program/include/rcube_shared.inc	2012-04-19 07:42:19 UTC (rev 6103)
@@ -146,25 +146,23 @@
 
 
 /**
- * Create a unix timestamp with a specified offset from now.
+ * Returns number of seconds for a specified offset string.
  *
- * @param string $offset_str  String representation of the offset (e.g. 20min, 5h, 2days)
- * @param int    $factor      Factor to multiply with the offset
+ * @param string $str  String representation of the offset (e.g. 20min, 5h, 2days, 1week)
  *
- * @return int Unix timestamp
+ * @return int Number of seconds
  */
-function get_offset_time($offset_str, $factor=1)
+function get_offset_sec($str)
 {
-    if (preg_match('/^([0-9]+)\s*([smhdw])/i', $offset_str, $regs)) {
-        $amount = (int)$regs[1];
+    if (preg_match('/^([0-9]+)\s*([smhdw])/i', $str, $regs)) {
+        $amount = (int) $regs[1];
         $unit   = strtolower($regs[2]);
     }
     else {
-        $amount = (int)$offset_str;
+        $amount = (int) $str;
         $unit   = 's';
     }
 
-    $ts = time();
     switch ($unit) {
     case 'w':
         $amount *= 7;
@@ -174,15 +172,27 @@
         $amount *= 60;
     case 'm':
         $amount *= 60;
-    case 's':
-        $ts += $amount * $factor;
     }
 
-    return $ts;
+    return $amount;
 }
 
 
 /**
+ * Create a unix timestamp with a specified offset from now.
+ *
+ * @param string $offset_str  String representation of the offset (e.g. 20min, 5h, 2days)
+ * @param int    $factor      Factor to multiply with the offset
+ *
+ * @return int Unix timestamp
+ */
+function get_offset_time($offset_str, $factor=1)
+{
+    return time() + get_offset_sec($offset_str) * $factor;
+}
+
+
+/**
  * Truncate string if it is longer than the allowed length.
  * Replace the middle or the ending part of a string with a placeholder.
  *



More information about the svn mailing list