5.37Enabling ETag Response Caching
ETag (entity tag) is a mechanism in the HTTP protocol used to identify the version of a resource. The server returns the resource's ETag value in the response headers (for example, "abc123"), and the client can carry that value via the If-None-Match header in subsequent requests. If the resource has not changed, the server returns 304 Not Modified without retransmitting the content; otherwise it returns the new content along with a new ETag.
After enabling ETag caching, the framework handles this process automatically: the first request caches the response and the ETag, and subsequent requests automatically attach If-None-Match; when a 304 status code is received, the cached content is reused directly, reducing data transfer and improving request efficiency. In weak network environments or mobile data-metering scenarios, this mechanism can significantly reduce bandwidth consumption while speeding up response times.
HTTP declarative requests enable ETag response caching through the UseETagAttribute attribute. The corresponding HTTP declarative extractor is implemented as the UseETagDeclarativeExtractor type, which is responsible for parsing the UseETagAttribute attribute and building the ETag response caching configuration needed for the HttpRequestBuilder instance.
// Applied on the interface definition, affecting all methods[UseETag]public interface IHttpService : IHttpDeclarative{ // Applied on a method [UseETag] [Get("https://furion.net/")] Task<string> GetStringAsync(); [UseETag(false)] // Disable this feature [Get("https://furion.net/")] Task<string> GetStringAsync();}UseETagAttribute contains the following constructors and properties:
-
Constructors:
new(): applies to methods or interfaces, enablingETagresponse caching.new(enabled): applies to methods or interfaces, setting whether to enableETagresponse caching.
-
Properties:
Enabled: whether to enable (typebool), with a default value oftrue(enabled).