0

I have a dynamic php site which generates a json string. So if a javascript does 10 requests to this site per minute the json string is generated and echoed 10 times.
I want to limit the requests going through to my server to 1 request per minute.
I thought the cache control header would do the job, but it looks like I'm wrong.

Here is what I tried. I set my php page to this:

<?php 
    header("Cache-Control: max-age=60");
    echo "{'test':'abc'}";
?>

Loaded the site with the browser; it returned {'test':'abc'} Then I quickly changed the php page to:

<?php 
    header("Cache-Control: max-age=60");
    echo "{'qwe':'123'}";
?>

Reloaded the page quickly and got: {'qwe':'123'}

So the second request went through even though the minute wasn't over. I wanted the first result to be returned from cache for one minute, without doing another request.

What am I doing wrong?

Forivin
  • 14,780
  • 27
  • 106
  • 199
  • 1
    You can't _force_ caching. All you can do is tell it the maximum time to cache _if_ it caches. – Barmar Oct 15 '14 at 19:09
  • You need to such caching server side. – CJD Oct 15 '14 at 19:11
  • @Barmar It's okay if it would simply work with standard installations of Firefox, Chrome & co. – Forivin Oct 15 '14 at 19:13
  • @CJD And how am I supposed to do "such" caching on the server side? The point is that I don't want a million requests coming at my server. It really is about the traffic. – Forivin Oct 15 '14 at 19:15
  • In your JS you could cache the result in localstorage? See http://stackoverflow.com/questions/17379312/cache-json-response – CJD Oct 15 '14 at 19:26
  • @CJD and would that work through multiple tabs? I really just want one request, even I I open the same url on 20 tabs. But how would cache:true look with raw XmlHttpReqest? I'm not using ajax directly. – Forivin Oct 15 '14 at 19:33
  • @Forivin if all your tabs are requesting the same local storage content then yes, I don't see why not. Edit: Actually scratch that, every 60 seconds, ALL the tabs would refresh. – CJD Oct 15 '14 at 19:36
  • @CJD I need to use the browser cache then, don't I – Forivin Oct 15 '14 at 20:00

0 Answers0