14.14. Using Cached Results When Working with HTTP for Faster Performance
Problem
You are looking for a way to speed up code that reaches out to the Web via HTTP for content.
Solution
Use the RequestCachePolicy class to determine how your HttpWebRequests react in the presence of a caching entity. RequestCachePolicy has seven levels defined by the RequestCacheLevel enumeration, as shown in Table 14-3.
Table 14-3. RequestCacheLevel enumeration values
Flag value | Purpose |
|---|---|
| Get content only directly from the server (default setting in .NET). |
| Accept the requested item from any cache between the request and the server of the content. |
| Accept the request to be fulfilled from only the local cache; throws a |
| Accept content from intermediate caches or from the server directly, subject to the current cache policy and content age (recommended level for most apps, even though it is not the default setting). |
| Content will not be accepted from caches nor added to any. Equivalent to the HTTP no-cache directive. |
| Get content directly from the server but store response in the cache. |
| Check the content timestamp on the server against the cache and take the most recent one. |
The RequestCachePolicy is set up using the CacheIfAvailable RequestCacheLevel so that the request will always take the "closest" content to enhance retrieval speed. If Default is used, the request is still subject to the underlying cache policy ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access