fast_cache; } function __construct($bin, $options, $default_options) { parent::__construct($bin, $options, $default_options); // Include required files. // This probably has to be moved to the parent class so it works // for all engines. if (!isset($options['include'])) { $options['include'] = $default_options['include']; } if (is_array($options['include'])) { foreach($options['include'] as $file) { include_once($file); } } else { include_once($options['include']); } $collection_name = $this->prefix . '-' . $bin; $this->collection = mongodb_collection($collection_name); } function get($key) { // Attempt to pull from static cache. $cache = parent::get($key); if (isset($cache)) { return $cache; } $cache = $this->collection->findOne(array('_id' => (string)$key)); if (!$cache || !isset($cache['data'])) { return FALSE; } // Prepare cache object unset($cache['_id']); $cache = (object)$cache; if ($cache->serialized) { $cache->data = unserialize($cache->data); } // Update static cache parent::set($key, $cache); return $cache; } function garbage_collection() { global $user; // Garbage collection necessary when enforcing a minimum cache lifetime. $cache_flush = variable_get('cache_flush_' . $this->name, 0); if ($cache_flush && ($cache_flush + variable_get('cache_lifetime', 0) <= $_SERVER['REQUEST_TIME'])) { // Reset the variable immediately to prevent a meltdown in heavy load situations. variable_set('cache_flush_' . $this->name, 0); // Time to flush old cache data $find = array('expire' => array('$lte' => $cache_flush, '$ne' => CACHE_PERMANENT)); $this->collection->remove($find); } } function set($key, $data, $expire = CACHE_PERMANENT, $headers = NULL) { $scalar = is_scalar($data); $entry = array( '_id' => (string)$key, 'cid' => (string)$key, 'created' => $_SERVER['REQUEST_TIME'], 'expire' => $expire, 'headers' => $headers, 'serialized' => !$scalar, 'data' => $scalar ? $data : serialize($data), ); if (!empty($key)) { $this->collection->save($entry); } } function delete($key) { // Delete from static cache parent::flush(); if ($key == '*') { // Whole bin delete $this->collection->remove(); } elseif (substr($key, strlen($key) - 1, 1) == '*') { // Wildcard delete $cid = substr($key, 0, strlen($key) - 1); $this->collection->remove(array('cid' => new MongoRegex('/'. preg_quote($cid) .'.*/'))); } else { // Single entry delete $this->collection->remove(array('_id' => (string)$key)); } } function flush() { $this->collection->remove(array('expire' => array('$ne' => CACHE_PERMANENT, '$lte' => $_SERVER['REQUEST_TIME']))); } }