From 63c830af343b0cdc7a5e03e2bc1b4309a2cf06c9 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Thu, 4 Jun 2009 13:52:37 +0000 Subject: ACM memory abstract class git-svn-id: file:///svn/phpbb/branches/phpBB-3_0_0@9533 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/acm/acm_memory.php | 401 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 401 insertions(+) create mode 100644 phpBB/includes/acm/acm_memory.php (limited to 'phpBB/includes/acm/acm_memory.php') diff --git a/phpBB/includes/acm/acm_memory.php b/phpBB/includes/acm/acm_memory.php new file mode 100644 index 0000000000..964fdb2db4 --- /dev/null +++ b/phpBB/includes/acm/acm_memory.php @@ -0,0 +1,401 @@ +cache_dir = $phpbb_root_path . 'cache/'; + } + + /** + * Load global cache + */ + function load() + { + // grab the global cache + $this->vars = $this->read('global'); + + if ($this->vars !== false) + { + return true; + } + + return false; + } + + /** + * Unload cache object + */ + 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 + */ + function save() + { + if (!$this->is_modified) + { + return; + } + + $this->write('global', $this->vars, 2592000); + + $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] == '_') + { + if (!$this->_exists($var_name)) + { + return false; + } + + return $this->read($var_name); + } + else + { + return ($this->_exists($var_name)) ? $this->vars[$var_name] : false; + } + } + + /** + * Put data into cache + */ + function put($var_name, $var, $ttl = 2592000) + { + if ($var_name[0] == '_') + { + $this->write($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, 'ctpl_') !== 0 && strpos($entry, 'tpl_') !== 0) + { + continue; + } + + $this->remove_file($this->cache_dir . $entry); + } + closedir($dir); + + unset($this->vars); + unset($this->sql_rowset); + unset($this->sql_row_pointer); + + $this->vars = array(); + $this->sql_rowset = array(); + $this->sql_row_pointer = array(); + + $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 + $temp = $this->read('sql_' . $table_name); + + if ($temp === false) + { + continue; + } + + // delete each query ref + foreach ($temp as $md5_id => $void) + { + $this->delete('sql_' . $md5_id); + } + + // delete the table ref + $this->delete('sql_' . $table_name); + } + + return; + } + + if (!$this->_exists($var_name)) + { + return; + } + + if ($var_name[0] == '_') + { + $this->delete($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(); + } + } + + /** + * 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 + */ + function sql_load($query) + { + // Remove extra spaces and tabs + $query = preg_replace('/[\n\r\s\t]+/', ' ', $query); + $query_id = sizeof($this->sql_rowset); + + if (($result = $this->read('sql_' . md5($query))) === false) + { + return false; + } + + $this->sql_rowset[$query_id] = $result; + $this->sql_row_pointer[$query_id] = 0; + + return $query_id; + } + + /** + * Save sql query + */ + function sql_save($query, &$query_result, $ttl) + { + global $db; + + // Remove extra spaces and tabs + $query = preg_replace('/[\n\r\s\t]+/', ' ', $query); + $hash = md5($query); + + // determine which tables this query belongs to + // Some queries use backticks, namely the get_database_size() query + // don't check for conformity, the SQL would error and not reach here. + if (!preg_match('/FROM \\(?(`?\\w+`?(?: \\w+)?(?:, ?`?\\w+`?(?: \\w+)?)*)\\)?/', $query, $regs)) + { + // Bail out if the match fails. + return; + } + $tables = array_map('trim', explode(',', $regs[1])); + + foreach ($tables as $table_name) + { + // Remove backticks + $table_name = ($table_name[0] == '`') ? substr($table_name, 1, -1) : $table_name; + + if (($pos = strpos($table_name, ' ')) !== false) + { + $table_name = substr($table_name, 0, $pos); + } + + $temp = $this->read('sql_' . $table_name); + + if ($temp === false) + { + $temp = array(); + } + + $temp[$hash] = true; + + // This must never expire + $this->write('sql_' . $table_name, $temp, 0); + } + + // 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)) + { + $this->sql_rowset[$query_id][] = $row; + } + $db->sql_freeresult($query_result); + + $this->write('sql_' . $hash, $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; + } + + /** + * 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 -- cgit v1.2.1 From aec3a498de31b380d0eb48154de0b2bd56054dfa Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Thu, 4 Jun 2009 14:34:07 +0000 Subject: Lets follow some PHP4 conventions underscores for internal methods. git-svn-id: file:///svn/phpbb/branches/phpBB-3_0_0@9536 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/acm/acm_memory.php | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'phpBB/includes/acm/acm_memory.php') diff --git a/phpBB/includes/acm/acm_memory.php b/phpBB/includes/acm/acm_memory.php index 964fdb2db4..7875c3441e 100644 --- a/phpBB/includes/acm/acm_memory.php +++ b/phpBB/includes/acm/acm_memory.php @@ -34,6 +34,8 @@ class acm_memory */ function acm_memory() { + global $phpbb_root_path; + $this->cache_dir = $phpbb_root_path . 'cache/'; } @@ -43,7 +45,7 @@ class acm_memory function load() { // grab the global cache - $this->vars = $this->read('global'); + $this->vars = $this->_read('global'); if ($this->vars !== false) { @@ -78,7 +80,7 @@ class acm_memory return; } - $this->write('global', $this->vars, 2592000); + $this->_write('global', $this->vars, 2592000); $this->is_modified = false; } @@ -105,7 +107,7 @@ class acm_memory return false; } - return $this->read($var_name); + return $this->_read($var_name); } else { @@ -120,7 +122,7 @@ class acm_memory { if ($var_name[0] == '_') { - $this->write($var_name, $var, $ttl); + $this->_write($var_name, $var, $ttl); } else { @@ -180,7 +182,7 @@ class acm_memory foreach ($table as $table_name) { // gives us the md5s that we want - $temp = $this->read('sql_' . $table_name); + $temp = $this->_read('sql_' . $table_name); if ($temp === false) { @@ -190,11 +192,11 @@ class acm_memory // delete each query ref foreach ($temp as $md5_id => $void) { - $this->delete('sql_' . $md5_id); + $this->_delete('sql_' . $md5_id); } // delete the table ref - $this->delete('sql_' . $table_name); + $this->_delete('sql_' . $table_name); } return; @@ -207,7 +209,7 @@ class acm_memory if ($var_name[0] == '_') { - $this->delete($var_name); + $this->_delete($var_name); } else if (isset($this->vars[$var_name])) { @@ -248,7 +250,7 @@ class acm_memory $query = preg_replace('/[\n\r\s\t]+/', ' ', $query); $query_id = sizeof($this->sql_rowset); - if (($result = $this->read('sql_' . md5($query))) === false) + if (($result = $this->_read('sql_' . md5($query))) === false) { return false; } @@ -290,7 +292,7 @@ class acm_memory $table_name = substr($table_name, 0, $pos); } - $temp = $this->read('sql_' . $table_name); + $temp = $this->_read('sql_' . $table_name); if ($temp === false) { @@ -300,7 +302,7 @@ class acm_memory $temp[$hash] = true; // This must never expire - $this->write('sql_' . $table_name, $temp, 0); + $this->_write('sql_' . $table_name, $temp, 0); } // store them in the right place @@ -314,7 +316,7 @@ class acm_memory } $db->sql_freeresult($query_result); - $this->write('sql_' . $hash, $this->sql_rowset[$query_id], $ttl); + $this->_write('sql_' . $hash, $this->sql_rowset[$query_id], $ttl); $query_result = $query_id; } -- cgit v1.2.1 From 0cdad21cb38337255761b53513d5626674ab7986 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Thu, 4 Jun 2009 15:04:36 +0000 Subject: Introduce XCache and eAccelerator, make some small changes to the abstract, forgot a period in the command for r9537 :( git-svn-id: file:///svn/phpbb/branches/phpBB-3_0_0@9538 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/acm/acm_memory.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'phpBB/includes/acm/acm_memory.php') diff --git a/phpBB/includes/acm/acm_memory.php b/phpBB/includes/acm/acm_memory.php index 7875c3441e..a97f680d1e 100644 --- a/phpBB/includes/acm/acm_memory.php +++ b/phpBB/includes/acm/acm_memory.php @@ -228,7 +228,7 @@ class acm_memory { if ($var_name[0] == '_') { - return true; + return $this->_isset($var_name); } else { @@ -398,6 +398,19 @@ class acm_memory return @unlink($filename); } + + /** + * Check if a cache var exists + * + * @access protected + * @param string $var Cache key + * @return bool True if it exists, otherwise false + */ + function _isset($var) + { + // Most caches don't need to check + return true; + } } ?> \ No newline at end of file -- cgit v1.2.1 From cd1c5de3a508ec851b11baf4095449c66ee6c770 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Thu, 4 Jun 2009 15:37:53 +0000 Subject: Finished I hope, appropriate errors when modules are missing. git-svn-id: file:///svn/phpbb/branches/phpBB-3_0_0@9540 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/acm/acm_memory.php | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'phpBB/includes/acm/acm_memory.php') diff --git a/phpBB/includes/acm/acm_memory.php b/phpBB/includes/acm/acm_memory.php index a97f680d1e..85e7a7b9d7 100644 --- a/phpBB/includes/acm/acm_memory.php +++ b/phpBB/includes/acm/acm_memory.php @@ -37,6 +37,13 @@ class acm_memory global $phpbb_root_path; $this->cache_dir = $phpbb_root_path . 'cache/'; + + if (!isset($this->extension) || !extension_loaded($this->extension)) + { + global $acm_type; + + trigger_error("Could not find required extension [{$this->extension}] for the ACM module $acm_type.", E_USER_ERROR); + } } /** -- cgit v1.2.1 From 832c6b2f62917b0c456c7a9f33f40d2ebeb75387 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Thu, 4 Jun 2009 15:59:42 +0000 Subject: Use unique per board cache keys per [1] important when using shared memory from opcode caches with multiple boards on one server. [1] http://area51.phpbb.com/phpBB/viewtopic.php?p=201739#p201739 git-svn-id: file:///svn/phpbb/branches/phpBB-3_0_0@9541 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/acm/acm_memory.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'phpBB/includes/acm/acm_memory.php') diff --git a/phpBB/includes/acm/acm_memory.php b/phpBB/includes/acm/acm_memory.php index 85e7a7b9d7..c7b5d34a47 100644 --- a/phpBB/includes/acm/acm_memory.php +++ b/phpBB/includes/acm/acm_memory.php @@ -22,6 +22,8 @@ if (!defined('IN_PHPBB')) */ class acm_memory { + var $key_prefix; + var $vars = array(); var $is_modified = false; @@ -34,9 +36,10 @@ class acm_memory */ function acm_memory() { - global $phpbb_root_path; + global $phpbb_root_path, $dbname, $table_prefix; - $this->cache_dir = $phpbb_root_path . 'cache/'; + $this->cache_dir = $phpbb_root_path . 'cache/'; + $this->key_prefix = md5($dbname, $table_prefix) . '_'; if (!isset($this->extension) || !extension_loaded($this->extension)) { -- cgit v1.2.1 From a539fca62b10f53a5f5dadf07f9ab07340fdabf9 Mon Sep 17 00:00:00 2001 From: Meik Sievertsen Date: Sun, 7 Jun 2009 11:34:01 +0000 Subject: some corrections, only very minor things. git-svn-id: file:///svn/phpbb/branches/phpBB-3_0_0@9554 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/acm/acm_memory.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'phpBB/includes/acm/acm_memory.php') diff --git a/phpBB/includes/acm/acm_memory.php b/phpBB/includes/acm/acm_memory.php index c7b5d34a47..fd9b9ff342 100644 --- a/phpBB/includes/acm/acm_memory.php +++ b/phpBB/includes/acm/acm_memory.php @@ -156,7 +156,7 @@ class acm_memory while (($entry = readdir($dir)) !== false) { - if (strpos($entry, 'ctpl_') !== 0 && strpos($entry, 'tpl_') !== 0) + if (strpos($entry, 'sql_') !== 0 && strpos($entry, 'data_') !== 0 && strpos($entry, 'ctpl_') !== 0 && strpos($entry, 'tpl_') !== 0) { continue; } @@ -415,7 +415,7 @@ class acm_memory * @access protected * @param string $var Cache key * @return bool True if it exists, otherwise false - */ + */ function _isset($var) { // Most caches don't need to check -- cgit v1.2.1 From 75ae7aee971c91e8548aabc93319dfa0789d6164 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Sat, 13 Jun 2009 13:14:27 +0000 Subject: Fix some issues with XCache, can't totally resolve the purge() method as XCache does not expose its settings to userland PHP. #46435 git-svn-id: file:///svn/phpbb/branches/phpBB-3_0_0@9579 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/acm/acm_memory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/includes/acm/acm_memory.php') diff --git a/phpBB/includes/acm/acm_memory.php b/phpBB/includes/acm/acm_memory.php index fd9b9ff342..1ed4fb0d55 100644 --- a/phpBB/includes/acm/acm_memory.php +++ b/phpBB/includes/acm/acm_memory.php @@ -39,7 +39,7 @@ class acm_memory global $phpbb_root_path, $dbname, $table_prefix; $this->cache_dir = $phpbb_root_path . 'cache/'; - $this->key_prefix = md5($dbname, $table_prefix) . '_'; + $this->key_prefix = substr(md5($dbname . $table_prefix), 0, 8) . '_'; if (!isset($this->extension) || !extension_loaded($this->extension)) { -- cgit v1.2.1