How to use data caching during network operations (HTML)

This topic shows how to use data caching with network operations in a Windows Runtime app.

Caching network content as app data

Storing content to disk lets your app behave fast and fluidly in situations where an app is unexpectedly terminated or use of a network is limited. For example, an RSS feed reader app can immediately display feeds that were cached to disk from a previous session. Once the latest feeds are available, the app can update its content. This ensures that the user has content to view immediately upon launch while waiting for new content updates.

Windows 8.1 provides the ApplicationData class in the Windows.Storage namespace. This class provides access to the app data store. Application data is mutable data that is specific to a particular app. It includes runtime state, user preferences, and other settings. Application data is created, read, updated, and deleted when the app is running. For more information, see Accessing app data with the Windows Runtime.

Files transferred by your app through network operations can be cached as app data in either the Roaming, Local, or Temporary folders.

Note  Windows Phone 8.1 also includes a special Cache folder for files that are intentionally saved or removed by your app.

 

Folder Description
Local

Files remain on the machine on which they were originally written and are not synchronized with other devices.

Temporary

Files are subject to deletion when not in use. The system considers factors such as available disk capacity and the age of a file when determining when or whether to delete a temporary file.

Roaming

Files are synchronized across devices where users have signed in with connected accounts. Roaming of files is not instant; the system weighs several factors when determining when to send the data.

Usage of roaming data should be kept below the quota, defined by the RoamingStorageQuota property, or else roaming of data will be suspended. Files cannot be roamed while an app is still writing to them, so be sure to close your app’s file objects when they are no longer needed.

 

The code snippets below demonstrate the caching of a server response, in the form of a .txt file from a network operation, as app data to the Roaming folder. The code then demonstrates how to retrieve cached content using the associated filename.

Examples

First we define a reference to Roaming folder. Next, our cacheResponse example creates a new file within the Roaming folder, and indicates that any existing file with the same name should be replaced. Once the file has been created, the contents are written to the new serverResponse.txt file from the file originally returned with the server response.

var roamingFolder = Windows.Storage.ApplicationData.current.roamingFolder;
var filename = "serverResponse.txt";

function cacheResponse(strResponse) {
    roamingFolder.createFileAsync(filename, Windows.Storage.CreationCollisionOption.replaceExisting)
        .done(function (file) {
            return Windows.Storage.FileIO.writeTextAsync(file, strResponse);
        });
}

To access the cached serverResponse.txt file at a later time, our getCachedResponse example retrieves the file by name, defined by filename, and displays the text it contains.


function getCachedResponse() {
    roamingFolder.getFileAsync(filename)
        .then(function (file) {
            return Windows.Storage.FileIO.readTextAsync(file);
        }).done(function (response) {
            print(response);
        }, function () {
            // getFileAsync or readTextAsync failed. 
            // No cached response
        });
}

Caching content included with the server response as app data will allow for fast access and display following an app termination and relaunch. For more details on writing settings to the app data store and how to respond to roaming events, read Managing app data or download the Application data sample.

Application data sample

Quickstart: Local app data

Quickstart: Roaming app data

Quickstart: Temporary app data

Guidelines for Roaming app data