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.
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.
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:
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.
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.
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();