The Wolverhampton Hell’s Angels are shaking our windows and foundations with their stomach churning firework display, which I would be watching now rather than typing this, but for the trees at the back of the garden that are in the way!
This post comes in too parts, the good news and the bad news.
First the good news. At lunchtime, Rich and I had a chat while doing our lunchtime circuit of the business park, and we were talking about ways to make Colourphon quicker. We know we can do the analysis which takes a bunch of coloured pixels and puts a human friendly name to the most frequently occurring in the image. But the problem was that this was taking upwards of 30 seconds, so PHP, quite rightly, kept throwing back a maximum execution timeout error.
Rich has been thinking alot about application architecture, and in particular memcached.
So tonight, on my ubuntu development machine, I installed memcached
sudo apt-get install memcached
I installed a pecl extension for PHP.
sudo pecl install memcache
Then I added a function to instantiate a Memcache object with an array of servers, finding a neat way to get an array stored as a constant, by having the constant contain an object reference that could be evaluated.
$arr=array(1,2,3);
define("ARRAY_CONSTANT","return ".var_export($arr,1).";");
Then when we want to use the constant we simply evaluate it.
foreach(eval(ARRAY_CONSTANT) as $val ){
//do stuff with $val
}
The upshot is:
1st page load: 29.94 seconds – including several hundred calls to the class with most processing overhead.
2nd page load: 1.61 seconds – with not a single call to the class with most processing overhead!!
So if you need massive performance boost, use memcache – originally desiged for database cals, but if you want to cache a bunch of frequently used data – even objects, simply serealize and deserialize when you need it.
It has taken longer to write and proof read this post than it did to add the 12 lines of code to get it to work.
The bad news? This isn’t live on a public machine yet
.