public
Created

TeamCal Opti 2

  • Download Gist
db_conn_query_cache.patch
Diff
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
Index: includes/db.class.php
===================================================================
--- includes/db.class.php (Revision 109)
+++ includes/db.class.php (Revision 112)
@@ -29,8 +29,9 @@
var $db_user = '';
var $db_pass = '';
var $db_persistent = '';
- var $db_handle = '';
+ static $db_handle = '';
var $db_errortxt = '';
+ static $query_cache = array();
/**
* Constructor reading server and database information from the
@@ -53,23 +54,24 @@
*/
function db_connect()
{
+ if (!myDB::$db_handle)
switch ($this->db_type) {
case 1 : // MySQL
if ($this->db_persistent) {
- $this->db_handle = @ mysql_pconnect($this->db_server, $this->db_user, $this->db_pass);
+ myDB::$db_handle = @ mysql_pconnect($this->db_server, $this->db_user, $this->db_pass);
} else {
- $this->db_handle = @ mysql_connect($this->db_server, $this->db_user, $this->db_pass);
+ myDB::$db_handle = @ mysql_connect($this->db_server, $this->db_user, $this->db_pass);
}
- if (!$this->db_handle) {
+ if (!myDB::$db_handle) {
$errtxt = "Connecting to mySQL server " . $this->db_server . " failed.";
$this->db_error($errtxt, "db_connect()", true);
return;
}
- if (!@ mysql_select_db($this->db_name, $this->db_handle)) {
- $errtxt = "
+ if (!@ mysql_select_db($this->db_name, myDB::$db_handle)) {
+ $errtxt = "s
Error: Connection to MySQL database " . $this->db_server . "/" . $this->db_name . " failed.<BR>
- Code: " . @ mysql_errno($this->db_handle) . ",<BR>
- Message: " . @ mysql_error($this->db_handle) . "
+ Code: " . @ mysql_errno(myDB::$db_handle) . ",<BR>
+ Message: " . @ mysql_error(myDB::$db_handle) . "
";
$this->db_error($errtxt, "db_connect()", true);
}
@@ -87,11 +89,24 @@
{
switch ($this->db_type) {
case 1 : // MySQL
- $result = mysql_query($query, $this->db_handle);
- if (!$result) {
- echo "Error: A problem was encountered while executing this query:<br><br>\n\n$query<br><br>\n\n";
- die("Error: Fatal database error!<br>\n");
+ $upp_query = strtoupper($query);
+ if (strpos($upp_query, 'UPDATE') || strpos($upp_query, 'INSERT') || strpos($upp_query, 'DELETE')){
+ // we're changing the database so throw away the cache
+ myDB::$query_cache = array();
}
+ if (!array_key_exists($query, myDB::$query_cache)){
+ $result = mysql_query($query, myDB::$db_handle);
+ if (!$result) {
+ echo "Error: A problem was encountered while executing this query:<br><br>\n\n$query<br><br>\n\n";
+ die("Error: Fatal database error!<br>\n");
+ }
+ myDB::$query_cache[$query] = $result;
+ } else {
+ $result = myDB::$query_cache[$query];
+ if (mysql_num_rows($result) > 0){
+ mysql_data_seek($result, 0);
+ }
+ }
break;
}
return $result;

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.