d6 queries caching

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
//DELETE not work
/**
* @file: includes/database.mysql-common.inc
*/
define('QUERY_LOCAL_CACHE', TRUE);
function _db_q($query) {
global $qcache;
$args = func_get_args();
$query_lower = strtolower($query);
list($action, $table) = _query_parse($query_lower);
//var_dump($action,$table,$query);
if ($action == 'select') {
if ($table) {
if ( ! isset($qcache[$table][$query_lower])) {
$qcache[$table][$query_lower] = call_user_func_array('mysql_query', $args);
}
else {
echo 'cache: '.$query.'<br />';
}
return $qcache[$table][$query_lower];
}
return call_user_func_array('mysql_query', $args);
}
else {
if ($table && isset($qcache[$table])) {
unset($qcache[$table]);
}
return call_user_func_array('mysql_query', $args);
}
}
function _query_parse($query) {
$pos = strpos($query, '*/');
$query = substr($query, $pos ? $pos + 3 : 0);
$action = substr($query, 0, strpos($query, ' '));
$table = NULL;
switch ($action) {
case 'select':
case 'delete':
$p = strpos($query, 'from');
if ($p) {
$begin_pos = $p + 5;
}
else {
break;
}
case 'update':
if ( ! isset($begin_pos)) {
$begin_pos = 7;
}
$end_pos = strpos($query, ' ', $begin_pos);
$table = $end_pos ? substr($query, $begin_pos, $end_pos - $begin_pos) : substr($query, $begin_pos);
break;
case 'insert':
case 'replace':
$begin_pos = strpos($query, 'into') + 5;
$m = preg_split('/[\s\(]/', substr($query, $begin_pos), 2, PREG_SPLIT_OFFSET_CAPTURE); //refactory
$end_pos = $begin_pos + $m[1][1] - 1;
$table = substr($query, $begin_pos, $end_pos - $begin_pos);
break;
default:
}
return array($action, $table);
}
/**
* Helper function for db_query().
* @file: includes/database.mysql.inc
*/
function _db_query($query, $debug = 0) {
global $active_db, $queries, $user;
if (variable_get('dev_query', 0)) {
list($usec, $sec) = explode(' ', microtime());
$timer = (float)$usec + (float)$sec;
// If devel.module query logging is enabled, prepend a comment with the username and calling function
// to the SQL string. This is useful when running mysql's SHOW PROCESSLIST to learn what exact
// code is issueing the slow query.
$bt = debug_backtrace();
// t() may not be available yet so we don't wrap 'Anonymous'.
$name = $user->uid ? $user->name : variable_get('anonymous', 'Anonymous');
// str_replace() to prevent SQL injection via username or anonymous name.
$name = str_replace(array('*', '/'), '', $name);
$query = '/* '. $name .' : '. $bt[2]['function'] .' */ '. $query;
}
if (defined('QUERY_LOCAL_CACHE') && QUERY_LOCAL_CACHE) {
$result = _db_q($query, $active_db);
}
else {
$result = mysql_query($query, $active_db);
}
if (variable_get('dev_query', 0)) {
$query = $bt[2]['function'] ."\n". $query;
list($usec, $sec) = explode(' ', microtime());
$stop = (float)$usec + (float)$sec;
$diff = $stop - $timer;
$queries[] = array($query, $diff);
}
if ($debug) {
print '<p>query: '. $query .'<br />error:'. mysql_error($active_db) .'</p>';
}
if (!mysql_errno($active_db)) {
return $result;
}
else {
// Indicate to drupal_error_handler that this is a database error.
${DB_ERROR} = TRUE;
trigger_error(check_plain(mysql_error($active_db) ."\nquery: ". $query), E_USER_WARNING);
return FALSE;
}
}