From 581b6b41fd51549fe05afede9da647ccf6297660 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Wed, 27 Aug 2014 08:46:57 -0400
Subject: [PATCH] Make possible to set vacation start/end date and time

---
 program/lib/Roundcube/rcube_cache.php |   78 +++++++++++++++++++++++++++++++-------
 1 files changed, 63 insertions(+), 15 deletions(-)

diff --git a/program/lib/Roundcube/rcube_cache.php b/program/lib/Roundcube/rcube_cache.php
index 08c9fc8..0017dca 100644
--- a/program/lib/Roundcube/rcube_cache.php
+++ b/program/lib/Roundcube/rcube_cache.php
@@ -38,12 +38,14 @@
     private $type;
     private $userid;
     private $prefix;
+    private $table;
     private $ttl;
     private $packed;
     private $index;
     private $cache         = array();
     private $cache_changes = array();
     private $cache_sums    = array();
+    private $max_packet    = -1;
 
 
     /**
@@ -71,8 +73,9 @@
             $this->db   = function_exists('apc_exists'); // APC 3.1.4 required
         }
         else {
-            $this->type = 'db';
-            $this->db   = $rcube->get_dbh();
+            $this->type  = 'db';
+            $this->db    = $rcube->get_dbh();
+            $this->table = $this->db->table_name('cache');
         }
 
         // convert ttl string to seconds
@@ -194,14 +197,25 @@
     {
         if ($this->type == 'db' && $this->db && $this->ttl) {
             $this->db->query(
-                "DELETE FROM ".$this->db->table_name('cache').
+                "DELETE FROM ".$this->table.
                 " WHERE user_id = ?".
                 " AND cache_key LIKE ?".
-                " AND " . $this->db->unixtimestamp('created')." < ?",
+                " AND expires < " . $this->db->now(),
                 $this->userid,
-                $this->prefix.'.%',
-                time() - $this->ttl);
+                $this->prefix.'.%');
         }
+    }
+
+
+    /**
+     * Remove expired records of all caches
+     */
+    static function gc()
+    {
+        $rcube = rcube::get_instance();
+        $db    = $rcube->get_dbh();
+
+        $db->query("DELETE FROM " . $db->table_name('cache') . " WHERE expires < " . $db->now());
     }
 
 
@@ -271,7 +285,7 @@
         else {
             $sql_result = $this->db->limitquery(
                 "SELECT data, cache_key".
-                " FROM ".$this->db->table_name('cache').
+                " FROM " . $this->table.
                 " WHERE user_id = ?".
                 " AND cache_key = ?".
                 // for better performance we allow more records for one key
@@ -306,13 +320,19 @@
      * Writes single cache record into DB.
      *
      * @param string $key  Cache key name
-     * @param mxied  $data Serialized cache data 
+     * @param mixed  $data Serialized cache data
      *
      * @param boolean True on success, False on failure
      */
     private function write_record($key, $data)
     {
         if (!$this->db) {
+            return false;
+        }
+
+        // don't attempt to write too big data sets
+        if (strlen($data) > $this->max_packet_size()) {
+            trigger_error("rcube_cache: max_packet_size ($this->max_packet) exceeded for key $key. Tried to write " . strlen($data) . " bytes", E_USER_WARNING);
             return false;
         }
 
@@ -326,7 +346,7 @@
         // Remove NULL rows (here we don't need to check if the record exist)
         if ($data == 'N;') {
             $this->db->query(
-                "DELETE FROM ".$this->db->table_name('cache').
+                "DELETE FROM " . $this->table.
                 " WHERE user_id = ?".
                 " AND cache_key = ?",
                 $this->userid, $key);
@@ -337,8 +357,10 @@
         // update existing cache record
         if ($key_exists) {
             $result = $this->db->query(
-                "UPDATE ".$this->db->table_name('cache').
-                " SET created = ". $this->db->now().", data = ?".
+                "UPDATE " . $this->table.
+                " SET created = " . $this->db->now().
+                    ", expires = " . ($this->ttl ? $this->db->now($this->ttl) : 'NULL').
+                    ", data = ?".
                 " WHERE user_id = ?".
                 " AND cache_key = ?",
                 $data, $this->userid, $key);
@@ -348,9 +370,9 @@
             // for better performance we allow more records for one key
             // so, no need to check if record exist (see rcube_cache::read_record())
             $result = $this->db->query(
-                "INSERT INTO ".$this->db->table_name('cache').
-                " (created, user_id, cache_key, data)".
-                " VALUES (".$this->db->now().", ?, ?, ?)",
+                "INSERT INTO " . $this->table.
+                " (created, expires, user_id, cache_key, data)".
+                " VALUES (" . $this->db->now() . ", " . ($this->ttl ? $this->db->now($this->ttl) : 'NULL') . ", ?, ?, ?)",
                 $this->userid, $key, $data);
         }
 
@@ -411,7 +433,7 @@
         }
 
         $this->db->query(
-            "DELETE FROM ".$this->db->table_name('cache').
+            "DELETE FROM " . $this->table.
             " WHERE user_id = ?" . $where,
             $this->userid);
     }
@@ -576,4 +598,30 @@
 
         return $this->packed ? @unserialize($data) : $data;
     }
+
+    /**
+     * Determine the maximum size for cache data to be written
+     */
+    private function max_packet_size()
+    {
+        if ($this->max_packet < 0) {
+            $this->max_packet = 2097152; // default/max is 2 MB
+
+            if ($this->type == 'db') {
+                $value = $this->db->get_variable('max_allowed_packet', 1048500);
+                $this->max_packet = min($value, $this->max_packet) - 2000;
+            }
+            else if ($this->type == 'memcache') {
+                $stats = $this->db->getStats();
+                $remaining = $stats['limit_maxbytes'] - $stats['bytes'];
+                $this->max_packet = min($remaining / 5, $this->max_packet);
+            }
+            else if ($this->type == 'apc' && function_exists('apc_sma_info')) {
+                $stats = apc_sma_info();
+                $this->max_packet = min($stats['avail_mem'] / 5, $this->max_packet);
+            }
+        }
+
+        return $this->max_packet;
+    }
 }

--
Gitblit v1.9.1