Search code examples
phpdata-access

Saving and restoring a class in PHP as an optimisation technique?


I have a web page in a PHP app that accesses a proprietary format data file, parses it, and generates a result based on user input. It works perfect.

However, the data file is now getting bigger and bigger, and taking longer to parse. User requests are also on the increase.

Before it all gets out of hand, I'm wanting to optimise the app a little without spending too long doing it.

At the moment, the data file is parsed inside a class and that class knows everything about the file.

The output only needs to be accurate to within 4 hours of the data file.

The quickest way to optimise this to me would be to load and parse the file on a cron event each hour and save that data somewhere which the web page can load much faster, thus meaning parsing the file once per hour instead of at every request.

What is the best way of doing this?

I instantly thought "database", but that seems like a lot of work trying to store all the data, and any of the data can change in the file, so rather than trying to compare data I would have to drop the table each hour and repopulate, a chance where the web app could then return incorrect data to the user.

So then I thought is it possible to take a class, save it to disk, and the web app re-load that data into memory when it requires it ? Meaning very little modification of the app (and a small script run on a cron).


Solution

  • You could serialize the class but that's not really recommended.

    As you're using php and are only interested in the result set then the easiest would be to store the data using uapc, it's just a key/value store in memory.

    Example:

    function GetData() {
        $cached = apc_fetch('myParsedData');
        if(!empty($cached)) {
            return $cached;
        }
        $data = file_get_contents(...);
        $result = Parse($data);
        apc_store('myParsedData', $result, 3600);
        return $result;
    }
    
    function Parse($data) {
        //do something
        return $result;
    }
    

    This will store the data for 3600 seconds, and when it's empty it'll load it from disk.