当前位置:网站首页>Egg (19): use egg redis performance optimization to cache data and improve response efficiency
Egg (19): use egg redis performance optimization to cache data and improve response efficiency
2022-07-28 16:55:00 【Ponnenult】
Preface :
When writing interfaces with a large amount of data , I found that the response speed was too slow , Interface , Usually once 8s-12s, Such a long response time , It brings great inconvenience to users , Here is the cache method , To optimize the interface , After the first response , Cache the data , Then use the cached data to do some data operations .
Operation steps :
1、 Computer installation redis Tools
window:
github Share : entrance

Personal online disk sharing ( recommend )
link :https://pan.baidu.com/s/1c4mBdwhxJshvC3i_fI2_gg
Extraction code :
5ntglinux install
Check if the system is installed redis
yum info redis
If not installed , Follow these steps
install epel library
yum install epel-release -y
install redis
yum install redis -y
operation
start-up :systemctl start redis
restart :systemctl restart redis
close :systemctl stop redis
Set boot up
systemctl enable redismac install
brew install redis
brew services start redis
redis-server /usr/local/etc/redis.conf2、 The computer runs the newly installed redis Tools
(1) Put the newly downloaded installation package into your folder , Open this file zip , Then decompress
![]()
(2) The extracted file , find redis-server.exe This is the shortcut key to start the service , Click on

(3) The following screen appears , On behalf of the service started , The port number is 6379

(4)cmd Dynamically add , View cache contents
myKey Is your key value ,abc It is concrete. value value
Set key value pairs set myKey abc
Check the key value pairs get myKey1) Be careful , Just the second time 3 The service opened in step cannot be closed , Open one again cmd bounced

2) Input redis-cli.exe -h 127.0.0.1 -p 6379 , If there are other errors , Don't worry ,

3) Change command input .\redis-cli.exe -h 127.0.0.1 -p 6379 , See the following service , It means success

4) Enter our setup command , And then go back
set demo 123456 

5) Get the cache content we just set , Be careful , We didn't set fruit abc, So what I got was (nil), We set it up demo, You can see the specific content
get abc
3、egg Operations in the project
(1) Project installation plug-ins
npm i egg-redis -S2、config/plugin.js Introduce plug-in in
exports.redis = {
enable: true,
package: 'egg-redis'
}3、config.default.js Configure the connection address in
config.redis = {
client: {
port: 6379,
host: '127.0.0.1',
password: 'auth',
db: 0
},
}4、service Next Create a new service file redis.js

const Service = require('egg').Service;
const time = 60 * 60 * 24 * 365 // Default cache expiration time 365 God
class RedisService extends Service {
// Set up
async set(key, value, seconds) {
// seconds Effective time
let { redis } = this.app;
value = JSON.stringify(value);
if(!seconds){
// await redis.set(key, value);
await redis.set(key, value, 'EX', time);
}else{
// Set the effective time
await redis.set(key, value, 'EX', seconds);
}
}
// obtain
async get(key) {
let { redis } = this.app;
let data = await redis.get(key);
if (!data) return;
data = JSON.parse(data);
return data;
}
// Empty redis
async flushall() {
let { redis } = this.app;
redis.flushall();
return;
}
}
module.exports = RedisService;
5、 Method
(1) obtain
let xiaoshuo = await this.app.redis.get('xiaoshuo1');(2) Set up
let str = 123
await this.app.redis.set('xiaoshuo1', str);6、 Specific business use
async getXiaoshuo1() {
let xiaoshuo = await this.app.redis.get('xiaoshuo1');
// If there's cached data , Just use the cache directly
if(xiaoshuo){
this.ctx.body = {
code:200,
masg:'success',
data: {
list:xiaoshuo
}
}
return
}
// obtain mysql data
let allList = await this.app.mysql.query(initSql);
this.ctx.body = {
code:200,
masg:'success',
data: {
list:allList
}
};
}7、 If you want to test the time of the request interface and the time after using the cache , The test method
console.time() // Start timing
let goodsList = await this.service.redis.get('goodsList');
if (!goodsList) {
console.log(' No, redis cache ')
goodsList = await this.app.mysql.select('goods');
this.service.redis.set('goodsList', goodsList);
}
console.timeEnd() // Print duration
await this.ctx.body = goodsList;边栏推荐
- Ansa secondary development - Introduction to interface development tools
- Sort 2 bubble sort and quick sort (recursive and non recursive explanation)
- Some suggestions on Oracle SQL tuning
- Fx3 development board and schematic diagram
- MySQL CDC if the binlog log file is incomplete, can you read all the data in the full volume stage
- Some opinions on bug handling
- Call DLL file without source code
- Some suggestions on optimizing HyperMesh script performance
- 小程序:scroll-view默认滑倒最下面
- Analysis of echo service model in the first six chapters of unp
猜你喜欢

Leetcode learn to insert and sort unordered linked lists (detailed explanation)

TCP handshake, waving, time wait connection reset and other records

Im im development optimization improves connection success rate, speed, etc

每一个账号对应所有密码,再每一个密码对应所有账号暴力破解代码怎么写?...

MySQL 5.7 and sqlyogv12 installation and use cracking and common commands

阿里大哥教你如何正确认识关于标准IO缓冲区的问题

Interesting kotlin 0x07:composition

有趣的 Kotlin 0x08:What am I

【深度学习】:《PyTorch入门到项目实战》第二天:从零实现线性回归(含详细代码)

Ruoyi's solution to error reporting after integrating flyway
随机推荐
【JS】1394- ES2022 的 8 个实用的新功能
How to set ticdc synchronization data to only synchronize the specified library?
局域网无法访问apache服务器
LeetCode-学会复杂带随机指针链表的题(详解)
parseJson
Li Hongyi, machine learning 5. Tips for neural network design
First day of QT study
Is smart park the trend of future development?
Microsoft: edge browser has built-in disk cache compression technology, which can save space and not reduce system performance
Introduction and implementation of stack (detailed explanation)
Ansa secondary development - Introduction to interface development tools
Ruoyi's solution to error reporting after integrating flyway
Interesting kotlin 0x07:composition
Cluster construction and use of redis5
Signal and slot mechanism of QT learning
The local area network cannot access the Apache server
给定正整数N、M,均介于1~10 ^ 9之间,N <= M,找出两者之间(含N、M)的位数为偶数的数有多少个
MySQL CDC if the binlog log file is incomplete, can you read all the data in the full volume stage
有趣的 Kotlin 0x06:List minus list
小程序:scroll-view默认滑倒最下面