From 01d264c069331c7695869b72c4cfea99a3988e1d Mon Sep 17 00:00:00 2001
From: Joe Oblivian <0blivian@insiberia.net>
Date: Thu, 15 Sep 2011 20:43:47 +0200
Subject: [PATCH] Performance tweaks:

- Cache more aggressively
- Use persistent connections

Profiling: changing the define AI_DB_PROFILER to true will enable query logging (use with caution!)
---
 .htaccess         |  9 +++++++--
 db-config.php     |  4 +---
 wp-content/db.php | 21 ++++++++++++++++++++-
 3 files changed, 28 insertions(+), 6 deletions(-)

diff --git a/.htaccess b/.htaccess
index 8438c0731..9566a09bf 100644
--- a/.htaccess
+++ b/.htaccess
@@ -7,6 +7,9 @@ RewriteCond %{HTTP_HOST}   ^ventitre\.noblogs\.org [NC]
 RewriteRule ^/?(.*)         http://www.ventitre.org/$1 [L,R=301,NE]
 ####
 
+#avoid inspection of queries log
+RewriteRule ^/noblogs_queries.*	/ancheno [L,R=404]
+
 # uploaded files
 RewriteRule ^files/(.+) wp-includes/ms-files.php?file=$1 [L]
 RewriteRule ^gallery/([0-9]+)/(previews|previews-med)/(.*)$ oldgal/$1/$2/$3 [L]
@@ -17,7 +20,8 @@ RewriteRule ^resource/[^/]+/download/(.*)$ wp-includes/ms-files.php?file=2010/08
 # BEGIN WPSuperCache
 <IfModule mod_rewrite.c>
 AddDefaultCharset UTF-8
-RewriteCond %{REQUEST_URI} !^.*[^/]$
+# Commented in order to cache more
+# RewriteCond %{REQUEST_URI} !^.*[^/]$
 RewriteCond %{REQUEST_URI} !^.*//.*$
 RewriteCond %{REQUEST_METHOD} !POST
 RewriteCond %{QUERY_STRING} !.*=.*
@@ -31,7 +35,8 @@ RewriteCond %{HTTP:Accept-Encoding} gzip
 RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{HTTP_HOST}/%{HTTP:X-Forwarded-Proto}/$1/index.html.gz -f
 RewriteRule ^(.*) "/wp-content/cache/supercache/%{HTTP_HOST}/%{HTTP:X-Forwarded-Proto}/$1/index.html.gz" [L]
 
-RewriteCond %{REQUEST_URI} !^.*[^/]$
+#Commented in order to cache more
+#RewriteCond %{REQUEST_URI} !^.*[^/]$
 RewriteCond %{REQUEST_URI} !^.*//.*$
 RewriteCond %{REQUEST_METHOD} !POST
 RewriteCond %{QUERY_STRING} !.*=.*
diff --git a/db-config.php b/db-config.php
index 3b0e0246e..88f67a322 100644
--- a/db-config.php
+++ b/db-config.php
@@ -1,8 +1,7 @@
 <?php
 
 // Common settings
-$wpdb->save_queries = false;
-$wpdb->persistent = false;
+$wpdb->persistent = true;
 $wpdb->max_connections = 30;
 
 define("NOBLOGS_BACKEND_CONFIG", "/etc/noblogs/backends");
@@ -22,4 +21,3 @@ $wpdb->add_database(array(
     ));
 
 $wpdb_reverse_backend_map = noblogs_load_backends(NOBLOGS_BACKEND_CONFIG, $wpdb->hash_map);
-
diff --git a/wp-content/db.php b/wp-content/db.php
index 18d53a1bf..99ff86ed1 100644
--- a/wp-content/db.php
+++ b/wp-content/db.php
@@ -1,4 +1,6 @@
 <?php
+//AI patch: set to true if you want to activate query profiling
+define('AI_DB_PROFILER', false);
 
 /*
 Plugin Name: HyperDB
@@ -1441,6 +1443,23 @@ class hyperdb extends wpdb {
 
 } // class hyperdb
 
-$wpdb = new hyperdb();
+/**
+ * AI custom: Logs all queries for debugging purposes
+ */
+function ai_log_db_queries($query, $time, $backtrace=null, hyperdb $obj ) {
+    $fh = fopen( ABSPATH . '/profiling/noblogs_queries_'. date('Ymd') . '.log', 'a');
+    if (!$fh) {
+        return array($query, $time, $backtrace);
+    }
+    fwrite($fh, sprintf("##\n#Date: %s\n#Query time: %s\n%s\n", date('r'), $time, $query));
+    fclose($fh);
+    return array($query, $time, $backtrace);
+}
 
+
+$wpdb = new hyperdb();
+if ( AI_DB_PROFILER === true ) {
+    $wpdb->save_queries = true;
+    $wpdb->save_query_callback = 'ai_log_db_queries';
+}
 require( DB_CONFIG_FILE );
-- 
GitLab