How can I change the cache behavior of audio files?

You will see a shorter request time in Twilio’s 200 OK response when a file is properly cached. If you change a file already cached by Twilio, make sure your web server is sending the proper headers to inform Twilio that the contents of the file have changed.

How Twilio performs cache checks

Twilio will cache files when HTTP headers allow it (via ETag and Last-Modified headers). Responding with `Cache-Control: no-cache` will ensure Twilio always checks if the file has changed, allowing your your web server to respond with a new version or with a 304 Not Modified to instruct Twilio to use its cached version.

Time-based caching of an audio file

The Cache-Control header is used in HTTP to control the behavior of caches.  The max-age directive is used to specify (in seconds) the maximum age of the content before the cache becomes stale (i.e., the content will not change for some period of time).

As an example, if you know that your content will not change for 3 days, you could add the following HTTP caching header server to your server's response, this will tell Twilio to use the cached version of the file until 259200 seconds have elapsed:

Cache-Control: max-age=259200 (259200 = 60s x 60m x 24h x 3d)

In PHP for example, you would add this line to your output:

header('Cache-Control: max-age=<new max-age value>');

Changing the behavior of an audio file that is already cached

What if you've changed a file in some way and don't want Twilio to continue honoring the existing cache directives?  Rather than waiting until the cache expires, which can be hardly ideal, changing the audio file name or changing the file location on your web server can serve as a timely workaround.

Say the audio file name is /AAAA.wav  and you have instructed Twilio to cache the file for three days. On the 2nd day, you make a change to the file and urgently need Twilio to pick this up.  You may simply replace all references to /AAAA.wav  with /BBBB.wav, it will force Twilio to execute the new caching instructions. This technique will work even if the underlying media is the same.

Have more questions? Submit a request
Powered by Zendesk