/** * Reads an array of cache objects from storage. * * @param string $keys - Ids of the cache entry to read * @return array of vB_CacheObjects */ protected function readCacheArray($keys) { $cacheids = array(); foreach($keys as $id => $key) { $cacheids[$id] = "'" . vB::$db->escape_string($key) . "'"; } $rst = vB::$db->query_read_slave(" SELECT cacheid, data, expires, locktime, serialized FROM " . TABLE_PREFIX . "cache WHERE cacheid in (" . implode(',' , $cacheids) . ") AND expires > " . TIMENOW ); if (!$rst) { return false; } $found = array(); $missing = array(); while($record = vB::$db->fetch_array($rst)) { if (intval($record['serialized'])) { try { $record['data'] = unserialize($record['data']); if ($record['data']) { $obj = new vB_CacheObject($record['cacheid'], $record['data'], $record['expires'], $record['locktime']); //only return good values if (!$obj->isExpired()) { $found[$record['cacheid']] = $record['data']; } } } catch (exception $e) { //If we got here, something was improperly serialized //There's not much we can do, but we don't want to return bad data. } } else { if (!$obj->isExpired()) { $found[$record['cacheid']] = $record['data']; } } } return $found; }
/** * Writes data as a cache object. * * A string key is required to uniquely identify a cache object. Client * code should add all information that would affect the individualisation of * the cache object to the key. * * If lifetime_mins is supplied the cache object will be purged the next time it * is read after the TTL has passed. * * If a cache object should be purged on triggered events then events should be * supplied as an array of string id's of the form 'scope.event', for example * 'widget55.updated' may be used to purge a cache of a defined widget with the * id 55 when it is reconfigured. * * Note: To use events, a cacheObserver event handler must be registered first. * @see cache::attachObserver() * * @param string $key - Identifying key * @param mixed $data - Data to cache * @param int $lifetime - Lifetime of cache, in minutes * @param array string $events - Purge events to associate with the cache object * @return int | bool - Cache id or false */ public function write($key, $data, $lifetime_mins = false, $events = false) { // Check if caching is disabled, usually for debugging if (vB::$vbulletin->options['nocache']) { return false; } // If data is empty then there's nothing to write if (!$data) { return false; } // Wrap data in a cache object $cache = new vB_CacheObject($key, $data); if ($lifetime_mins) { $cache->setExpiry(TIMENOW + $lifetime_mins * 60); } // Write the cache object $this->writeCache($cache); // Notify observers of cache write and events $this->notifyWritten($key, $events); // Unlock the cache entry $this->unlock($key); $this->values_read[$key] = $data; //need to clear no_values for this key if (isset($this->no_values[$key])) { unset($this->no_values[$key]); } return $cache->getKey(); }