[Asp.net web api]緩衝

來源:互聯網
上載者:User

標籤:summary   ima   text   sum   摘要   參考   sage   server   png   

摘要

為了提高介面的效能,我們常做的最佳化就包括緩衝,對經常訪問但變化不大的資料進行緩衝。或者使用http的緩衝,減少請求的次數。

web api緩衝

在提供的api,我們也可以實現緩衝,來減少訪問的次數。有個比較好的組件,供我們使用。

通過Nuget安裝WebApi.OutputCache.V2開源組件,項目地址:https://github.com/filipw/Strathweb.CacheOutput

使用

    [RoutePrefix("api/user")]    public class UserController : ApiController    {        private List<User> _lstUsers;        public UserController()        {            _lstUsers = new List<User>()            {                new User{ Id=1, Name="wolfy1"},                new User{ Id=2, Name="wolfy2"},                new User{ Id=3, Name="wolfy3"},                new User{ Id=4, Name="wolfy4"},                new User{ Id=5, Name="wolfy5"},            };        }        /// <summary>        /// 緩衝120s        /// </summary>        /// <returns></returns>        [HttpGet]        [Route("all")]        [CacheOutput(ClientTimeSpan = 120, ServerTimeSpan = 120)]        public HttpResponseMessage GetAll()        {            HttpResponseMessage response = Request.CreateResponse();            response.StatusCode = HttpStatusCode.OK;            response.Content = new StringContent(JsonConvert.SerializeObject(new { _code = 200, _data = _lstUsers }),
System.Text.Encoding.UTF8); return response; } }

測試

第一次訪問

第二次重新整理

在Debug模式下,第二次沒有進入端點,沒有發起請求。

總結

在最佳化查詢介面的時候,如果資料不是非常頻繁的變化,可以使用該組件進行緩衝方面的最佳化。關於該組件其他參數,可以參考它項目地址中提供的文檔。

 

[Asp.net web api]緩衝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.