views:

74

answers:

4

I'm attempting to make a php script that can load the current weather forecast and it uses a bit of XML pre-processing to digest the input, however it is accessed quite often and reloaded. The problem begins with my current host, which yes I do understand why, limits the amount of processing power a script takes up.

Currently takes an entire process for ever execution, which is around 3 seconds per execution. I'm limited to 12, yet I get quite a few pings.

My question to you guys is: What methods, if any, can I use to cache the output of a script so that it does not have to pre-process something it already did 5 minutes ago. Since it is weather, I can have a time difference of up to 2 hours.

I am quite familiar with php too, so don't worry xD.

~Thank you very much, Jonny :D

A: 

need a code snippet to see what kind of processing you are doing. consider using xdebug to better optimize your code. Also you may use a benchmarking tool such as AB to see how many processes your server can handle.

there are several different caching mechanisms available but without seeing what kind of process you are doing it is hard to say...

Mohammad
+2  A: 

You could run a cronjob that would generate the weather forecast data and then just display the whole thing from cache. You could use APC so it is always loaded in memory (plus all other added advantages).

Miha Hribar
Ah that would probably work, thanks :D
JonnyLitt
Otherwise, an php-only alternative could be to use `filemtime()` to return a cached file or regenerate after a period.
Nick Bedford
A: 

3 seconds is an extremely long execution time, as already asked, some cold would be nice to see how you process the 'input' and in what format said input is in.

A quick and dirty article about caching out of script to file is found here:

http://codestips.com/?p=153

theraven
+1  A: 

The Zend Framework provides the Zend_Cache object with multiple backends (File, memcached, APD). Or you can roll your own with something like:

$cachFile = "/path/to/cache/file";
$ttl = 60; // 60 second time to live
if (!file_exists($cacheFile) || time()-filemtime($cacheFile) > $ttl) {
    $data = getWeatherData(); // Go off and get the data
    file_put_contents(serialize($cacheFile), $data);
} else {
    $data = unserialize(file_get_contents($cacheFile));
}
Neel
Yes, I may possibly do something in which I'll use a text/plain medium to store the output as a cache-type object, and then update it when time comes through each execution.
JonnyLitt
Or you can just serialize the whole thing to file, I've edited the answer to match.
Neel