Requirements: Have one in the .NET FrameworkOutputCacheAttributeThe feature (which is used to mark the operation method of caching its output) stores frequently accessed and infrequently changing data in memory and outputs response content directly, which can improve the concurrency performance of the website and reduce the pressure on the server and database. This feature was only added back in .NET 7.
ASP.NET Output cache middleware in Core:The hyperlink login is visible.
The principle of output caching: each request first determines whether it exists in the cache, if it does not exist, writes the content of the response to the cache and then outputs the response, if the cache exists, the data output response is directly obtained from the cache. via output cacheReducing querying the database, reducing some logical operations, and fetching directly from the cache improves both concurrency and responsiveness。
Since there is no OutputCacheAttribute feature in .NET 6, we can implement a simple output cache ourselves.
In the absence of an output cache, the stress test results are as follows (The project database is connected to the Hong Kong server):
Stress test results: Only 10 concurrency per second allowed (slow in database transfer).
To try to add an output cache, first, create a new OutputCacheAttribute feature, the code is as follows:
Create a new OutputCacheMiddleware middleware to block the request, and the code is as follows:
Add the UseOutputCacheExtensions middleware to Program.cs with the following code:
Finally, add the feature to the controller method with the following code:
Again with pressure testing, it can be achievedMore than 10,000 concurrency per second, as shown in the figure below:
cassowary.exe run -u http://localhost:5222/zh-cn -c 50 -n 100000
(End)
|