1
votes

I have a systen in place where I have two-layered cache on the website. Every request checks for cache on the web server and if it finds the generated page that has been cached, it returns the cache instead of generating it again.

For example:

  1. User A visits website URL http://www.example.com/home/
  2. Server does not find cache
  3. Server generates the page
  4. Server writes the page to cache
  5. Server returns the generated page to User A
  6. User A is happy

and

  1. User B visits website URL http://www.example.com/home/
  2. Server finds cache
  3. Server returns the cache instead of generating the page again
  4. User B is happy

All that works without problems. But I also want to add an option that browser would not ping the server again (saving servers time of checking if cache exists or not) and use its own cache instead.

  1. User A visits URL http://www.example.com/home/ again
  2. Browser has that page in cache
  3. Browser loads the page for the user from cache

I cannot get the latter working. During original page generation, I am sending to the user with the page the following headers:

header('Cache-Control: public, max-age=10000, must-revalidate');
header('Expires: Fri, 03 Feb 2012 01:59:45 GMT');

But when I check for it with Firebug or Chrome Developer Tools it does not say it is using a cache, instead asking for the data from the server again. I know I must be doing something wrong, since I have the same thing set up for static files like Javascript, and that works.

To test this I didn't just try reloading the page, I created links on the website and moving between those links it asked for the pages from server each time.

Am I missing something?

EDIT:

Alright, apparently what happened was that server sent "Pragma: no-cache" automatically every time. Does anyone know why server would do that? This kept the browser from using cache.

2
Just a thought, but are you refreshing the page in Firebug/Chrome developer with CTRL-R? That will force a reload, ignoring any cache..konsolenfreddy
You can try header('Pragma:') to delete the header but depending on where the header is generated, this may not work. You should also be able to check for/respond to an If-Modified-Since: header in order to implement this reliably.DaveRandom
I think the must-revalidate directive is causing the behaviour. As I understand, the directive causes the browser to revalidate the page against the server before serving it, though it lets the browser to cache the page - w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.4. Makes sense?Abhay

2 Answers

2
votes

If session is enabled for that page/url, the Pragma: no-cache-header will be added to the http header which prevents the browser from using the cache.

2
votes

if you use session_start PHP will add

Expires: Thu, 19 Nov 1981 08:52:00 GMT
Pragma: no-cache
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0

the whole point of PHP is to provide dynamic pages after all.

to stop this ...

session_cache_limiter(''); 
session_start();

and you can then write your own headers based on the content you provide