24

I'ld like to present always the latest website content to the user but also have it fast loaded. By researching I came across postings people suggesting to use the cache for speeding up loading.

So what do I need to add to my website to "overwrite" the cache after 3 days to display the latest content?

2
  • 1
    do you use wordpress? drupal? do you have a database? caching is not as cut and dry as you seem to think it is...
    – Trey
    Jun 26, 2011 at 21:19
  • Sorry. I'm using a simple PHP website without a CMS.
    – Uli
    Jun 26, 2011 at 23:17

4 Answers 4

22

The Cache-Control header is used in HTTP 1.1 to control the behavior of caches. The max-age directive is used to specify (in seconds) the maximum age of the content before it becomes stale (i.e., the content will not change for some period of time). So if you know that your content will not change for 3 days, you want your server to add the following HTTP header:

Cache-Control: max-age=259200

(259200 = 60s x 60m x 24h x 3d)

To do that in PHP, add this line to your output:

header('Cache-Control: max-age=259200');

Read here for more info on the header function:

1
  • 1
    For IIS you can set it for static content (keeping dynamic stuff dynamic) in the web.config using the format [-][d.]hh:mm:ss[.ff]. It should go under <system.webServer><staticContent><clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="3.00:00:00" /></staticContent></system.webServer> Jan 17, 2013 at 21:37
13

There is more than one way to do this - but you need to consider exactly what you need to cache and what you don't. The biggest speed increases will likely come from making sure your assets (css, images, javascript) are cached, rather than the html itself. You then need to look at various factors (how often do these assets change, how will you force a user to download a new version of the file of you do change it?).

Often as part of a sites release process, new files (updated files) are given a new filename to force the users browser to redownload the file, but this is only one approach.

You should take a look at apache mod_expire, and the ability to set expiry times for assets using the .htaccess file.

http://www.google.com/?q=apache+cache+control+htaccess#q=apache+cache+control+htaccess

3
  • Did I understand correctly that I just need to add this code to my servers .htaccess file to allow caching files for 3 days? # 3 DAYS <FilesMatch "\.(php|xml|css|js|gif|jpg|jpeg|png|ico|swf)$"> Header set Cache-Control "max-age=259200, proxy-revalidate" </FilesMatch> After that 3 days it'll all get reloaded, right? Source: askapache.com/htaccess/apache-speed-cache-control.html
    – Uli
    Jun 27, 2011 at 12:32
  • The answer assumes i am using Apache as the server, what if i am using a Ruby application? This will not work
    – Joseph
    Aug 18, 2014 at 7:18
  • Here is a blog post with clear explanations on how to set the cache headers in html and your favorite programminglanguage docs.fastly.com/guides/22951283/26628787
    – Joseph
    Aug 18, 2014 at 7:49
1

As mentioned Expires and Cache-Control Headers are usually the best way to incorporate information about information lifetime.

Because clients are not very reliable on interpreting these informations proxies with caching capabilities like squid, varnish or such solutions are preferred by most people. You also need to consider if you want to cache only static content (like images, stylesheets, ..) or dynamically generated content as well.

0

As per the YSlow recommendations you could configure your web server to add an Expires or a Cache-Control HTTP header to the response which will result in user agents caching the response for the specified duration.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.