Ten Dollar Dates

A friend of mine has started a new website. The site is all about inexpensive date ideas. http://www.tendollardates.com/

I think this may be an excellent resource for us guys to get a woman’s perspective on what we can do to be sweet. Cheap.

This should be an excellent resource for me down the road – good thing Katie doesn’t know about the site yet. I am going to subscribe to the RSS feed and get my daily dose of do-good ideas.

Lets all leave comments or trackbacks, and let her know we are interested so she gets into the habit of posting an idea a day. If you have any suggestions, feel free to leave a comment here with them or email them to me and I will forward them on to her.

Homepage Caching

I have been having a problem at work. My homepage is now getting 100,000 hits a month. My home page is big unfortunately, about 43Kb plus auxiliary files. But the worst part is the 10 different calls to the database under 5 different modules, each with it’s own connection to the DB. This racks up some serious MySQL disk IO along with logs, and all that other stuff. Needless to say, I have been encountering some minor performance issues.

Simple solution is to cache the homepage.

I want to be able to regenerate the homepage whenever I want. I want the homepage to regenerate itself periodically, especially immediately into a new day as much of the content on the homepage is date specific. I don’t want to do any extra work to process this cache.

For my implementation, I am taking advantage of the fact that Apache will search for an index page: index.html, index.php, default,htm etc. I let index.html be my rendered, static copy of my homepage with index.php being the dynamic version. The big trick is that every time index.php is called, it writes out to index.html. Now it’s just a matter of getting index.php called at appropriate times.

In index.php, I am using php’s output buffering to capture the content of the homepage. I then write that buffer, unchanged, out to index.html and to the web-browser.

At the top of my homepage you would see the following:

/* Output Buffer
* We are doing something a bit odd here. The home page is the most hit page on the site. There are a LOT of calls on the home page to database, etc.
*
* In an attempt to make the server load go down, this index.php file will write out an index.html file as a static file
* This takes advantage of the fact that apache is looking for index.html before index.php
* running index.php will create index.html
* removing index.html will cause index.php to run and create index.html
*
* A cronjob removes index.html hourly
* Admin tools that modify data that could be displayed on the homepage should cause to have deleted index.html
*
* You can manually force an update to index.html by loading www.r-world.com/index.php (a clever way is to have the webmaster bookmark index.php as his start page.)
*
*/

function callback($buffer)
{
// lets write this page out to index.html.
$indexbufferfilename = 'index.html';

$indexbuffererror = '';

if (!$handle = fopen($indexbufferfilename, 'w')){
$indexbuffererror .= "Cannot open file ($indexbufferfilename)n";
exit;
}

if (fwrite($handle, $buffer) === FALSE) {
$indexbuffererror.= "Cannot write to file ($indexbufferfilename)n";
exit;
}

//echo "Success, wrote ($somecontent) to file($indexbufferfilename)";

fclose($handle);

if(!empty($indexbuffererror))
{
mail('[email protected]', "Error in creating homepage", $indexbuffererror);
}

// send the page to the browser
return $buffer;

}

ob_start("callback");

I then have the content of my homepage. I then end the page with a ob_end_flush(); as the very last line of code on the page.

This takes care of the creating of the homepage cache.

In order to dynamically have the cache created, I added a few lines of mod_rewrite to my .htaccess file that will check to see if index.html exists and has something in it or it calls an alternate file, my index.php. I found the idea on theApache1.3 URL Rewriting Guide.

My .htaccess file looks like:


Directory Index index.html index.php

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^index.html$ index.php [L]

I have several administration tools to edit data that is being displayed on the homepage. I have added an include to a file that simply deletes the index.html page. Now, whenever I modify data that may be displayed on the homepage, it is automatically displayed on the homepage as the homepage is re-created. I also have a cronjob that runs this file at the top of every hour.

As for how well it works. My average number of MySQL connections per second fell from 32 to 26 for a 20% reduction in MySQL calls. The CPU usage used by MySQL fell a couple of percentage points on the server, as several of the SQL statements for the homepage are 7 SELECT UNIONs that aren’t exactly trivial. The system Load Average also dropped about 33% as MySQL and apache aren’t hitting the drive near as much anymore.

The best part is that the PHP processing time has been taken out of the Load Time for the homepage. 1 poor sod per hour, and the first person to get the latest update will need to wait for the PHP processing (which everyone used to do anyhow) and writing out to disk. I also set me and my bosses bookmarks to load the index.php version of the page as whenever one of the two of us are looking at the site, we are likely looking to verify a change, and this will force an update.

Clever, eh?

Hawking Technologies USB 804.11G wireless adapter.

 
A friend of mine was looking for a Wireless card for her computer. I saw this at CompUSA and recomended it to her. She hasn’t picked it up yet, but when she does I will report here how well it worked for her.

I think I want one of these. The USB form factor is really nice, no cracking a case and using up a PCI slot. I like how it has a removeable (and extendable) antenna. I also think that the 2 inches that the length of the USB moves the antenna away from the computer case would probably help with interference. You could also plug the USB devise into a hub or at the end of an extension cable to optimize location for reception. All in all, I like it. Now if they would only add a gig of memory to the device, and install the drivers right on the unit.

Posted by Picasa
WordPress Appliance - Powered by TurnKey Linux