当前位置:网站首页>egg(十九):使用egg-redis性能优化,缓存数据提升响应效率
egg(十九):使用egg-redis性能优化,缓存数据提升响应效率
2022-07-28 15:44:00 【Ponnenult】
前言:
在写一些大数据量的接口时候,发现响应速度太慢了,接口,通常一次要8s-12s,这么长的响应时长,给用户带来了极大的不便,这里来利用缓存方法,来对接口进行优化,第一次响应完以后,把数据缓存起来,之后用缓存数据来做一些数据操作。
操作步骤:
1、电脑安装redis工具
window:
github分享:入口

个人网盘分享(推荐)
链接:https://pan.baidu.com/s/1c4mBdwhxJshvC3i_fI2_gg
提取码:
5ntglinux安装
查看系统是否安装redis
yum info redis
如果没有安装,执行以下步骤
安装epel库
yum install epel-release -y
安装redis
yum install redis -y
操作
启动:systemctl start redis
重启:systemctl restart redis
关闭:systemctl stop redis
设置开机启动
systemctl enable redismac安装
brew install redis
brew services start redis
redis-server /usr/local/etc/redis.conf2、电脑运行刚安装的redis工具
(1)把刚下载的安装包放到你的文件夹中,打开是这个文件压缩包,然后解压
![]()
(2)解压后的文件,找到 redis-server.exe 这个是启动服务的快捷键,点击

(3)出现下面的画面,代表服务启动了,端口号是6379

(4)cmd弹框中动态添加,查看缓存内容
myKey是你的key值,abc是具体的value值
设置键值对 set myKey abc
查看键值对 get myKey1)注意,刚刚第3步打开的服务不能关闭,再次打开一个cmd弹框

2)输入 redis-cli.exe -h 127.0.0.1 -p 6379 ,如果出现其他报错,不要着急,

3)更换命令输入 .\redis-cli.exe -h 127.0.0.1 -p 6379 ,看到下面的这个服务,就表示成功了

4)输入我们的设置命令,然后回车
set demo 123456 

5)获取我们刚设置的缓存内容,注意,我们没有设置果abc,所以拿到的是(nil),我们设置了demo,就可以看到具体内容
get abc
3、egg项目中的操作
(1)项目安装插件
npm i egg-redis -S2、config/plugin.js中引入插件
exports.redis = {
enable: true,
package: 'egg-redis'
}3、config.default.js中配置连接地址
config.redis = {
client: {
port: 6379,
host: '127.0.0.1',
password: 'auth',
db: 0
},
}4、service下 新建一个服务文件redis.js

const Service = require('egg').Service;
const time = 60 * 60 * 24 * 365 //默认缓存失效时间 365天
class RedisService extends Service {
// 设置
async set(key, value, seconds) {
// seconds 有效时长
let { redis } = this.app;
value = JSON.stringify(value);
if(!seconds){
// await redis.set(key, value);
await redis.set(key, value, 'EX', time);
}else{
// 设置有效时间
await redis.set(key, value, 'EX', seconds);
}
}
// 获取
async get(key) {
let { redis } = this.app;
let data = await redis.get(key);
if (!data) return;
data = JSON.parse(data);
return data;
}
// 清空redis
async flushall() {
let { redis } = this.app;
redis.flushall();
return;
}
}
module.exports = RedisService;
5、方法中具体使用
(1)获取
let xiaoshuo = await this.app.redis.get('xiaoshuo1');(2)设置
let str = 123
await this.app.redis.set('xiaoshuo1', str);6、具体业务使用
async getXiaoshuo1() {
let xiaoshuo = await this.app.redis.get('xiaoshuo1');
//如果有缓存数据,就直接使用缓存
if(xiaoshuo){
this.ctx.body = {
code:200,
masg:'success',
data: {
list:xiaoshuo
}
}
return
}
//获取mysql数据
let allList = await this.app.mysql.query(initSql);
this.ctx.body = {
code:200,
masg:'success',
data: {
list:allList
}
};
}7、如果你想测试请求接口的时间和使用了缓存以后的时间,测试方法
console.time() // 开始计时
let goodsList = await this.service.redis.get('goodsList');
if (!goodsList) {
console.log('没有redis缓存')
goodsList = await this.app.mysql.select('goods');
this.service.redis.set('goodsList', goodsList);
}
console.timeEnd() // 打印时长
await this.ctx.body = goodsList;边栏推荐
- Thoughts on solving the pop-up of malicious computer advertisements
- curl无输出返回空白或者null问题解决
- Learn ABAQUS script programming script in an hour
- 局域网无法访问apache服务器
- The local area network cannot access the Apache server
- nowcode-学会删除链表中重复元素两题(详解)
- Redis series 4: sentinel (sentinel mode) with high availability
- PHP计算坐标距离
- Geodetic coordinate system to Martian coordinate system
- 排序1-插入排序与希尔排序
猜你喜欢

LeetCode每日一练 —— 160. 相交链表

FX3开发板 及 原理图

排序3-选择排序与归并排序(递归实现+非递归实现)

KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书

USB产品(FX3、CCG3PA)的调试方法

Huada chip hc32f4a0 realizes RS485 communication DMA transceiver

Use js direct OSS to store files in Alibaba cloud and solve the limitation of large file upload server

About the web docking pin printer, lodop uses

Wei Jianjun couldn't catch up with Li Shufu by riding a BMW

IM即时通讯开发优化提升连接成功率、速度等
随机推荐
排序5-计数排序
PHP image upload
Using pyqt to design gui in ABAQUS
微软100题-天天做-第11题
PHP gets the applet code, and the applet jumps with parameters
About mit6.828_ HW9_ Some problems of barriers xv6 homework9
mysql cdc 如果binlog日志文件不全,全量阶段能读到所有数据吗
Each account corresponds to all passwords, and then each password corresponds to all accounts. How to write the brute force cracking code
Sort 1-insert sort and Hill sort
flashfxp 530 User cannot log in. ftp
ANSA二次开发 - Apps和ANSA插件管理
重置grafana登录密码为默认密码
Use js direct OSS to store files in Alibaba cloud and solve the limitation of large file upload server
后台弹出layer提示
HyperMesh自动保存(增强版)插件使用说明
PHP图片合成技术
关于web对接针式打印机问题,Lodop使用
Ansa secondary development - apps and ansa plug-in management
Solve the width overflow of rich text pictures such as uniapp
局域网无法访问apache服务器