/** * Object constructor. * * @param string $type Engine type ('db' or 'memcache' or 'apc') * @param int $userid User identifier * @param string $prefix Key name prefix * @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 */ function __construct($type, $userid, $prefix = '', $ttl = 0, $packed = true) { $rcube = rcube::get_instance(); $type = strtolower($type); if ($type == 'memcache') { $this->type = 'memcache'; $this->db = $rcube->get_memcache(); } else { if ($type == 'apc') { $this->type = 'apc'; $this->db = function_exists('apc_exists'); // APC 3.1.4 required } else { $this->type = 'db'; $this->db = $rcube->get_dbh(); $this->table = $this->db->table_name('cache'); } } // convert ttl string to seconds $ttl = get_offset_sec($ttl); if ($ttl > 2592000) { $ttl = 2592000; } $this->userid = (int) $userid; $this->ttl = $ttl; $this->packed = $packed; $this->prefix = $prefix; }
/** * rcube_shared.inc: get_offset_sec() */ function test_get_offset_sec() { $data = array('1s' => 1, '1m' => 1 * 60, '1h' => 1 * 60 * 60, '1d' => 1 * 60 * 60 * 24, '1w' => 1 * 60 * 60 * 24 * 7, '1y' => (int) '1y', 100 => 100, '100' => 100); foreach ($data as $value => $expected) { $result = get_offset_sec($value); $this->assertEquals($expected, $result, "Invalid get_offset_sec() result for {$value}"); } }
/** * 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; }
/** * Object constructor. * * @param rcube_db $db DB handler * @param rcube_imap $imap IMAP handler * @param int $userid User identifier * @param bool $skip_deleted skip_deleted flag * @param string $ttl Expiration time of memcache/apc items * @param int $threshold Maximum cached message size */ function __construct($db, $imap, $userid, $skip_deleted, $ttl = 0, $threshold = 0) { // convert ttl string to seconds $ttl = get_offset_sec($ttl); if ($ttl > 2592000) { $ttl = 2592000; } $this->db = $db; $this->imap = $imap; $this->userid = $userid; $this->skip_deleted = $skip_deleted; $this->ttl = $ttl; $this->threshold = $threshold; // cache all possible information by default $this->mode = self::MODE_INDEX | self::MODE_MESSAGE; // database tables $this->index_table = $db->table_name('cache_index', true); $this->thread_table = $db->table_name('cache_thread', true); $this->messages_table = $db->table_name('cache_messages', true); }
/** * Garbage collector function for temp files. * Remove temp files older than two days */ public function gc_temp() { $tmp = unslashify($this->config->get('temp_dir')); // expire in 48 hours by default $temp_dir_ttl = $this->config->get('temp_dir_ttl', '48h'); $temp_dir_ttl = get_offset_sec($temp_dir_ttl); if ($temp_dir_ttl < 6 * 3600) { $temp_dir_ttl = 6 * 3600; } // 6 hours sensible lower bound. $expire = time() - $temp_dir_ttl; if ($tmp && ($dir = opendir($tmp))) { while (($fname = readdir($dir)) !== false) { if ($fname[0] == '.') { continue; } if (@filemtime($tmp . '/' . $fname) < $expire) { @unlink($tmp . '/' . $fname); } } closedir($dir); } }
/** * Object constructor. * * @param rcube_db $db DB handler * @param rcube_imap $imap IMAP handler * @param int $userid User identifier * @param bool $skip_deleted skip_deleted flag * @param string $ttl Expiration time of memcache/apc items * @param int $threshold Maximum cached message size */ function __construct($db, $imap, $userid, $skip_deleted, $ttl = 0, $threshold = 0) { // convert ttl string to seconds $ttl = get_offset_sec($ttl); if ($ttl > 2592000) { $ttl = 2592000; } $this->db = $db; $this->imap = $imap; $this->userid = $userid; $this->skip_deleted = $skip_deleted; $this->ttl = $ttl; $this->threshold = $threshold; // cache all possible information by default $this->mode = self::MODE_INDEX | self::MODE_MESSAGE; }