fast_cache; } /** * Completely override parent method to remove stuff we don't need (locking, * lookup etc). */ function __construct($bin, $options, $default_options) { $this->name = $bin; // Setup our prefixes so that we can prefix a particular bin, or if not set use the default prefix. if (isset($options['prefix'])) { $this->prefix = $options['prefix']; } elseif (!empty($default_options['prefix'])) { $this->prefix = $default_options['prefix']; } // This allows us to turn off fast_cache for cache_page so that we can get anonymous statistics. if (isset($options['fast_cache'])) { $this->fast_cache = $default_options['fast_cache']; } elseif (isset($default_options['fast_cache'])) { $this->fast_cache = $default_options['fast_cache']; } // This allows us to turn off static content caching for modules/bins that are already doing this. if (isset($options['static'])) { $this->static = $options['static']; } elseif (isset($default_options['static'])) { $this->static = $default_options['static']; } include_once('sites/all/modules/mongodb/mongodb.module'); $collection_name = $this->prefix . '-' . $bin; $this->collection = mongodb_collection($collection_name); } function get($key) { // Attempt to pull from static cache. $cache = parent::get($this->key($key)); if (isset($cache)) { return $cache; } // Garbage collection necessary when enforcing a minimum cache lifetime. $this->garbageCollection(); $cache = $this->collection->findOne(array('_id' => (string)$key)); // Prepare cache object unset($cache['_id']); $cache = (object)$cache; if ($cache->serialized) { $cache->data = unserialize($cache->data); } // Update static cache parent::set($this->key($key), $cache); return $cache; } /** * Garbage collection for get(). */ protected function garbageCollection() { 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 = $this->key(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']))); } }