当前位置:网站首页>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 !
边栏推荐
- What is the secondary domain name of the website? What is the relationship between the secondary domain name and the primary domain name?
- What is cloud server? How to access the ECS Homepage
- Why domain name should be resolved? What is the domain name registration query
- Develop a customized music player from scratch, and your girlfriend will have it?
- Jimureport building block report - what problems does the layout design solve?
- Intensive learning and application of "glory of the king" to complete the application of 7 real worlds other than human players
- Implementation principle of Flink connector mongodb CDC
- Analysis of PHP environment configuration
- Hard core JS: there may be a memory leak in your program
- Precautions for online education and training industry filing
猜你喜欢

Hard core observation 553 AI needs to identify almost everyone in the world with hundreds of billions of photos

Training methods after the reform of children's programming course

Popularization of children's programming education in specific scenarios

Analyzing the superiority of humanoid robot in the post human era

Introduction to the "penetration foundation" cobalt strike Foundation_ Cobalt strike linkage msfconsole

Analysis on the subjective enthusiasm of post-90s makers' Education

Zhang Xiaodan, chief architect of Alibaba cloud hybrid cloud: evolution and development of government enterprise hybrid cloud technology architecture

解析90后创客教育的主观积极性

Intensive learning and application of "glory of the king" to complete the application of 7 real worlds other than human players

Loss and optimization of linear regression, machine learning to predict house prices
随机推荐
Activity recommendation | cloud native community meetup phase VII Shenzhen station begins to sign up!
How the query address of cloud native monitoring data exposes the public network
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
How should a new data center be built?
Analysis on the subjective enthusiasm of post-90s makers' Education
SUSE system cannot install cosfs solution
Shuttle global levitation button
Bi-sql order by
014_ TimePicker time selector
IP and traffic reconciliation tool networktrafficview
How to expand virtual machine capacity on vSphere client
Oracle database prompts no operation permission
让孩子们学习Steam 教育的应用精髓
4G industrial VPN router
Pg-pool-ii read / write separation experience
[Yunyue plan] Tencent's cloud industry opening is based on the digital marketing of games such as king / eating chicken / fighting landlords and private domain marketing
Webmeng: create a website you are proud of
Introduction to the "penetration foundation" cobalt strike Foundation_ Cobalt strike linkage msfconsole
What is stored in the domain name server? How does the domain name server provide services?
API service orchestration platform, full web visual orchestration