Cache-control vs. Expires

If you want browser and cache devices to cache some of your resources, you have two options one is using Expires header and other is using max-age directive in cache-control header. Consider following things before deciding on whether you should use max-age or cache-control header

  • The Expires header is deprecated in the Http 1.1 specification

  • The Cache-control header was introduced in HTTP 1.1 specification, and there are quite a few HTTP 1.0 devices out there and some of them dont understand Cache-control

  • The HTTP 1.1 Header definition says this "If a response includes both an Expires header and a max-age directive, the max-age directive overrides the Expires header, even if the Expires header is more restrictive."

  • The cache-control directive gives you more fine grained control over the different aspects of caching.

I wanted to see how the browser reacts if i set both Expires and Cache-control header at the same time. I did create a servlet which sets

public class ResourceServingServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("Entering ResourceServingServlet.doGet()");

System.out.println("Request path " + request.getPathInfo());
System.out.println("Query String " + request.getQueryString());
response.setHeader("Cache-Control", "max-age=2592000");
response.setHeader("Expires", "Thu, 25 Jul 2010 18:26:10 GMT");
getServletContext().getRequestDispatcher("/js/test.js").include(request, response);
System.out.println("Exiting ResourceServingServlet.doGet()");

I am setting cache-control header with value of max-age equal to 2592000, which means 30 days. Then i set Expires date to Thu, 25 Jul 2010 18:26:10 GMT, and when i tried accessing this servlet on 19th of July, the Expires header was supposed to expire the content in 6 days.

When i tried accessing the ResourceServlet these are the headers that i got in the Firebug

As you can see i am setting both Expires and Cache-control header and value of Date field is 19th of July 2010, which means the response was returned from the server on 19th of July 2010. When i looked the cache entry in the firebug this is what i see

The Firefox entry is about to expire on the 18th of July 2010, which is 30 days. That means firefox ignores the value of Expires header even though it is more restrictive.


srjwebsolutions said...

We are leading responsive website designing and development company in Noida.
We are offering mobile friendly responsive website designing, website development, e-commerce website, seo service and sem services in Noida.

Responsive Website Designing Company in Noida
Website Designing Company in Noida
SEO Services in Noida
SMO Services in Noida

Vikas Chaudhary said...

Battery Mantra is Authorized exide car battery dealer in Noida and Greater Noida. We are providing our service in Indirapuram, Delhi, Ashok Nagar.

Exide Battery Dealer in Noida
Battery Dealer in Noida
Authorized Battery Dealer in Noida
Car Battery Dealer in Noida
Car Battery Dealer
Exide Battery Dealer

EG MEDI said... is online medical store pharmacy in laxmi nagar Delhi. You can Order prescription/OTC medicines online.
Cash on Delivery available. Free Home Delivery

Online Pharmacy in Delhi
Buy Online medicine in Delhi
Online Pharmacy in laxmi nagar
Buy Online medicine in laxmi nagar
Onine Medical Store in Delhi
Online Medical store in laxmi nagar
Online medicine store in delhi
online medicine store in laxmi nagar
Purchase Medicine Online
Online Pharmacy India
Online Medical Store