Tropo is part of CiscoLearn More

Caching Tips & Techniques

Typically, any external files used by a Tropo application (audio files, externally hosted scripts) get cached by our cache servers for quick and easy retrieval. In general terms, a cache server sits between a web server and a web browser. It acts as an intermediary in the web page loading process, invisibly accepting a request from a browser on behalf of the web server containing the actual content. This caching server watches all requests for web pages and audio files and saves a copy for itself as they go by. If there's another request for the same object, the local copy can be used instead of asking the web server for it again. This can dramatically improve performance and is especially important when it comes to audio latency - you don't want your audio files to stutter or pause.

Most of the time, when a file changes on the web server, the cached file will determine whether it needs to update the cached copy by looking at the Last-Modified date and the Etag. However, in some instances - such as when you're serving out of a CDN (Content Delivery Network) that has very aggressive caching enabled, or when you're serving a dynamic file like a PHP script - you may encounter situations where the caching prevents your users from accessing your updated file.

There are several ways around this caching dilemma, all of which involve HTTP header manipulation. HTTP headers are meta-tags that get passed every time an HTTP request is made. Every web server passes these HTTP headers and the headers tell a browser how to handle the content it receives in the body of the request. Below is an example of the HTTP headers being passed by Microsoft Internet Information Server 5.0.

    HTTP/1.1 200 OK
    Server: Microsoft-IIS/5.0
    Cache-Control: no-cache
    Expires: Fri, 07 Dec 2001 23:34:20 GMT
    Connection: close
    Date: Fri, 07 Dec 2001 23:34:20 GMT
    Content-Type: text/xml
    Accept-Ranges: bytes
    Last-Modified: Fri, 07 Dec 2001 20:04:41 GMT
    ETag: "9cd56754d837c11:96b"
    Content-Length: 632

Notice in particular the Cache-Control: and Expires: headers. They are the key to controlling the actions of the caching servers. What the cache server sees in the returning HTTP headers determines what it does with the document. If you configure your HTTP Cache-Control header as no-cache, Tropo will always return to your web server for the latest documents. Additionally, the HTTP Expires header can provide you even more granular control over how long a document should remain in the cache. By setting the Expires header to a fixed point in time, you can control how long a document remains in the cache. After the time you specify, further fetches for the document will come from the web server rather than the cache.

Here's an example showing how to set your HTTP headers in a PHP script:

    <?php
    header('Cache-Control: no-cache');
    print('<?xml version="1.0" encoding="UTF-8" ?>');
    ?>

An excellent resource for all things HTTP caching related can be found at http://www.mnot.net/cache_docs/ - if you’re unsure about the methodology behind caching in general or how to set the appropriate HTTP headers, it’s definitely a recommended read.