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.