Caching 'large' amount of data

Morning all,

I’m working on building a page that displays user reports. Reports are of variable length (1 to hundreds of pages) and I would like to cache all the report data (load the full report) before displaying it.

Each report page will come back separately in html format, so I can exploit a php loop to retrieve all the report data before displaying anything on the page. However, this is where my problem begins.

I would like to show only page 1 when the report is opened and cache the full report somewhere.
I then want to add a control that allows a user to flip to page 2,3,etc…, however, I don’t want to invoke a server call at this point (my current implementation will make a server call for each page individually).

The difficulty is with the fact that I can’t populate a php array and invoke this in a JavaScript-kind-of way, because the php variables will be gone at the point the page is loaded. Also, due to the potential size of reports (hundreds of pages each containing many lines of html) I am also unsure about creating JavaScript dynamically.

What would be the best way to go about this?

Thanks in advance for any input.

Update:

I am now using the $_SESSION variable to cache my entire reports (which could reach sizes of 1-2 MB). The essence is coded as follows:

[php]
if (!isset($_SESSION[“reportcache”]))
{
$FullReport = $client->runReport($uri, ‘html’, null, $controls);
$reportcache = array();

	while ($reportpage = GetNextPage($FullReport))
	{
		$reportcache[] = $reportpage;
	}

	$_SESSION["reportcache"] = $reportcache;
}

[/php]

Each time the user ‘flips’ to a new (report) page, the (web) page will post back and the specified (report) page will be retrieved from the $_SESSION variable using the index:

[php]echo $_SESSION[“reportcache”][$page - 1];[/php]

This all works like a charm, but due to having limited data available I can only test this for reports up to +/- 150 pages. In the ‘real’ situation a report could be much (10x , 100x?) larger. Is the $_SESSION variable suitable to hold so much data, or are there better alternatives?

Note: I do unset() the $_SESSION[“reportcache”] variable just before the first time a report is run, so this solution does work for multiple reports. I’m just concerned that I’m misusing the $_SESSION variable at this point.

Thanks!

A common caching method is simply to create local cache files with serialised/gzipped contents. You can then in your AJAX call just read the contents of the appropriate file without having to open a database connection and/or run a query.

Variable can hold extreme amounts of data (I myself have had an array storing ~45mb fairly recently), however whether that makes the use of the $_SESSION variable an okay solution, I wouldn’t like to say.

If you’d like to change to the file method, I have a custom solution that I wrote myself that I can send/link to you, but if this works and is having no impact on the server/user experience then I personally wouldn’t push your decision in either direction.

Sponsor our Newsletter | Privacy Policy | Terms of Service