Archive for June, 2007

CakePHP Tips: Disable Database Caching

Friday, June 29th, 2007

Cake caches database queries. This is good, generally, it speeds things up a lot. But it is not always good. For example, if I want to query the database once a second to look for updates (e.g. for my chat application) the you don’t want the cache. Here’s how to clear it:

1) Create /app/app_model.php, and add in it the following

<?php

class AppModel extends Model{
function _clearDBCache() {
$db =& ConnectionManager::getDataSource($this->useDbConfig);
$db->_queryCache = array();
}
}

Note: you don’t always need a close "?>"

2) Call $modelNameHere->_clearDBCache() whereever you need the DB cache cleared.

That’s it! Have fun!

Whilst I am on the subject of CakePHP tips, why not look into using expects() to make your CakePHP queries more efficient - only request the data you need. Much better than using unbindModel and bindModel all over the place!

Bookmark and Share

How I made an AJAX chat client more efficient.

Friday, June 29th, 2007

I am currently working on making AJAX chat. This has had many issues associated with it, the major one being bandwidth usage. By default the headers sent/received in a basic AJAX request under Firefox with mootools are are around 1kb! That is huge, and will put massive stretches on bandwidth. For this reason, I tried to minimize the headers as best I could, and got them down to around 150bytes each way, total. Read on for what I did…

I used the following options for the client (mootools, but you should be able to get the gist):

Client headers (JavaScript with Mootools):

new XHR({method:’get’, headers:{’Accept’:”, ‘Accept-Language’:”, ‘Accept-Encoding’:”, ‘Accept-Charset’:”, ‘User-Agent’:” }});

This saved a lot of the unnecessary details being sent (if you require these headers, why not cache them in your $_SESSION variable in PHP?). The biggest of these headers was actually the User-Agent header. Turning this off gave me issues with sessions under CakePHP though. It turns out that Cake uses the User-Agent string to increase the security of Sessions. Well, for now I class the bandwidth saved more important than the security implications, so I added the following lines to /app/webroot/index.php in cake:

Fix sessions (Apache server with CakePHP):

$_SERVER['OLD_HTTP_USER_AGENT'] = $_SERVER['HTTP_USER_AGENT'];
$_SERVER['HTTP_USER_AGENT'] ="REMOVED";

Which made Cake "think" that all web browsers were called "REMOVED." An alternative method could be to introduce a new session purely for usage through the chat interface, but that seemed like too much hassle to me.

I also removed the following headers from the server side, using PHP code:

Server headers (Apache, PHP and CakePHP):

header(’X-Powered-By:’);
header(’Cache-Control:’);
header(’Expires:’);
header(’Vary:’);
header(’P3P:’);
header(’Server:’,true);
header(’Pragma:’);

And edited my Apache config to reduce the size of the ‘Server’ header (as apparently I cannot remove it using PHP):

Reducing Apache signature header:

ServerSignature Off
ServerTokens Prod

The ServerSignature option is more of a security thing - stopping people from knowing exactly what versions of software my server runs.

This worked well. I then compressed all my XML outputs as far as I could, using one character tags with one character properties, and reducing empty f tags to <f/> wherever possible. This worked fine. But I realised that we were still sending 0.5kb/second/client. It was at this point that I thought

Why not get the server to wait for new content?

So by using the php usleep() function I had the data fetching function poll the database every $delay milliseconds. If there were changes, it would output the changes and return. If there were no changes after 30 seconds, it would exit anyway with an "empty" message: <f/>. If the client disconnected it would discontinue.

This seemed to be working great, except new posts that I wrote to the chat were not appearing until the aforementioned 30seconds had expired. It took me a while to track down the reason, but it is due to CakePHP and PHP serializing session access so as to prevent race conditions. Well my chat function just cached all the session data (it did not need to edit it) and then ran

Releasing the session (so other connections work):

session_write_close();

in order to release the session, so other connections could continue. Now I am aware of this issue, I think I will be building session_write_close() into many more of my functions!

But now I have a chat solution with variable delays (currently set to 0.5s) that allows you to have AJAX chat without requiring huge amounts of bandwidth. I can even increase the 30 seconds by changing PHP’s max_execution_time.

What if the server suddenly comes under attack?

I didn’t mention: I also (optionally) send out the delay number to the scripts, so if I want to I can change the delay from 0.5s to 5s across all currently connected clients within 30 seconds, without any of them having to reload, or even realise the change! This means that if bandwidth is suddenly a problem, I can ask the web browsers to reduce their pounding of the servers, and they will almost instantly. Pretty neat, eh?

I am beginning to think that AJAX is a viable solution to chat, so long as you think about it enough.

Keywords for Google:

Increasing AJAX efficiency, making efficient AJAX code, make AJAX more efficient, reduce header count, decrease number of headers, remove headers, reduce AJAX bandwidth

Ideas for the future:

Currently the chat is stored in tables in a MySQL database, but I think we will be using memcached to improve efficiency some time soon. We will also probably remove the overhead of CakePHP as a framework just for the processor intensive chatroom feed script.

Bookmark and Share

IBM

Tuesday, June 26th, 2007

I like IBM. I have even visited them a couple of times if you look at my homepage. Well, whilst reading LifeHacker, I saw a link to the following article on the IBM website: Whistle while you work to run commands on your computer. Well, the title sounded like fun so I read on a little. They say it may take around an hour to set up, so perhaps I will do it on a weekend. What made me smile was the following excerpt:

"Note that you will have to be running this program in a relatively noise-free environment, so plug in your headphones and make sure your CD drive is spun down. If your laptop’s battery is on fire, try unplugging the smoke detector before running this program."

I might try and stick more subtle humour like this into the stuff I make for Brain Bakery.

Bookmark and Share

Facebook Applications - Feeds and SMS

Tuesday, June 26th, 2007

I wrote two facebook applications yesterday. Yes, you heard me - two, yesterday. Admittedly I did work for 12 hours almost solid yesterday.

One is an application that lists new posts on blog feeds that you are interested in, and the other allows you to SMS your facebook page to tell your mates "I’m down the pub, why don’t you join me?" They are both, obviously, in their infancy, and the SMS one took me another days coding a few weeks ago (by day, I mean 10-12 hours…) to get SMSs sent to my phone to hook up to my computer well… but it works very well now, tested it last night!

So… watch this space. When they get a little more professional I will consider releasing them. Before then, I need a new phone that I can dedicate to the purpose, or a Nokia usb data cable for my 3310.

Bookmark and Share

Microsoft Surface Parody Video

Sunday, June 24th, 2007

A few days ago I was pretty amazed by video’s of Microsoft’s new technology, which they call "Microsoft Surface," see TechCrunch. Here’s what it can do:

Well, thankfully, I have seen the light since then, due to this video, thanks again to TechCrunch:

But I still think this technology is pretty amazing! Can’t wait for Linux people and hardware enthusiasts to make a competing platform for a fraction of the cost… I can dream, can’t I?

Bookmark and Share

Recording your computer screen under linux

Friday, June 22nd, 2007

From my daily read of  Lifehacker, I noticed a program called "recordMyDesktop" - this would be very useful for someone trying to demonstrate how to do something on a linux computer to a large number of users, for example when trying to make a video manual for a website.

Ubuntu users can install recordMyDesktop and gtk-recordMyDesktop with:

sudo apt-get install recordmydesktop gtk-recordmydesktop

Thanks again, Lifehacker!

Bookmark and Share

RESULTS DAY!

Friday, June 22nd, 2007

That’s right! It’s Uni results day! I’m sure you are all on the edge of your seat, so I shall put you out of your misery swiftly!

I got a first!

And a strong one at that: I averaged 83.2%! To see my individual results, see my main site, but basically my highest grade last semester was 90% and my lowest 62 (second lowest 82…). I also got the

"School Applied Prize"

That means I got the highest grade this year in applied mathematics at Southampton University!

Out of the 112 people on the mathematics board, I was one of 7 to get a prize! Quite an honour. Here are the prizes, with the other winner’s names smudged out to protect their privacy:

And here is my line on the results page:

That’s Benjamin Gillam, course: Mathematics with Physics (BSc), grade: First, and a prize.

Rumour has it the prize is £50, which would be nice! :-)

I’m very happy with my grade, though I was aiming for 85%. Still, best grade in applied mathematics is pretty good!

I have enjoyed my stay at Uni, but now I really want to get into doing some really cool new technologies for Brain Bakery Ltd, and fortunately, that is what I will be doing!

Bookmark and Share

WebDAV on Windows XP SP2

Thursday, June 21st, 2007

When Microsoft released Windows XP’s SP2, they broke a lot of stuff. Amongst it was WebDAV Basic Authentication. They "disabled it by default" because it is a "security risk." Well, that is true, but there is no option in menus anywhere to re-enable it. You have to do a registry edit! Well… I have just had to write the following excerpt for BrainBakery’s CMS product:

If you are using Windows XP SP2, and you cannot connect to WebDAV, then you need to enable "Basic Authentication" by running this command (all one line) from Start -> Run:

REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\WebClient\Parameters" /v UseBasicAuth /t REG_DWORD /d 2

And then restart your computer. More information can be found here: http://support.microsoft.com/kb/841215.

Now, this is not live yet, as I have not tested it. I will test it as soon as Jem stops playing The Sims 2, so I can use her Windows XP machine. Here’s hoping it doesn’t break anything.

Incidentally, if you are wondering why I used "2" and not "1", the reason is that 2 enables Basic Authentication over plain HTTP (not HTTPS) under Windows Vista. I don’t know if the same command will work for Vista though.

This post could be useful for users of the PEAR module HTTP_WebDAV_Server, as that only currently supports Basic Authentication, I think.

If you find this useful, please leave me a comment! Thanks.

Bookmark and Share

SCARF Camp - Photos

Thursday, June 21st, 2007

So I have finally got round to getting the last few months worth of photos off of my phone. This is the badge that Jem made me.

Continue reading below for some more!

The field had quite a few rabbit holes:

Here is the one on the right, in close up:

That is Jem’s foot next to it to give some scale (mind you, she has tiny feet…)

Here is the campfire that we all sat around, after it had been buring for a while. It was quite big, though not as big as two years ago. We sat around it eating fish (or sausage) and chips. Yum yum!

On Sunday, a young girl called Chloe made me a card to say ‘thank you’; here is the outside (consider it folded in half):

And here is the inside:

When we got home, there was a huge spider to greet us!

Of course, it was my job to take it outside, though I must admit I put it in an ice-cream box first! And I call myself a Scout…

Anyway that’s enough of that, I best get back to work!

Bookmark and Share

SCAM? 08450229900 ‘prank’ phonecall

Wednesday, June 20th, 2007

So, I just received a missed call from 08450229900. It lasted 1 second. I immediately Google’d the number to try and find out what company it was, but I only got one hit, the comment at the bottom of this post:

http://www.ektopia.co.uk/ektopia/archives/2004/11/12/say-no-to-0870

The comment was also left today, just 20 minutes ago. I wonder what that was all about? I guess, like this guy, that it is a scam. Anyone else had this phone call?

Bookmark and Share