In ASP. NET Web APIs, cache is implemented through ETag, and apietag

Source: Internet
Author: User

In ASP. NET Web APIs, cache is implemented through ETag, and apietag

 

Generally, the Server is stateless. in ASP. NET Web APIs, we can generate the ETag attribute in the Server response body and play a cache role. The general implementation principle is:

1. An ETag attribute is returned in the response body of the server.
2. The client passes the attribute value of ETag to the server through the If-None-Match attribute.
3. The server returns status code 304.

The ETag attribute is returned in the response body.

Install CacheCow. Server

Configure in WebApiConfig. cs:

Using CacheCow. Server;

Var cacheHandler = new CachingHandler ();
Config. MessageHandlers. Add (cacheHandler );

At this time, the client sends a request:

User-Agent: Fiddler
Host: localhost: 8901

An ETag attribute exists in the returned message.

ETag: W /"..."

Generally, the ETag value is affected when the server or IIS is restarted.

In this case, put the ETag in the Request body:

User-Agent: Fiddler
Host: localhost: 8901
If-None-Match :"..."

The server returns 304 Not Found. This means that the Entity has Not been changed since it was created.

The ETag and LastModified attributes are returned in the response body.

Var cacheHandler = new CachingHandler ();
CacheHandler. AddLastModifiedHeader = false; // The default value is true.
Config. MessageHandlers. Add (cacheHandler );

At this time, the client sends a request:

User-Agent: Fiddler
Host: localhost: 8901

An ETag attribute exists in the returned message.

ETag: W /"..."

In this case, put the ETag in the Request body:

User-Agent: Fiddler
Host: localhost: 8901
If-None-Match :"..."

The server returns 304 Not Found. The Last-Modified attribute, indicating the Last modification time.

The ETag attribute related to SQL Server is returned in the response body.

Install CacheCow. Server. EntityTagStore. SqlServer

Var connString = ConfigurationManager. ConnectionStrings ["DefaultConnection"]. ConnectionString;
Var etagStore = new SqlServerEntityTagStore (connString );
Var cacheHandler = new CacheHandler (etagStore );
Config. MessageHandlers. Add (cacheHandler );

Add a stored procedure to SQL Server.

→ Open the folder where the project is located
→ Packages folder
→ CacheCow. Server. EntityTagStore. SqlServer.0.4.1 folder
→ Script. SQL file under scripts, copy the statements in the file, and create the corresponding table and stored procedure on SQL Server.

At this time, the client sends a request:

User-Agent: Fiddler
Host: localhost: 8901

An ETag attribute exists in the returned message.

ETag :"..."

However, the attribute value of ETag does not start with "W.

In this case, put the ETag in the Request body:

User-Agent: Fiddler
Host: localhost: 8901
If-None-Match :"..."

The server returns 304 Not Found. This means that the Entity has Not been changed since it was created.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.