Simple caching in PHP5
class cache {
function __construct($path=’.') {
$this->path = $path;
}
function cache($name, $callback, $args) {
if (file_exists(“$this->path/$name”)) {
$this->callbacks[$name] = array($callback, $args);
return implode(”, file(“$this->path/$name”));
} else {
return $this->save($name, $callback, $args);
}
}
function save($name, $callback, $args) {
$text = call_user_func($callback, $args);
$fh = fopen(“$this->path/$name”, ‘w’);
fwrite($fh, $text);
fclose($fh);
return $text;
}
function __desctruct() {
if (is_array($this->callbacks)) {
foreach ($this->callbacks as $name => $callback) {
list($callback, $args) = $callback;
return $this->save($name, $callback, $args);
}
}
}
}
You call it using:
$cache = new Cache();
$cache->cache('MyFunc.txt', 'MyFunc', 'an argument');
All it does is save the output of the function to a file with the given name. The clever bit is that the output it sends to the browser is the content of the file; it doesn't actually call the function until the end of the script execution and so doesn't slow the output to the browser down.
Start sending output.
Output cached result of function.
Finish sending output.
Call function and save to the cache file.
It means the content will always be slightly out of date, but in a lot of situations that may not be a problem.
Quick note, this is only really possible (at least the way I've done it) in PHP5 because it requires destructor support.
Why people think Finnish is a hard language
This page lists the 2253 forms of the word “kauppa”, Finnish for “shop”.
Finnish is highly agglutinative so having a lot of forms for each word is expected
It isn’t as bad as you think since Finnish suffixes embody concepts most languages use several words for. i.e. Finnish uses longer words in most sentences but there are fewer of them.
This is cleverly embodied in the quote from this site:
“Why lose time and energy saying ‘the committee that takes care of negotiations concerning the truce’ when you can use a simple little word like ‘aseleponeuvottelutoimikunta?’”
Beam me up Scotty
For more information visit CNN
Google Moon
Google have put a load of satellite images of the moon along with data of various moon landings to provide us with Google Moon.
Plugins
Technorati tags to posts easily, and the other is bstat, a nice stats plugin.
So we’ll see if they work.
New OrbWars/Galaxia Forums
Galaxia and OrbWars forums have merged. Not terribly apparent since there haven’t been any Galaxia forums for a long time and as such it only has one actual forum.
But still, check it out:
http://forums.orbwars.co.uk]]>
IC to host IVDC
Imperial College‘s bid to host the Inter Varsity Dancesport Competition in 2006 has been succesful and I’m on the “support team”
Wohoo!]]>
Public development version of Galaxia Reborn
Signup works but doesn't actually give you anything you can use so just login as "Q" with password "q".
You can move ships around and build more at the colony (you have to find the colony first though :P) although you can't send them into space just yet. Also, the entire interface has been translated into Finnish (well almost. Two little bits were added afterwards but they'll get done soon enough). Thanks for that go to my girlfriend Julia. If anyone is interested in translating the interface into a different language let me know
PHP5
I now have PHP5 installed alongside PHP4. It lowers efficiency a bit since it's installed as CGI instead of an Apache module but for testing it's fine :)
For anyone planing on doing this who isn't too familiar with installing stuff via the command line on Unix-like OSes (like me), there is a very usefulguide from Scott Huring.]]>
Wohoo, MySQL 4!
This is all preparation for installing PHP5 so everyone can actually see Galaxia Reborn :)
