当前位置:网站首页>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;边栏推荐
- 阿里云 MSE 支持 Go 语言流量防护
- ANSYS secondary development - MFC interface calls ADPL file
- Sort 4-heap sort and massive TOPK problem
- IM即时通讯开发优化提升连接成功率、速度等
- UNP前六章 回射服务模型 解析
- 有趣的 Kotlin 0x09:Extensions are resolved statically
- 微软100题-天天做-第16题
- 【深度学习】:《PyTorch入门到项目实战》第八天:权重衰退(含源码)
- 重置grafana登录密码为默认密码
- Microsoft: edge browser has built-in disk cache compression technology, which can save space and not reduce system performance
猜你喜欢

Sort 2 bubble sort and quick sort (recursive and non recursive explanation)

【深度学习】:《PyTorch入门到项目实战》第五天:从0到1实现Softmax回归(含源码)

关于 CMS 垃圾回收器,你真的懂了吗?

【深度学习】:《PyTorch入门到项目实战》第八天:权重衰退(含源码)

概率论与数理统计第一章

Learn ABAQUS script programming script in an hour

Interesting kotlin 0x0a:fun with composition

Ansa secondary development - build ansa/meta secondary development environment on pycharm

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

Learn to use MySQL explain to execute the plan, and SQL performance tuning is no longer difficult
随机推荐
Sort 3-select sort and merge sort (recursive implementation + non recursive implementation)
Some suggestions on Oracle SQL tuning
WSL+Valgrind+Clion
Hdu1847 problem solving ideas
我该如何理解工艺
Multiple commands produce '... /xxx.app/assets.car' problem
Efficiency comparison of three methods for obtaining timestamp
Redis series 4: sentinel (sentinel mode) with high availability
Splash (渲染JS服务)介绍安装
Call DLL file without source code
Optimization of network request success rate in IM instant messaging software development
【深度学习】:《PyTorch入门到项目实战》:简洁代码实现线性神经网络(附代码)
【深度学习】:《PyTorch入门到项目实战》第一天:数据操作和自动求导
"Weilai Cup" 2022 Niuke summer multi school training camp 3 h.hacker sam+ segment tree /dp/ divide and conquer (without the largest sub segment and of the inspection interval)
Introduction and implementation of queue (detailed explanation)
微软100题-天天做-第16题
HM secondary development - data names and its use
Each account corresponds to all passwords, and then each password corresponds to all accounts. How to write the brute force cracking code
Implementation of paging
有趣的 Kotlin 0x0A:Fun with composition