Microsoft Docs

RequestCachePolicy Class

Namespace: System.Net.Cache

Assembly: System.dll

Description

The RequestCachePolicy class defines caching policies for outgoing Web requests. It allows developers to specify how a request should be retrieved from the cache, how it should be stored, and under what circumstances a cached response may be used.

Constructors

Default
CacheLevel
public RequestCachePolicy();

Initializes a new instance with the default cache level (RequestCacheLevel.Default).

Properties

NameTypeDescription
LevelRequestCacheLevelGets or sets the caching level for the request.
AgeTimeSpanGets the maximum age of a cached response that is acceptable.

Methods

SignatureDescription
void SetCacheability(HttpCacheAgeControl cacheability) Specifies the cacheability of the request.
void SetMaxAge(TimeSpan maxAge) Defines the maximum age of cached responses that may be used.
void SetNoCache() Disables caching for the request.
override string ToString() Returns a string representation of the current policy.

Remarks

The RequestCachePolicy class works in conjunction with the HttpWebRequest or WebRequest classes. By setting a policy on a request, developers can control how responses are cached locally and when the cache should be bypassed.


// Example: Setting a custom cache policy
var request = (HttpWebRequest)WebRequest.Create("https://example.com/data");
request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.CacheIfAvailable);
request.GetResponse();