mod_pagespeed - How can I set Expires and cache update time?

Vladislav Romarov picture Vladislav Romarov · Jul 17, 2012 · Viewed 7.4k times · Source

I have interested in two things:

T.1) mod_pagespeed has installed the Expires header value for each file except html,xml..., documents.So how can I change this time for each file and folder ? Can this module do that ?

I hope example need not for the first task.

T.2) mod_pagespeed updates each cached file after 3-4 minutes it was changed. So if I change content of file then it updates after refreshing after 3-4 minutes. How can I control this time by this module ?

Example for second question:

I have saved JPEG image called test.jpg,refreshed in browser like that:

localhost/images/test.jpg

Then I change this file with image editor and re-save it. Now this file has saved in browser`s cache and will be rendered in browser after 3-4 minutes. So I want to control this time for each file and folder.

P.S: Guys,I really need yo help ! Killed whole week trying to solve this trouble.

Answer

sligocki picture sligocki · Jul 17, 2012

A little background: mod_pagespeed extends the cache lifetime of resources and adds a unique hash code to the resource name to improve user cacheability and thus speed up the page load for returning users.

As you've pointed out there are two distinct cache lifetimes that are important to mod_pagespeed.

(T.1) is the cache lifetime mod_pagespeed sets. It is currently set to 1 year and I don't think it is customizable. But since we add in the unique hash code to the URL, you should not need to customize this in most cases. As soon as mod_pagespeed realizes that the resource has changed, it will put a different URL into the HTML file and thus update the user caches.

(T.2) is the original cache lifetime of the resource (by default 5 minutes). mod_pagespeed reloads the file whenever it expires, and thus you see that 3-4 minute lag. There are a couple different ways you can improve this. The best is if the files are completely static, you can use ModPagespeedLoadFromFile. When you use that directive, mod_pagespeed will check the file every time it rewrites HTML, meaning the resource will be updated instantly. If you cannot use LoadFromFile, you can explicitly set shorter cache lifetimes for your resources in Apache. For example:

<Directory ".../foo/">
  ExpiresByType image/gif "modification plus 3 minutes"
  ExpiresByType text/css  "modification plus 1 minute"
</Directory>
<Directory ".../bar/">
  ExpiresByType text/css "modification plus 5 minutes"
</Directory>

However, note that if you reduce this time, you will make mod_pagespeed request the resource more often (every 1 minute for CSS files in foo/ directory above) and can put more load on your server unnecessarily. Another solution is to clear mod_pagespeed cache during development. This can allow you to update the server quickly when developing, but not make your server unnecessarily burdened the rest of the time.