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:
- User A visits website URL
http://www.example.com/home/
- Server does not find cache
- Server generates the page
- Server writes the page to cache
- Server returns the generated page to User A
- User A is happy
and
- User B visits website URL
http://www.example.com/home/
- Server finds cache
- Server returns the cache instead of generating the page again
- 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.
- User A visits URL
http://www.example.com/home/
again - Browser has that page in cache
- 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.
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 anIf-Modified-Since:
header in order to implement this reliably. – DaveRandom