cache_dir = $phpbb_root_path . 'cache/'; } /** * Load global cache */ function load() { // grab the global cache if (xcache_isset('global')) { $this->vars = xcache_get('global'); return true; } return false; } /** * Unload cache object */ function unload() { $this->save(); unset($this->vars); unset($this->sql_rowset); unset($this->sql_row_pointer); } /** * Save modified objects */ function save() { if (!$this->is_modified) { return; } xcache_set('global', $this->vars, 31536000); $this->is_modified = false; } /** * Tidy cache */ function tidy() { // cache has auto GC, no need to have any code here :) set_config('cache_last_gc', time(), true); } /** * Get saved cache object */ function get($var_name) { if ($var_name[0] == '_') { return (xcache_isset($var_name)) ? xcache_get($var_name) : false; } else { if (!sizeof($this->vars)) { $this->load(); } return (isset($this->vars[$var_name])) ? $this->vars[$var_name] : false; } } /** * Put data into cache */ function put($var_name, $var, $ttl = 31536000) { if ($var_name[0] == '_') { xcache_set($var_name, $var, $ttl); } else { $this->vars[$var_name] = $var; $this->is_modified = true; } } /** * Purge cache data */ function purge() { // Purge all phpbb cache files $dir = @opendir($this->cache_dir); if (!$dir) { return; } while (($entry = readdir($dir)) !== false) { if (strpos($entry, 'sql_') !== 0 && strpos($entry, 'data_') !== 0 && strpos($entry, 'ctpl_') !== 0 && strpos($entry, 'tpl_') !== 0) { continue; } @unlink($this->cache_dir . $entry); } closedir($dir); $n = xcache_count(XC_TYPE_VAR); for ($i = 0; $i < $n; $i++) { xcache_clear_cache(XC_TYPE_VAR, $i); } unset($this->vars); unset($this->sql_rowset); unset($this->sql_row_pointer); $this->is_modified = false; } /** * Destroy cache data */ function destroy($var_name, $table = '') { if ($var_name == 'sql' && !empty($table)) { if (!is_array($table)) { $table = array($table); } foreach ($table as $table_name) { // gives us the md5s that we want if (!xcache_isset('sql_' . $table_name)) { continue; } $temp = xcache_get('sql_' . $table_name); // delete each query ref foreach ($temp as $md5_id => $void) { xcache_unset('sql_' . $md5_id); } // delete the table ref xcache_unset('sql_' . $table_name); } return; } if ($var_name[0] == '_') { xcache_unset($var_name); } else if (isset($this->vars[$var_name])) { $this->is_modified = true; unset($this->vars[$var_name]); // We save here to let the following cache hits succeed $this->save(); } } /** * Load cached sql query */ function sql_load($query) { // Remove extra spaces and tabs $query = preg_replace('/[\n\r\s\t]+/', ' ', $query); $query_id = sizeof($this->sql_rowset); if (!xcache_isset('sql_' . md5($query))) { return false; } $this->sql_rowset[$query_id] = xcache_get('sql_' . md5($query)); $this->sql_row_pointer[$query_id] = 0; return $query_id; } /** * Save sql query */ function sql_save($query, &$query_result, $ttl) { // Remove extra spaces and tabs $query = preg_replace('/[\n\r\s\t]+/', ' ', $query); // determine which tables this query belongs to preg_match('/FROM \\(?(\\w+(?: \\w+)?(?:, ?\\w+(?: \\w+)?)*)\\)?/', $query, $regs); $tables = array_map('trim', explode(',', $regs[1])); foreach ($tables as $table_name) { if (($pos = strpos($table_name, ' ')) !== false) { $table_name = substr($table_name, 0, $pos); } if (xcache_isset('sql_' . $table_name)) { $temp = xcache_get('sql_' . $table_name); } else { $temp = array(); } $temp[md5($query)] = true; xcache_set('sql_' . $table_name, $temp, $ttl); } // store them in the right place $query_id = sizeof($this->sql_rowset); $this->sql_rowset[$query_id] = array(); $this->sql_row_pointer[$query_id] = 0; while ($row = phpbb::$db->sql_fetchrow($query_result)) { $this->sql_rowset[$query_id][] = $row; } phpbb::$db->sql_freeresult($query_result); xcache_set('sql_' . md5($query), $this->sql_rowset[$query_id], $ttl); $query_result = $query_id; } /** * Ceck if a given sql query exist in cache */ function sql_exists($query_id) { return isset($this->sql_rowset[$query_id]); } /** * Fetch row from cache (database) */ function sql_fetchrow($query_id) { if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id])) { return $this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]++]; } return false; } /** * Fetch a field from the current row of a cached database result (database) */ function sql_fetchfield($query_id, $field) { if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id])) { return (isset($this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]][$field])) ? $this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]][$field] : false; } return false; } /** * Seek a specific row in an a cached database result (database) */ function sql_rowseek($rownum, $query_id) { if ($rownum >= sizeof($this->sql_rowset[$query_id])) { return false; } $this->sql_row_pointer[$query_id] = $rownum; return true; } /** * Free memory used for a cached database result (database) */ function sql_freeresult($query_id) { if (!isset($this->sql_rowset[$query_id])) { return false; } unset($this->sql_rowset[$query_id]); unset($this->sql_row_pointer[$query_id]); return true; } } ?>