Twilio supports caching of your Programmable SMS media 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.
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 a media 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 a media 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 media file's name or changing the file location on your web server as a timely workaround.
Say the media file name is /AAAA.jpg
, 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.jpg with /BBBB.jpg
, which will force Twilio to execute the new caching instructions. This technique will work even if the underlying media is the same.