------------------------------------------------------------
revno: 1205
committer: Jakub Jankiewicz <[email protected]>
branch nick: aikiframework
timestamp: Thu 2012-05-31 17:46:50 +0200
message:
  Add in memory cache for mysql
modified:
  configs/config.php
  libs/database/index.php
  libs/database/mysql.php


--
lp:aikiframework
https://code.launchpad.net/~aikiframework-devel/aikiframework/trunk

Your team Aiki Framework Developers is subscribed to branch lp:aikiframework.
To unsubscribe from this branch go to 
https://code.launchpad.net/~aikiframework-devel/aikiframework/trunk/+edit-subscription
=== modified file 'configs/config.php'
--- configs/config.php	2012-02-21 06:25:35 +0000
+++ configs/config.php	2012-05-31 15:46:50 +0000
@@ -177,10 +177,15 @@
  * @global string $config["cache_dir"] */
 $config["cache_dir"] = "cache";
 
-/** if set to true will cache the results of sql queries to files
+/** if set to true will cache the results of sql queries
  * @global bool $config["enable_query_cache"] */
 $config["enable_query_cache"] = false;
 
+/** if set to true will cache sql results to files
+ * if set to false and enable_query_cache is set to true will cache only in memory
+ * @global bool $config["use_disk_cache"] */
+$config["use_disk_cache"] = false;
+
 /** enable multi databases connections
  * @global bool $config["allow_multiple_databases"] */
 $config["allow_multiple_databases"] = false;

=== modified file 'libs/database/index.php'
--- libs/database/index.php	2012-01-25 20:50:16 +0000
+++ libs/database/index.php	2012-05-31 15:46:50 +0000
@@ -75,7 +75,9 @@
 
 	//Aikicms turned this to false
 	public $debug_echo_is_on = false;
-
+    
+    // in memory cache - added by AIKI
+    public $cached_queries = array();
 	/**********************************************************************
 		*  Constructor
 		*/
@@ -108,9 +110,30 @@
 
 	public function query($query)
 	{
-		die(EZSQL_CORE_ERROR);
+        die(EZSQL_CORE_ERROR);
 	}
 
+    public function get_memory_cache($query) {
+        if ($this->cache_queries && isset($cached_queries[$query]) &&
+            count($cached_queries[$query]) > 0) {
+            $result_cache = $cached_queries[$query];
+            $this->col_info = $result_cache['col_info'];
+            $this->last_result = $result_cache['last_result'];
+            $this->num_rows = $result_cache['num_rows'];
+            return $result_cache['return_value'];
+        }
+    }
+
+    public function store_memory_cache($query, $is_insert)
+    {
+        if ($this->cache_queries && $is_insert) {
+            $cached_queries[$query] = array('col_info' => $this->col_info,
+                                            'last_result' => $this->last_result,
+                                            'num_rows' => $this->num_rows,
+                                            'return_value' => $this->num_rows);
+        }
+    }
+
 	/**********************************************************************
 		*  Format a string correctly for safe insert - over-ridden by specific
 		*  DB class
@@ -621,11 +644,13 @@
 }
 
 
-if (isset($config['enable_query_cache']) && isset($config['cache_dir'])
-	&& $config['enable_query_cache'] ) {
-	
+if (isset($config['enable_query_cache']) && $config['enable_query_cache']) {
+    $db->cache_queries = true;
+}
+
+if (isset($config['cache_dir']) && isset($config['use_disk_cache']) &&
+    $config['use_disk_cache']) {
 	$db->cache_timeout = !isset($config['db_cache_timeout']) ? $config['db_cache_timeout'] : 24;
 	$db->cache_dir = $config['cache_dir'];
 	$db->use_disk_cache = true;
-	$db->cache_queries = true;
 }

=== modified file 'libs/database/mysql.php'
--- libs/database/mysql.php	2012-01-25 22:49:24 +0000
+++ libs/database/mysql.php	2012-05-31 15:46:50 +0000
@@ -70,6 +70,7 @@
 	public  $dbpassword = false;
 	public  $dbname = false;
 	public  $dbhost = false;
+    
 
 	/**********************************************************************
 		*  Constructor - allow the user to perform a qucik connect at the
@@ -170,7 +171,7 @@
 			$this->register_error($str.' in '.__FILE__.' on line '.__LINE__);
 			$this->show_errors ? trigger_error($str,E_USER_WARNING) : null;
 			// @TODO need revision, perhaps must die never
-			if ( !defined("AIKI_INSTALLER_APPS") ){				
+			if ( !defined("AIKI_INSTALLER_APPS") ) {
 				die("Fatal error: Lost Connection to database. please make sure the information in your config.php are correct");
 			}
 		}
@@ -227,7 +228,10 @@
 		// Keep track of the last query for debug..
 		$this->last_query = $query;
 
-
+        $memory_cache = $this->get_memory_cache($query);
+        if ($memory_cache) {
+            return $memory_cache;
+        }
 
 		// Use core file cache function
 		$cache = $this->get_cache($query);
@@ -254,7 +258,8 @@
 			if ( $str = @mysql_error($this->dbh) )
 			{
 				//check if multi database is enabled
-				if (isset ($config["allow_multiple_databases"]) and $config["allow_multiple_databases"]){
+				if (isset ($config["allow_multiple_databases"]) and
+                    $config["allow_multiple_databases"]) {
 
 					$find_request_to_external_db = preg_match('/\bfrom\b\s*(\w+)\.(\w+)/i',$query,$matches);
 					if ($find_request_to_external_db){
@@ -321,7 +326,6 @@
 					$i++;
 				}
 
-
 				// Store Query Results
 				$num_rows=0;
 				while ( $row = @mysql_fetch_object($this->result) )
@@ -341,7 +345,9 @@
 			}
 
 			// disk caching of queries
-			$this->store_cache($query,$is_insert);
+			$this->store_cache($query, $is_insert);
+            // in memory cache
+            $this->store_memory_cache($query, $is_insert);
 
 			// If debug ALL queries
 			$this->trace || $this->debug_all ? $this->debug() : null ;

_______________________________________________
Mailing list: https://launchpad.net/~aikiframework-devel
Post to     : [email protected]
Unsubscribe : https://launchpad.net/~aikiframework-devel
More help   : https://help.launchpad.net/ListHelp

Reply via email to