当前位置:网站首页>WebApi性能优化
WebApi性能优化
2022-06-25 09:37:00 【@@Mr.Fu】
文章目录
一、WebApi调优
什么是WebApi调优
当浏览器端请求WebApi服务端读写数据需要耗费时间,将耗费的时间缩短,就称之为WebApi调优。
如图:

- 目的
提升WebApi的性能。
二、WebApi 应用场景
场景
前后端分离的项目。
三、WebApi 性能瓶颈定位
定位性能瓶颈
请看NetCore性能排查文章:https://blog.csdn.net/Fu_Shi_rong/article/details/123733415?spm=1001.2014.3001.5501
四、WebApi性能优化手段一-本地缓存
单体项目
如图:

实现
设计思路
查询使用时先看本地缓存有无数据,有数据直接返回,无数据时到数据库查询,将查询到的数据添加到本地缓存中并将数据返回给浏览器端
优点与缺点
缺点
Memory是服务器内存中的缓存,如果并发量大并查询的数据又不一致,会造成内存非常的大 ,同时会造成GC不断地回收这个内存,由于Memory 内部使用的静态变量造成内存无法回收,GC每回收一次,就会消耗一次CPU资源,如果GC回收的频率比较大,那消耗的CPU资源也会变大,例如:现在的CPU的使用率为100%,GC消耗了70%CPU资源,那我们的吞吐量只有30%,那处理请求的性能大幅度的下降。
- 解决方案
- 用时间换空间的方法来解决,给缓存设置时间。
- 设置缓存大小
- 设置单个缓存大小,并设置自动失效时间
- 解决方案
优点
数据读写速度时间缩短,性能得到提升。
实现步骤
安装缓存Nuget包
Microsoft.Extensions.Caching.MemoryStartup注册
ConfigureServices方法中注册缓存服务 Service.AddMemoryCache(options=>{ options.SizeLimit = 1024*1024*100; //设置缓存大小 });在使用的场景进行注入
在构造方法中注入 private readonly IMemoryCache memoryCache; 构造函数 (IMemoryCache _memoryCache) { memoryCache =_memoryCache; } //测试对象 Person per = new Person(); //查询缓存方法 //memoryCache.Get<Person>(key); //为了放防止多线程并发 bool flag = memoryCache.TryGetValue<缓存对象>(key,out per); //true:有数据 false:无数据 //限制缓存大小 var cacheEntryOptions = new MemoryCacheEntryOptions(). //设置单个缓存大下 SetSize(1024). //设置过期时间 自动失效 SetSlidingExpiration(TimeSpan.FromSeconds(3)); //添加缓存 memoryCache.Set<Person>(key,value,cacheEntryOptions);
解决第一次请求耗时解决方案
将项目启动的时候,将数据库中的数据添加到缓存中
代码实现
//类继承 IHostService //并在Startup类中注册
五、WebApi 性能优化手段二-分布式缓存
- 方案
- 请看这篇文章。https://blog.csdn.net/Fu_Shi_rong/article/details/123930343?spm=1001.2014.3001.5501
六、WebApi 性能优化手段三-Http缓存(响应缓存)
方案
协商缓存
安装Nuget包
Marvin.Cache.Headers在Startup类中只注册
ConfigureServices方法中注册 Service.AddHttpCacheHeaders((options)=>{options.MaxAge = ....;//设置过期时间 默认60s options.CacheLocation = ....;//public 公共的 private 私有的只能当前客户端使用 options.NoStore= ...;// 设置响应头信息 不走本地缓存 options.NoTransform= ....;//设置请求头信息 }, (options1)=>{}); Configure方法中启动并存储校验信息 app.UseHttpCacheHeaders();控制器方法中添加代码
HttpContext.Response.Headers.Add("cache-control","max-age=60,public"); //是否开启缓存储数据 设置缓存时间 HttpContext.Response.Headers.Add("etag",Value);//校验 HttpContext.Response.Headers.Add("last-modified","Mon,24 Dec 2022 09:49:49 GMT");原理
当客户端第一次请求服务器,服务器响应后,服务器会往响应头里写入两个参数 【1、是否开启缓存存储数据2、校验】并存储到客户端,客户端会将数据存储到缓存仓库中;当客户端第二次请求到服务器,会将etag传到服务器进行校验,如果两个etag是相等的,服务端会返给客户端304,客户端会从缓存仓库中获取数据。
场景
不可变的数据使用。
缺陷
会浪费大量客户端和服务器进行交互。
强制缓存 [不常用]
步骤
安装Nuget包
Marvin.Cache.Headers在Startup类ConfigureServices方法中注册
ConfigureServices方法中注册 Service.AddHttpCacheHeaders((options)=>{ options.MustRevalidate = true; //全局的方式 不建议使用 }); Configure方法中启动并存储校验信息 app.UseHttpCacheHeaders();控制器方法中添加代码
HttpContext.Response.Headers.Add("cache-control","max-age=60,public,must-revalidate");针对某个控制器使用
//在控制器方法上添加特性 [HttpCacheExpiration(CacheLocation = CacheLocation.Public,MaxAge=60)] 设置NoStore = true 不走缓存 [HttpCacheValidation(MustRevalidate = true)]
场景
- 字典数据
- 静态资源 图片等
- js或css文件
七、WebApi 性能优化手段四-数据压缩(响应)
步骤
在Startup类ConfigureServices方法中注册
//响应数据压缩 services.AddResponseCompression();在Startup类Configure方法中开启服务
//必须写在中间件的开头 app.UseResponseCompression();
数据压缩的目的
数据传输的时候,减少传输的带宽,提升性能。
场景
只要涉及到数据传输都可以用数据压缩。
边栏推荐
- CYCA少儿形体礼仪 乐清市培训成果考核圆满落幕
- Flutter replaces the default icon of Gaud positioning
- Fluent creates, reads and writes JSON files
- Jetpack compose layout (IV) - constraintlayout
- [buuctf.reverse] 117-120
- [shared farm] smart agriculture applet, customized development and secondary development of Kaiyuan source code, which is more appropriate?
- MySQL source code reading (II) login connection debugging
- C语言刷题随记 —— 猴子吃桃
- Use evo
- Remove the mosaic, there's a way, attached with the running tutorial
猜你喜欢

How to download the school logo, school name and corporate logo on a transparent background without matting

Methodchannel of flutter

The gradle configuration supports the upgrade of 64 bit architecture of Xiaomi, oppo, vivo and other app stores

2021mathorcupc topic optimal design of heat dissipation for submarine data center

8. Intelligent transportation project (1)
![[competition -kab micro entrepreneurship competition] KAB National College Students' micro entrepreneurship action participation experience sharing (including the idea of writing the application form)](/img/f3/a8414635ad39ad405864f5fcf66d6d.jpg)
[competition -kab micro entrepreneurship competition] KAB National College Students' micro entrepreneurship action participation experience sharing (including the idea of writing the application form)

字符串 最长公共前缀
![[project part - structure and content writing of technical scheme] software system type mass entrepreneurship and innovation project plan and Xinmiao guochuang (Dachuang) application](/img/4a/1e83ea9e8e79c0ae7244a159943480.jpg)
[project part - structure and content writing of technical scheme] software system type mass entrepreneurship and innovation project plan and Xinmiao guochuang (Dachuang) application

Simple waterfall effect
![[wechat applet full stack development course] course directory (mpvue+koa2+mysql)](/img/1c/ab39cf0a69d90a85665a099f5dbd26.jpg)
[wechat applet full stack development course] course directory (mpvue+koa2+mysql)
随机推荐
Consul的基本使用与集群搭建
使用EVO
瑞吉外卖项目(二)
Jetpack compose layout (II) - material components and layout
Why should the terminal retail industry choose the member management system
Applet cloud development joint table data query and application in cloud function
Flutter dialog: cupertinoalertdialog
字符串 实现 strStr()
MySQL创建给出语句
Fluent creates, reads and writes JSON files
With the QQ group file storage function of super nice, you immediately have n cloud disks that are easy to download and never expire
[zufe expense reimbursement] zhecai invoice reimbursement specification (taking Xinmiao reimbursement as an example), which can be passed in one trip at most
[smart agriculture program] smart agriculture small program project is currently popular.
[buuctf.reverse] 121-125
广发证券靠谱吗?是否合法?开股票账户安全吗?
manhattan_slam环境配置
The problem of automatic page refresh after the flyer WebView pops up the soft keyboard
链表 删除链表中的节点
[competition -kab micro entrepreneurship competition] KAB National College Students' micro entrepreneurship action participation experience sharing (including the idea of writing the application form)
What are the PMP scores?