[ASP. NET Web API] Cache

Source: Internet
Author: User

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

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.