This project is read-only.

CacheExtensions

Motivation

Caching HTTP responses is a complex task, given the necessary interaction with no-cache, expires and max-age HTTP headers and storing/retrieving your data from the cache. Enough Software's HttpClient extension methods make this task trivial.

Usage

using Enough.Http.Cache;
[...]
HttpClient client = new HttpClient();
HttpResponseMessage responseMessage = await client.GetCachedAsync(url);

This is one way to use the GetCachedAsync() extension method. There are more overloads implemented to use - compare MSDN for more GetAsync calls. If the response is not stored locally or has expired, the response will be retrieved from the Internet and then stored.

Configure Your Cache Details

You can optionally configure details about the cache. By default the cache is stored in the AplicationData.Current.LocalFolder and has no size limitations.

To specify the storage location and the cache's size, call the ConfigureCacheAsync method:

await client.ConfigureCacheAsync(folder: ApplicationData.Current.TemporaryFolder, cacheSizeInKilobytes: 5 * 1024, saveCacheMetaDataAfterEachRetrieval: false);

The folder parameter defines the storage location. You can use one of these storage locations:

  • ApplicationData.Current.LocalFolder: by default the cache files are saved in the local filesystem
  • ApplicationData.Current.TemporaryFolder: use the temporary folder to store data such as the session state which is not needed after your app exits
  • ApplicationData.Current.RoamingFolder: use the roaming folder to make the data available on all Windows powered handsets of your user  (that probably does not make much sense, though)

The cacheSizeInKilobytes parameter defines the maximum size of the cache in kilobytes. By default the size is unlimited (0).

The saveCacheMetaDataAfterEachRetrieval parameter allows you to specify whether the cache meta data should be saved after each request - doing this will ensure that the meta data is always kept up-to-date, but it also costs some performance. If you set saveCacheMetaDataAfterEachRetrieval to false (by default this is true), you manually need to call await client.SaveCacheMetaDataAsync() at least once before you app exists.

Clear the Cache

Delete the complete cache by calling the ClearCacheAsync extension method:

await client.ClearCacheAsync(folder: ApplicationData.Current.TemporaryFolder)

When not specifying a folder the Application.Data.LocalFolder is assumed.

Save Cache Metadata

If you have configured the cache to not save the metadata at each retrieval you need to save the metadata before your app exists:

await client.SaveCacheMetaDataAsync();

 

Last edited Oct 7, 2013 at 1:38 PM by enoughrob, version 7