3 Simple Steps to Leverage CDN Caching
It is important to properly leverage the CDN caching mechanisms for best performance. This article provides you a best-practice guideline on how to set the Cache-Control
header and how to handle cached assets.
1. Use the Cache-Control
header
In general we recommend to leave the default cache related settings of your Zone as they are, such as the Expire
, Max Expire
, and Ignore Cache Control
settings.
Setting Ignore Cache Control
to disabled will enable the processing of Cache-Control
response header fields from the origin server, such as Cache-Control
and Expires
. Avoid to use the private
or no-cache
directive because they force a CDN to not cache your content, thereby not accelerate your assets. The Cache-Control
and Expires
headers should have the following format:
Cache-Control: max-age=86400
Expires: Tue, 12 May 2015 09:47:12 GMT
2. Set the expiry of at least a week
Setting the expiry to a week (604800 seconds) is a recommended value for assets that do not change often.
Cache-Control: max-age=604800
The Expire
setting defined in your Zone settings has only an impact on the web browser cache and not on the KeyCDN cache. The Max Expire
setting specifies the maximum time that cacheable HTTP documents will be retained without checking the origin server. You can set the value of both directives to 10080 minutes.
3. Purge updated files instantly
You can purge files in the KeyCDN dashboard or through the API if you want to delete the files from the CDN cache before they expire. The purging from the CDN cache happens instantly. You can decide to purge specific files or the complete cache of your Zone.
We are providing various plugins to simplify the purging through our API:
KeyCDN API adapters
Simply integrate the KeyCDN API with any of the following adapters into your application.
Task automation plugins
Frontend automation systems are commonly used. Grunt is rather a task based system where Gulp is based on streams. The following plugins help you to easily integrate them into your task flow.
- Grunt: grunt-keycdn
- Gulp: gulp-keycdn