3

I have a php generated javascript page that is creating an array from a database and is absolutely murdering my page load time. The page has a php extension and is currently using a header to mark its content-type as application/javascript.

I found a possible solution online, but it doesn't seem to be doing much to speed up my page's load time. The header code of my file right now is this:

header("Cache-Control: must-revalidate");
$offset = 60 * 60 * 24 * 3;
$ExpStr = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
header($ExpStr); 
header("Content-type: application/javascript");

Is there anything special I need to be doing to cache the file so I don't have it constantly trying to load these database calls? I'm using IIS 7.5 and PHP 5.3.13.

5
  • Consider reading the rest of the HTTP/1.1 spec on caching. It's possible you're receiving a "conditional GET" request that's asking to revalidate a cache entry. (This happens when you press the "Reload" button, instead of going to the address bar and pressing Enter.) You might be respond to a conditional GET with a fresh response; you could instead check for this explicitly, and return an empty HTTP 304 response. Basically, just setting "Expires" is rarely sufficient for caching. Commented Jan 5, 2013 at 17:59
  • Maybe codereview.stackexchange.com would be applicable in this case... Commented Jan 5, 2013 at 18:00
  • To Dumb Products: I'm using the SQLSRV extension Commented Jan 5, 2013 at 18:00
  • Also, in addition to random internet advice, I'd recommend hitting the spec on HTTP caching: w3.org/Protocols/rfc2616/rfc2616-sec13.html. It's not a light read but it's pretty understandable as far as specs go. Also, Google's resources seem good: developers.google.com/speed/articles/caching and developers.google.com/speed/docs/best-practices/caching?hl=en Commented Jan 5, 2013 at 18:01
  • To cache database queries you'd use Memcached Commented Jan 5, 2013 at 18:18

3 Answers 3

1

It seems to me that you are hardcoding the array in tags. If the array is really big, the browser have to load more bytes.

Consider using AJAX in conjunction with JSON. Use forexample jQuery to load the data from another script. E.g. api.php?req=getBigArray. And jQuery "success" callback to run logic when array is loaded. This means that two http requests will be done, but it will load your page at once.

Serverside:

<?php //api.php
switch($_GET['req']){
 case "getBigArray": 
    $arrayFromDatabase = array( /* Load from db ... */ );
    echo json_encode($arrayFromDatabase); 
 break;
}

Client:

$(document).ready(function(){
    $.getJSON('api.php?req=getBigArray', function(data) {
        console.log(data); // Use data in some way.
    });
});

This also decouples logic from serverside / frontend.

You can also look at memcache/apc if you want to cache results at the backend. Really simple API, but requires extra software installed on the serverside.

Sign up to request clarification or add additional context in comments.

1 Comment

It looks like I'm going to have go the AJAX route here. The caching methods listed above work somewhat, but yeah, the DB calls seem to be expensive and force me to have to go a different route than I had originally planned. Going with a bit different logic to fit my needs, but this seems to be the best and easiest way for me to get my page load times down.
1

If the database queries are that expensive, the simplest way is to write the whole generated output into a file and just output the file instead of querying the db every time the page is loaded. All you have to do then is to delete / update the file when the database changes.

You should also consider looking into memory caches and try to optimize your queries if possible.

Comments

1

i have always used cache-control with the max-age directive.

header('Cache-Control: max-age=' . $expires);
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT');

but i also agree with the previous comment. if your db calls are really that expensive, think about offsetting how often those calls are made. for instance, if you wanted to update your db cache once every day, i'd do something like this.

  • write the results of your db query to a file and name it based up generation date.
  • in your the file that does these calls, have it look at the generation date of the cached file, and if it's older than a day regenerate it. if it's younger just read/use the cache.

you can optionally you can have IIS run the update script via the task scheduler, then your script that makes the js can never do the age check, just read from the cache.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.