Improved sql cache to permit make cache of several databases TICKETS #3581

This commit is contained in:
fermin831 2016-05-03 16:06:13 +02:00
parent 48f66052da
commit bb4ec54fbc
8 changed files with 57 additions and 16 deletions

View File

@ -32,7 +32,9 @@ echo '</a><br />';
echo '<a class="white footer">'. __('Page generated at') . ' '. ui_print_timestamp ($time, true, array ("prominent" => "timestamp")); //Always use timestamp here echo '<a class="white footer">'. __('Page generated at') . ' '. ui_print_timestamp ($time, true, array ("prominent" => "timestamp")); //Always use timestamp here
echo '</a>'; echo '</a>';
if (isset ($config['debug'])) { if (isset ($config['debug'])) {
echo ' - Saved '.format_numeric ($sql_cache["saved"]).' Queries'; $cache_info = array();
$cache_info = db_get_cached_queries();
echo ' - Saved '.$cache_info[0].' Queries';
} }
?> ?>

View File

@ -35,6 +35,8 @@ function mysql_connect_db($host = null, $db = null, $user = null, $pass = null,
return false; return false;
} }
db_change_cache_id ($db, $host);
mysql_select_db($db, $connect_id); mysql_select_db($db, $connect_id);
return $connect_id; return $connect_id;
} }
@ -287,9 +289,9 @@ function mysql_db_process_sql($sql, $rettype = "affected_rows", $dbconnection =
if ($sql == '') if ($sql == '')
return false; return false;
if ($cache && ! empty ($sql_cache[$sql])) { if ($cache && ! empty ($sql_cache[$sql_cache['id']][$sql])) {
$retval = $sql_cache[$sql]; $retval = $sql_cache[$sql_cache['id']][$sql];
$sql_cache['saved']++; $sql_cache['saved'][$sql_cache['id']]++;
db_add_database_debug_trace ($sql); db_add_database_debug_trace ($sql);
} }
else { else {
@ -335,7 +337,7 @@ function mysql_db_process_sql($sql, $rettype = "affected_rows", $dbconnection =
} }
if ($cache === true) if ($cache === true)
$sql_cache[$sql] = $retval; $sql_cache[$sql_cache ['id']][$sql] = $retval;
mysql_free_result ($result); mysql_free_result ($result);
} }
} }

View File

@ -45,6 +45,8 @@ function oracle_connect_db($host = null, $db = null, $user = null, $pass = null,
$date_format = oci_parse($connect_id , 'alter session set NLS_DATE_FORMAT =\'YYYY-MM-DD HH24:MI:SS\''); $date_format = oci_parse($connect_id , 'alter session set NLS_DATE_FORMAT =\'YYYY-MM-DD HH24:MI:SS\'');
$decimal_separator = oci_parse($connect_id , 'alter session set NLS_NUMERIC_CHARACTERS =\'.,\''); $decimal_separator = oci_parse($connect_id , 'alter session set NLS_NUMERIC_CHARACTERS =\'.,\'');
db_change_cache_id($host, $db);
oci_execute($datetime_tz_format); oci_execute($datetime_tz_format);
oci_execute($datetime_format); oci_execute($datetime_format);
oci_execute($date_format); oci_execute($date_format);
@ -224,9 +226,9 @@ function oracle_db_process_sql($sql, $rettype = "affected_rows", $dbconnection =
if ($sql == '') if ($sql == '')
return false; return false;
if ($cache && ! empty ($sql_cache[$sql])) { if ($cache && ! empty ($sql_cache[$sql_cache['id']][$sql])) {
$retval = $sql_cache[$sql]; $retval = $sql_cache[$sql_cache['id']][$sql];
$sql_cache['saved']++; $sql_cache['saved'][$sql_cache['id']]++;
db_add_database_debug_trace ($sql); db_add_database_debug_trace ($sql);
} }
else { else {

View File

@ -37,6 +37,8 @@ function postgresql_connect_db($host = null, $db = null, $user = null, $pass = n
if (! $connect_id) { if (! $connect_id) {
return false; return false;
} }
db_change_cache_id($host, $db);
return $connect_id; return $connect_id;
} }
@ -200,9 +202,9 @@ function postgresql_db_process_sql($sql, $rettype = "affected_rows", $dbconnecti
if ($sql == '') if ($sql == '')
return false; return false;
if ($cache && ! empty ($sql_cache[$sql])) { if ($cache && ! empty ($sql_cache[$sql_cache['id']][$sql])) {
$retval = $sql_cache[$sql]; $retval = $sql_cache[$sql_cache['id']][$sql];
$sql_cache['saved']++; $sql_cache['saved'][$sql_cache['id']]++;
db_add_database_debug_trace ($sql); db_add_database_debug_trace ($sql);
} }
else { else {

View File

@ -223,7 +223,7 @@ function db_logoff ($id_user, $ip) {
db_pandora_audit("Logoff", "Logged out", $id_user, $ip); db_pandora_audit("Logoff", "Logged out", $id_user, $ip);
} }
$sql_cache = array ('saved' => 0); $sql_cache = array ('saved' => array());
/** /**
* Get the first value of the first row of a table in the database. * Get the first value of the first row of a table in the database.
@ -613,7 +613,40 @@ function db_add_database_debug_trace ($sql, $result = false, $affected = false,
function db_clean_cache() { function db_clean_cache() {
global $sql_cache; global $sql_cache;
$sql_cache = array ('saved' => 0); $sql_cache = array ('saved' => array ());
}
/**
* Change the sql cache id to another value
*
* @return None
*/
function db_change_cache_id($name, $host) {
global $sql_cache;
// Update the sql cache identification
$sql_cache['id'] = $name . "_" . $host;
if (!isset ($sql_cache['saved'][$sql_cache['id']])){
$sql_cache['saved'][$sql_cache['id']] = 0;
}
}
/**
* Get the total cached queries and the databases checked
*
* @return (total_queries, total_dbs)
*/
function db_get_cached_queries() {
global $sql_cache;
$total_saved = 0;
$total_dbs = 0;
foreach ($sql_cache['saved'] as $saver) {
$total_saved += format_numeric($saver);
$total_dbs++;
}
return array ($total_saved, $total_dbs);
} }
/** /**

View File

@ -54,7 +54,7 @@ if (!empty ($module)) {
// Disable SQL cache // Disable SQL cache
global $sql_cache; global $sql_cache;
$sql_cache = array ('saved' => 0); $sql_cache = array ('saved' => array());
//Convert start time and end time to unix timestamps //Convert start time and end time to unix timestamps

View File

@ -53,7 +53,7 @@ if (!empty ($module)) {
// Disable SQL cache // Disable SQL cache
global $sql_cache; global $sql_cache;
$sql_cache = array ('saved' => 0); $sql_cache = array ('saved' => array());
//Convert start time and end time to unix timestamps //Convert start time and end time to unix timestamps

View File

@ -68,7 +68,7 @@ if (!empty ($export_btn) && !empty ($module)) {
// Disable SQL cache // Disable SQL cache
global $sql_cache; global $sql_cache;
$sql_cache = array ('saved' => 0); $sql_cache = array ('saved' => array());
//Convert start time and end time to unix timestamps //Convert start time and end time to unix timestamps