当前位置:网站首页>Simple use of cache functions
Simple use of cache functions
2022-06-24 05:11:00 【Programming samadhi】
Preface
Caching is an important means of program optimization , Generally, space for time is used to improve program performance , The common caching methods are browser caching 、HTTP Cache, etc .
scene
Suppose there is such a simple scenario : The background returns a group of data to the front end for display , Considering the page performance problem , The front end needs pagination to display .
Let's do some code implementation based on the requirements of this scenario .
Achieve one
Very conventional implementation :
// Suppose the data returned in the background is an array , There is data in
function getPagerData(pageNumber, pageSize) {
return data.slice(pageSize * (pageNumber - 1), pageSize * pageNumber);
}Every time you click the page number , Call the getPagerData Method to calculate the data to be displayed .
If data The length of the is not large , Then every calculation should be fast , once data The length of , Then the speed of displaying data in each calculation is not so optimistic .
For performance and demonstration experience , The background pushes the data , As a result, the front-end display is slow due to the calculation of display data , That's a bit of a mystery , Background development may even suck at the front end .
Achieve two
Now that the paging function is done , That means users can click on the page number of any page . If the “ Achieve one ” Plan in , Even the data that has been displayed before , If the user wants to show again , You still have to recalculate , This requires a lot of repetitive work , It really affects your performance .
Now let's do such an optimization :
function dataController() {
let catchData = new Map();
return function (pageNumber, pageSize) {
if (!catchData.has(pageNumber)) {
catchData.set(pageNumber, data.slice(pageSize * (pageNumber - 1), pageSize * pageNumber));
}
return catchData.get(pageNumber);
};
}
let getPagerData = dataController();Then every time the user clicks the page number , We just need to call as follows :
const data = getPagerData(pageNumber, pageSize);
However , As a new era programmer with pursuit and ambition , We'll find a problem : above dataController Only for this specific scenario , No reusability ! What's the line , Must change , So we have the following method :
function dataController(fn) {
let catchData = new Map();
return function (...args) {
if (!catchData.has(args[0])) {
catchData.set(args[0], fn(...args));
}
return catchData.get(args[0]);
}
}
function getPagerData(pageNumber, pageSize) {
return data.slice(pageSize * (pageNumber - 1), pageSize * pageNumber);
}
let getData = dataController(getPagerData);Then every time you need data , Can be called like this :
let data = getData(pageNumer, pageSize);
The specific method of intercepting data is passed in as a parameter , Then if there are different interception logic , You just need to pass... As a function dataController You can get the data , Greatly improve the code reuse rate .
summary
The above is a simple use case for using the cache function !
~
~ The end of this paper , Thank you for reading !
~
Learn interesting knowledge , Make interesting friends , Create interesting souls !
Hello everyone , I am a 〖 The samadhi of programming 〗 The author of Hermit King , My official account is 『 The samadhi of programming 』, Welcome to your attention , I hope you can give me more advice !
You come to , With expectations , I have ink to greet ! You go back , No matter gain or loss , Only with lingering charm !
Knowledge and skills are equally important , Both internal and external skills , We should grasp both theory and practice 、 Hard on both hands !
边栏推荐
- Black horse programmer machine learning handout: preliminary use of linear regression API
- Have you got the programmer's drawing tools and skills?
- What is the use of domain name cloud resolution? What are the factors for domain name errors
- Bi-sql order by
- Leetcode (question 2) - adding two numbers
- Verifying data models in golang
- Here's all you want to know about takin data (1) startup command
- Use of golang testing framework goshub
- How to control CDN traffic gracefully in cloud development?
- Automatically convert local pictures to network pictures when writing articles
猜你喜欢

『渗透基础』Cobalt Strike基础使用入门_Cobalt Strike联动msfconsole

014_ TimePicker time selector

CTF learning notes 18:iwesec file upload vulnerability-03-content-type filtering bypass

"Emergency response practice" logparser log analysis practice

解析后人类时代类人机器人的优越性

Popularization of children's programming education in specific scenarios

Leetcode (question 2) - adding two numbers

Analyzing the superiority of humanoid robot in the post human era

少儿编程教育在特定场景中的普及作用

Hard core observation 553 AI needs to identify almost everyone in the world with hundreds of billions of photos
随机推荐
What is a network domain name? What is the role of a domain name for an enterprise
How Clickhouse queries imported data within a specified time period
What is an ECS? ECS、BCC、CVM...
Spirit breath development log (15)
Ext4 file system jam caused by MEM CGroup OOM
Zhang Xiaodan, chief architect of Alibaba cloud hybrid cloud: evolution and development of government enterprise hybrid cloud technology architecture
What is an evpn switch?
How novices choose ECs and how to judge the quality of ECS
Spirit breath development log (9)
Why domain name should be resolved? What is the domain name registration query
少儿编程课程改革后的培养方式
Bi-sql basic cognition
IP and traffic reconciliation tool networktrafficview
Shuttle global levitation button
2021-08-27: the normal odometer will display natural numbers in turn to indicate mileage, Kyrgyzstan
Deep learning common optimizer summary
The function of nearby people in the applet is realized, and the cloud development database is used to realize nearby people and friends within a distance of the neighborhood
5g and industrial Internet
The trunk warehouse can also be tob, and Tencent cloud microenterprises do not leave quality behind
Bi-sql - Select