当前位置:网站首页>Webapi performance optimization
Webapi performance optimization
2022-06-25 10:05:00 【@@Mr.Fu】
List of articles
- One 、WebApi tuning
- Two 、WebApi Application scenarios
- 3、 ... and 、WebApi Performance bottleneck positioning
- Four 、WebApi Performance optimization means 1 - Local cache
- 5、 ... and 、WebApi Performance optimization means 2 - Distributed cache
- 6、 ... and 、WebApi Performance optimization means 3 -Http cache ( Response cache )
- 7、 ... and 、WebApi Performance optimization means 4 - data compression ( Respond to )
One 、WebApi tuning
What is? WebApi tuning
When the browser side requests WebApi It takes time for the server to read and write data , Shorten the time it takes , It's called WebApi tuning .
Pictured :

- Purpose
promote WebApi Performance of .
Two 、WebApi Application scenarios
scene
Projects that are separated at the front and rear end .
3、 ... and 、WebApi Performance bottleneck positioning
Locate performance bottlenecks
Please have a look at NetCore Performance troubleshooting article :https://blog.csdn.net/Fu_Shi_rong/article/details/123733415?spm=1001.2014.3001.5501
Four 、WebApi Performance optimization means 1 - Local cache
Single project
Pictured :

Realization
Design thinking
When querying, first check whether there is data in the local cache , There's data coming back directly , Query the database when there is no data , Add the queried data to the local cache and return the data to the browser
Advantages and disadvantages
shortcoming
Memory Is the cache in the server memory , If the concurrency is large and the query data is inconsistent , It will cause a very large memory , At the same time, it will cause GC Constantly reclaim this memory , because Memory Memory cannot be recycled due to static variables used internally ,GC Every time you recycle , It will consume once CPU resources , If GC The frequency of recycling is relatively high , That consumes CPU Resources will also grow , for example : current CPU The usage rate of is 100%,GC consumed 70%CPU resources , Then our throughput is only 30%, The performance of processing requests is greatly reduced .
- Solution
- Use time for space to solve , Set the cache time .
- Set cache size
- Set the size of a single cache , And set the automatic expiration time
- Solution
advantage
Data reading and writing speed and time are reduced , Improved performance .
Implementation steps
Install cache Nuget package
Microsoft.Extensions.Caching.MemoryStartup register
ConfigureServices Method Service.AddMemoryCache(options=>{ options.SizeLimit = 1024*1024*100; // Set cache size });Inject in the scene of use
Inject... Into the construction method private readonly IMemoryCache memoryCache; Constructors (IMemoryCache _memoryCache) { memoryCache =_memoryCache; } // Test object Person per = new Person(); // Query cache method //memoryCache.Get<Person>(key); // To prevent multithreading concurrency bool flag = memoryCache.TryGetValue< Cache object >(key,out per); //true: There's data false: No data // Limit cache size var cacheEntryOptions = new MemoryCacheEntryOptions(). // Set the size of a single cache SetSize(1024). // Set expiration time Automatic failure SetSlidingExpiration(TimeSpan.FromSeconds(3)); // Add cache memoryCache.Set<Person>(key,value,cacheEntryOptions);
Solving the first request takes time
When the project starts , Add data from the database to the cache
Code implementation
// Class inheritance IHostService // And in Startup Class registration
5、 ... and 、WebApi Performance optimization means 2 - Distributed cache
- programme
- Please read this article .https://blog.csdn.net/Fu_Shi_rong/article/details/123930343?spm=1001.2014.3001.5501
6、 ... and 、WebApi Performance optimization means 3 -Http cache ( Response cache )
programme
Negotiate the cache
install Nuget package
Marvin.Cache.Headersstay Startup Class only register
ConfigureServices Method registration Service.AddHttpCacheHeaders((options)=>{options.MaxAge = ....;// Set expiration time Default 60s options.CacheLocation = ....;//public Public private Private can only be used by the current client options.NoStore= ...;// Set response header information No local cache options.NoTransform= ....;// Set the request header information }, (options1)=>{}); Configure Method to start and store verification information app.UseHttpCacheHeaders();Add code to the controller method
HttpContext.Response.Headers.Add("cache-control","max-age=60,public"); // Whether to enable buffered data storage Set cache time HttpContext.Response.Headers.Add("etag",Value);// check HttpContext.Response.Headers.Add("last-modified","Mon,24 Dec 2022 09:49:49 GMT");principle
When the client first requests the server , After the server responds , The server will write two parameters to the response header 【1、 Whether to enable cache storage data 2、 check 】 And store it to the client , The client will store the data in the cache warehouse ; When the client requests the server for the second time , Will etag Send it to the server for verification , If two etag They are equal. , The server will return to the client 304, The client will get data from the cache Repository .
scene
Immutable data use .
defects
It will waste a lot of client and server interaction .
Mandatory cache [ Not commonly used ]
step
install Nuget package
Marvin.Cache.Headersstay Startup class ConfigureServices Method registration
ConfigureServices Method registration Service.AddHttpCacheHeaders((options)=>{ options.MustRevalidate = true; // In a global way Not recommended }); Configure Method to start and store verification information app.UseHttpCacheHeaders();Add code to the controller method
HttpContext.Response.Headers.Add("cache-control","max-age=60,public,must-revalidate");Use... For a controller
// Add a property to the controller method [HttpCacheExpiration(CacheLocation = CacheLocation.Public,MaxAge=60)] Set up NoStore = true Don't walk the cache [HttpCacheValidation(MustRevalidate = true)]
scene
- Dictionary data
- Static resources Pictures, etc
- js or css file
7、 ... and 、WebApi Performance optimization means 4 - data compression ( Respond to )
step
stay Startup class ConfigureServices Method registration
// Response data compression services.AddResponseCompression();stay Startup class Configure Method
// It must be written at the beginning of the middleware app.UseResponseCompression();
The purpose of data compression
When data is transmitted , Reduce transmission bandwidth , Lifting performance .
scene
Data compression can be used whenever data transmission is involved .
边栏推荐
- 22 mathematical modeling contest 22 contest C
- How to "transform" small and micro businesses (II)?
- BUG-00x bug description + resolve ways
- Redis(一)原理与基本使用
- Oracle function trigger
- Modbus协议与SerialPort端口读写
- Get started quickly with jetpack compose Technology
- Bug- solve the display length limitation of log distinguished character encoding (edittext+lengthfilter)
- What should be paid attention to in PMP examination?
- Mysql 源码阅读(二)登录连接调试
猜你喜欢

瑞萨RA系列-开发环境搭建

Can two Mitsubishi PLC adopt bcnettcp protocol to realize wireless communication of network interface?

链表 删除链表中的节点

Rxjs TakeUntil 操作符的学习笔记
![[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate](/img/75/a06e20b4394579cbd9f6d3a075907a.jpg)
[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate

Redis(二)分布式锁与Redis集群搭建

Etcd tutorial - Chapter 4 etcd cluster security configuration

2台三菱PLC走BCNetTCP协议,能否实现网口无线通讯?

NetCore性能排查

Consul的基本使用与集群搭建
随机推荐
[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate
匯付國際為跨境電商賦能:做合規的跨境支付平臺!
The way that flutter makes the keyboard disappear (forwarding from the dependent window)
WebApi性能优化
Armbian version name comparison
oracle 函数 触发器
JS tool function, self encapsulating a throttling function
[buuctf.reverse] 117-120
How much does a small program cost? How much does a small program cost? It's clear at a glance
Kotlin advanced - class
[matlab] image binarization (imbinarize function)
Guiding principle - read source code
An auxiliary MVP architecture project quick development library -mvpfastdagger
【mysql学习笔记21】存储引擎
Exception: gradle task assemblydebug failed with exit code 1
How much money have I made by sticking to fixed investment for 3 years?
Ruiji takeout project (II)
x86电脑上下载debian的arm64的包
ShardingSphere-Proxy 5.0 分库分表(一)
Fluent: target support file /pods runner / pods runner frameworks Sh: permission denied - stack overflow