SUPPORT.TWILIO.COM END OF LIFE NOTICE: This site, support.twilio.com, is scheduled to go End of Life on February 27, 2024. All Twilio Support content has been migrated to help.twilio.com, where you can continue to find helpful Support articles, API docs, and Twilio blog content, and escalate your issues to our Support team. We encourage you to update your bookmarks and begin using the new site today for all your Twilio Support needs.

How can I change the cache behavior of audio files?

Twilio supports caching of your Programmable Voice audio files. Once properly cached, you will see a shorter request time in Twilio’s 200 OK response. 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. 

 

Cache static TwiML and media for play verbs

If you are using <Play> verbs, we recommend hosting your media in AWS S3 in us-east-1, eu-west-1, or ap-southeast-2 depending on which Twilio Region you are using. No matter where you host your media files, always ensure that you're setting appropriate Cache Control headers. Twilio uses a caching proxy in its webhook pipeline and will cache media files that have cache headers. Serving media out of Twilio's cache can take 10ms or less. Keep in mind that we run a fleet of caching proxies so it may take multiple requests before all of the proxies have a copy of your file in cache.

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 to see if the file has changed. This allows 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 to your server's response:

Cache-Control: max-age=259200

This tells Twilio to use the cached version of the file until 259200 seconds have elapsed (259200 equaling 60 seconds x 60 minutes x 24 hours x 3 days).

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, we recommend changing the audio file's name or changing the file location on your web server 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 can simply replace all references to /AAAA.wav with /BBBB.wav, which 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