当前位置:网站首页>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();
数据压缩的目的
数据传输的时候,减少传输的带宽,提升性能。
场景
只要涉及到数据传输都可以用数据压缩。
边栏推荐
- puzzle(019.2)六边锁
- 22 mathematical modeling contest 22 contest C
- Flutter Gaode map privacy compliance error
- Jetpack compose layout (III) - custom layout
- How to download the school logo, school name and corporate logo on a transparent background without matting
- Data-driven anomaly detection and early warning of 21 May Day C
- 【mysql学习笔记21】存储引擎
- Match a mobile number from a large number of mobile numbers
- 字符串 实现 strStr()
- ‘Flutter/Flutter. h‘ file not found
猜你喜欢

Flutter dialog: cupertinoalertdialog

Rxjs TakeUntil 操作符的学习笔记

Wechat official account can reply messages normally, but it still prompts that the service provided by the official account has failed. Please try again later

Remove the mosaic, there's a way, attached with the running tutorial

Analysis on the thinking of 2022 meisai C question
![[MySQL learning notes 22] index](/img/08/db7b765f7ddaa5706e3f7d00690d23.png)
[MySQL learning notes 22] index

Minio基本使用与原理
![[matlab] image binarization (imbinarize function)](/img/3e/066f460d9f436bbc43ea35e46093e2.jpg)
[matlab] image binarization (imbinarize function)

Data-driven anomaly detection and early warning of 21 May Day C

Jetpack compose layout (III) - custom layout
随机推荐
The gradle configuration supports the upgrade of 64 bit architecture of Xiaomi, oppo, vivo and other app stores
[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate
manhattan_ Slam environment configuration
Flutter replaces the default icon of Gaud positioning
Neat Syntax Design of an ETL Language (Part 2)
How to download the school logo, school name and corporate logo on a transparent background without matting
[buuctf.reverse] 117-120
JS tool function, self encapsulating a throttling function
[matlab] image binarization (imbinarize function)
Fluent creates, reads and writes JSON files
MySQL source code reading (II) login connection debugging
Chitubox micromake l3+ slicing software configuration correspondence
2台三菱PLC走BCNetTCP协议,能否实现网口无线通讯?
Can two Mitsubishi PLC adopt bcnettcp protocol to realize wireless communication of network interface?
Neat Syntax Design of an ETL Language (Part 2)
Online notes on Mathematics for postgraduate entrance examination (8): Kego equations, eigenvalues and eigenvectors, similarity matrix, quadratic series courses
How to make a self-made installer and package the program to generate an installer
汇付国际为跨境电商赋能:做合规的跨境支付平台!
‘Flutter/Flutter. h‘ file not found
What functions should smart agriculture applet system design have