Summary
In order to improve the performance of the interface, the optimizations we often do include caching, which caches data that is frequently accessed but does not change much. or use the HTTP cache to reduce the number of requests.
Web API Caching
In the API provided, we can also implement the cache to reduce the number of accesses. There is a better component for us to use.
Installing WebApi.OutputCache.V2 open source components through NuGet, project address: Https://github.com/filipw/Strathweb.CacheOutput
Use
[Routeprefix ("Api/user")] Public classUsercontroller:apicontroller {PrivateList<user>_lstusers; PublicUsercontroller () {_lstusers=NewList<user>() { Newuser{id=1, name="wolfy1"}, Newuser{id=2, name="wolfy2"}, Newuser{id=3, name="Wolfy3"}, Newuser{id=4, name="Wolfy4"}, Newuser{id=5, name="wolfy5"}, }; } /// <summary> ///Cache 120s/// </summary> /// <returns></returns>[HttpGet] [Route (" All")] [Cacheoutput (Clienttimespan= -, Servertimespan = -)] Publichttpresponsemessage GetAll () {httpresponsemessage response=Request.createresponse (); Response. StatusCode=Httpstatuscode.ok; Response. Content=NewStringcontent (Jsonconvert.serializeobject (New{_code = $, _data =_lstusers}),
SYSTEM.TEXT.ENCODING.UTF8); returnresponse; } }
Test
First time visit
Second Refresh
In debug mode, the second time does not enter the endpoint, and no request is initiated.
Summarize
When optimizing the query interface, you can use this component for caching optimizations if the data is not changing very frequently. For additional parameters of the component, refer to the documentation provided in its project address.
[ASP. NET Web API] Cache