Exemple #1
0
 /**
  * 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;
 }
Exemple #2
0
 /**
  * Deletes the cache record(s).
  *
  * @param string  $key         Cache key name or pattern
  * @param boolean $prefix_mode Enable it to clear all keys starting
  *                             with prefix specified in $key
  */
 private function remove_record($key = null, $prefix_mode = false)
 {
     if (!$this->db) {
         return;
     }
     if ($this->type != 'db') {
         $this->load_index();
         // Remove all keys
         if ($key === null) {
             foreach ($this->index as $key) {
                 $this->delete_record($key, false);
             }
             $this->index = array();
         } else {
             if ($prefix_mode) {
                 foreach ($this->index as $k) {
                     if (strpos($k, $key) === 0) {
                         $this->delete_record($k);
                     }
                 }
             } else {
                 $this->delete_record($key);
             }
         }
         return;
     }
     // Remove all keys (in specified cache)
     if ($key === null) {
         $where = " AND cache_key LIKE " . $this->db->quote($this->prefix . '.%');
     } else {
         if ($prefix_mode) {
             $where = " AND cache_key LIKE " . $this->db->quote($this->prefix . '.' . $key . '%');
         } else {
             $where = " AND cache_key = " . $this->db->quote($this->prefix . '.' . $key);
         }
     }
     $this->db->query("DELETE FROM " . $this->db->table_name('cache') . " WHERE user_id = ?" . $where, $this->userid);
 }