views:

80

answers:

4

tl;dr: I want to load an XML file once and reuse it over and over again.

I have a bit of javascript that makes an ajax request to a PHP page that collects and parses some XML and returns it for display (like, say there are 4,000 nodes and the PHP paginates the results into chunks of 100 you would have 40 "pages" of data). If someone clicks on one of those other pages (besides the one that initially loads) then another request is made, the PHP loads that big XML file, grabs that subset of indexes (like records 200-299) and returns them for display. My question is, is there a way to load that XML file only once and just reuse it over and over?

The process on each ajax request is:
- load the xml file (simplexml_load_file())
- parse out the bits needed (with xpath)
- use LimitIterator to grab the specific set of indexes I need
- return that set

When what I'd like it to be when someone requests a different paginated result is:
- use LimitIterator on the data I loaded in the previous request (reparse if needed)
- return that set

It seems (it is, right?) that hitting the XML file every time is a huge waste. How would I go about grabbing it and persisting it so that different pagination requests don't have to reload the file every time?

+1  A: 

I believe the closest thing you are going to get is Memcached.

Although, I wouldn't worry about it, especially if it is a local file. include like operations are fairly cheap.

Chacha102
Well it is simplexml_load_file that is doing the loading, not quite like an include and it seems that loading many thousands of records would be less than cheap. I'll see what memcached has to offer.
rg88
A: 

Could you load it into $_SESSION data? or would that blow out memory due to the size of the chunk?

niggles
+2  A: 

Just have your server do the reading and parsing of the paginated file based on the user input and feedback. Meaning it can be cached on the server much quicker than it would take the client to download and cache the entire XML document. Use PHP, Perl, ASP or what have you to paginate the data prior to displaying it to the user.

drlouie - louierd
+1  A: 

To the question "hitting the XML file every time is a huge waste" then answer is yes, if you have to parse that big XML file everytime. As I understand, you want to save the chunk the user is interested in so that you don't have to do that everytime. How about a very simple file cache? No extension required, fast, simple to use and maintain. Something like that:

function echo_results($start)
{
    // IMPORTANT: make sure that $start is a valid number
    $cache_file = '/path/to/cache/' . $start . '.xml';
    $source     = '/path/to/source.xml';
    $mtime      = filemtime($cache_file);

    if (file_exists($cache_file)
     && filemtime($cache_file) < $mtime)
    {
        readfile($cache_file);
        return;
    }

    $xml = get_the_results_chunk($start);
    file_put_contents($cache_file, $xml);

    echo $xml;
}

As an added bonus, you use the source file's last modification time so that you automatically ignore cached chunks that are older than their source.

You can even save it compressed and serve it as-is if the client supports gzip compression (IOW, 99% of browsers out there) or decompress it on-the-fly otherwise.

Josh Davis
But would $xml = get_the_results_chunk($start);file_put_contents($cache_file, $xml);Be much of an improvement over just my original:simplexml_get_file();I'm not sure it would be.
rg88
I don't know what your simplexml_get_file() function does. By caching the chunk, you save 99% of the processing the next time it is requested. If you prefer, you can also precache the whole 40 chunks at once, everytime the source document changes.
Josh Davis