diff options
author | Meik Sievertsen <acydburn@phpbb.com> | 2009-02-22 15:29:18 +0000 |
---|---|---|
committer | Meik Sievertsen <acydburn@phpbb.com> | 2009-02-22 15:29:18 +0000 |
commit | 794c5749696c9fa2595ed3a1d7c836a0d984e11c (patch) | |
tree | 37aab2f0f965ddcaeb1d26af753095f59a6e025c /phpBB/includes/acm/acm_memcache.php | |
parent | 7aced345c5a2871f6eddfe316297b4ff9a0ebb76 (diff) | |
download | forums-794c5749696c9fa2595ed3a1d7c836a0d984e11c.tar forums-794c5749696c9fa2595ed3a1d7c836a0d984e11c.tar.gz forums-794c5749696c9fa2595ed3a1d7c836a0d984e11c.tar.bz2 forums-794c5749696c9fa2595ed3a1d7c836a0d984e11c.tar.xz forums-794c5749696c9fa2595ed3a1d7c836a0d984e11c.zip |
remove global and change $user-> to phpbb::$user->
git-svn-id: file:///svn/phpbb/trunk@9334 89ea8834-ac86-4346-8a33-228a782c2dd0
Diffstat (limited to 'phpBB/includes/acm/acm_memcache.php')
-rw-r--r-- | phpBB/includes/acm/acm_memcache.php | 178 |
1 files changed, 124 insertions, 54 deletions
diff --git a/phpBB/includes/acm/acm_memcache.php b/phpBB/includes/acm/acm_memcache.php index bbb556f173..a89657b04e 100644 --- a/phpBB/includes/acm/acm_memcache.php +++ b/phpBB/includes/acm/acm_memcache.php @@ -1,10 +1,10 @@ <?php -/** +/** * * @package acm * @version $Id$ -* @copyright (c) 2005 phpBB Group -* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* @copyright (c) 2005 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License * */ @@ -17,33 +17,38 @@ if (!defined('IN_PHPBB')) } /** -* ACM Memcache Based Caching +* ACM File Based Caching * @package acm */ class acm { - private $vars = array(); - private $is_modified = false; + var $vars = array(); + var $is_modified = false; + + var $sql_rowset = array(); + var $sql_row_pointer = array(); + var $cache_dir = ''; - public $sql_rowset = array(); - public $cache_dir = ''; - private $memcache; + var $memcache; /** * Set cache path */ - function __construct() + function acm() { + $this->cache_dir = $phpbb_root_path . 'cache/'; $this->memcache = memcache_connect('localhost', 11211); } /** * Load global cache */ - private function load() + function load() { // grab the global cache - if ($this->vars = memcache_get($this->memcache, 'global')) + $this->vars = memcache_get($this->memcache, 'global'); + + if ($this->vars !== false) { return true; } @@ -54,20 +59,22 @@ class acm /** * Unload cache object */ - public function unload() + function unload() { $this->save(); unset($this->vars); unset($this->sql_rowset); + unset($this->sql_row_pointer); $this->vars = array(); $this->sql_rowset = array(); + $this->sql_row_pointer = array(); } /** * Save modified objects */ - private function save() + function save() { if (!$this->is_modified) { @@ -82,7 +89,7 @@ class acm /** * Tidy cache */ - public function tidy() + function tidy() { // cache has auto GC, no need to have any code here :) @@ -92,28 +99,29 @@ class acm /** * Get saved cache object */ - public function get($var_name) + function get($var_name) { - if ($var_name[0] === '_') + if ($var_name[0] == '_') { + if (!$this->_exists($var_name)) + { + return false; + } + return memcache_get($this->memcache, $var_name); } else { - if (!sizeof($this->vars)) - { - $this->load(); - } - return (isset($this->vars[$var_name])) ? $this->vars[$var_name] : false; + return ($this->_exists($var_name)) ? $this->vars[$var_name] : false; } } /** * Put data into cache */ - public function put($var_name, $var, $ttl = 31536000) + function put($var_name, $var, $ttl = 2592000) { - if ($var_name[0] === '_') + if ($var_name[0] == '_') { memcache_set($this->memcache, $var_name, $var, 0, $ttl); } @@ -127,7 +135,7 @@ class acm /** * Purge cache data */ - public function purge() + function purge() { // Purge all phpbb cache files $dir = @opendir($this->cache_dir); @@ -144,7 +152,7 @@ class acm continue; } - @unlink($this->cache_dir . $entry); + $this->remove_file($this->cache_dir . $entry); } closedir($dir); @@ -152,20 +160,22 @@ class acm unset($this->vars); unset($this->sql_rowset); + unset($this->sql_row_pointer); $this->vars = array(); - $this->var_expires = array(); $this->sql_rowset = array(); + $this->sql_row_pointer = array(); $this->is_modified = false; } + /** * Destroy cache data */ - public function destroy($var_name, $table = '') + function destroy($var_name, $table = '') { - if ($var_name === 'sql' && !empty($table)) + if ($var_name == 'sql' && !empty($table)) { if (!is_array($table)) { @@ -176,6 +186,7 @@ class acm { // gives us the md5s that we want $temp = memcache_get($this->memcache, 'sql_' . $table_name); + if ($temp === false) { continue; @@ -194,7 +205,12 @@ class acm return; } - if ($var_name[0] === '_') + if (!$this->_exists($var_name)) + { + return; + } + + if ($var_name[0] == '_') { memcache_delete($this->memcache, $var_name); } @@ -209,9 +225,29 @@ class acm } /** + * Check if a given cache entry exist + */ + function _exists($var_name) + { + if ($var_name[0] == '_') + { + return true; + } + else + { + if (!sizeof($this->vars)) + { + $this->load(); + } + + return isset($this->vars[$var_name]); + } + } + + /** * Load cached sql query */ - public function sql_load($query) + function sql_load($query) { // Remove extra spaces and tabs $query = preg_replace('/[\n\r\s\t]+/', ' ', $query); @@ -225,7 +261,7 @@ class acm } $this->sql_rowset[$query_id] = $temp; - + $this->sql_row_pointer[$query_id] = 0; return $query_id; } @@ -233,25 +269,15 @@ class acm /** * Save sql query */ - public function sql_save($query, &$query_result, $ttl) + function sql_save($query, &$query_result, $ttl) { - global $db; - // Remove extra spaces and tabs $query = preg_replace('/[\n\r\s\t]+/', ' ', $query); - // determine which tables this query belongs to: - - // grab all the FROM tables, avoid getting a LEFT JOIN - preg_match('/FROM \(?(\w+(?: (?!LEFT JOIN)\w+)?(?:, ?\w+(?: (?!LEFT JOIN)\w+)?)*)\)?/', $query, $regs); + // determine which tables this query belongs to + preg_match('/FROM \\(?(\\w+(?: \\w+)?(?:, ?\\w+(?: \\w+)?)*)\\)?/', $query, $regs); $tables = array_map('trim', explode(',', $regs[1])); - // now get the LEFT JOIN - preg_match_all('/LEFT JOIN\s+(\w+)(?: \w+)?/', $query, $result, PREG_PATTERN_ORDER); - $tables = array_merge($tables, $result[1]); - - $query_hash = md5($query); - foreach ($tables as $table_name) { if (($pos = strpos($table_name, ' ')) !== false) @@ -264,13 +290,14 @@ class acm { $temp = array(); } - $temp[$query_hash] = true; + $temp[md5($query)] = true; memcache_set($this->memcache, 'sql_' . $table_name, $temp, 0, $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 = $db->sql_fetchrow($query_result)) { @@ -278,35 +305,63 @@ class acm } $db->sql_freeresult($query_result); - memcache_set($this->memcache, 'sql_' . $query_hash, $this->sql_rowset[$query_id], 0, $ttl); + memcache_set($this->memcache, 'sql_' . md5($query), $this->sql_rowset[$query_id], 0, $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) */ - public function sql_fetchrow($query_id) + function sql_fetchrow($query_id) { - list(, $row) = each($this->sql_rowset[$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 ($row !== NULL) ? $row : false; + return false; } /** * Fetch a field from the current row of a cached database result (database) */ - public function sql_fetchfield($query_id, $field) + function sql_fetchfield($query_id, $field) { - $row = current($this->sql_rowset[$query_id]); + 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 ($row !== false && isset($row[$field])) ? $row[$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) */ - public function sql_freeresult($query_id) + function sql_freeresult($query_id) { if (!isset($this->sql_rowset[$query_id])) { @@ -314,9 +369,24 @@ class acm } unset($this->sql_rowset[$query_id]); + unset($this->sql_row_pointer[$query_id]); return true; } + + /** + * Removes/unlinks file + */ + function remove_file($filename, $check = false) + { + if ($check && !@is_writable($this->cache_dir)) + { + // E_USER_ERROR - not using language entry - intended. + trigger_error('Unable to remove files within ' . $this->cache_dir . '. Please check directory permissions.', E_USER_ERROR); + } + + return @unlink($filename); + } } ?>
\ No newline at end of file |