当前位置:网站首页>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;边栏推荐
- Qt学习第一天
- Sdl2 concise tutorial (4): using SDL_ Image library importing pictures
- 关于web对接针式打印机问题,Lodop使用
- 排序2-冒泡排序与快速排序(递归加非递归讲解)
- Design direction of daily development plan
- Some suggestions on optimizing HyperMesh script performance
- Leetcode topic
- Qt学习之Qt Designer(设计师)
- 每一个账号对应所有密码,再每一个密码对应所有账号暴力破解代码怎么写?...
- Redis source code optimization -- binding core
猜你喜欢

FX3开发板 及 原理图

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

Each account corresponds to all passwords, and then each password corresponds to all accounts. How to write the brute force cracking code

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

HM secondary development - data names and its use

一小时内学会Abaqus脚本编程秘籍

Optimization of network request success rate in IM instant messaging software development

The little red book of accelerating investment, "rush to medical treatment"?

Sort 3-select sort and merge sort (recursive implementation + non recursive implementation)

Leetcode daily practice - 160. Cross linked list
随机推荐
Leetcode learn to insert and sort unordered linked lists (detailed explanation)
Nowcode- learn to delete duplicate elements in the linked list (detailed explanation)
Headline article_ signature
LeetCode每日一练 —— 160. 相交链表
LwIP development | realize TCP server through socket
排序2-冒泡排序与快速排序(递归加非递归讲解)
Introduction and implementation of queue (detailed explanation)
PHP获取小程序码,小程序带参数跳转
Ansa secondary development - Introduction to interface development tools
ANSA二次开发 - 界面开发工具介绍
PHP计算坐标距离
LwIP development | socket | TCP | client
“蔚来杯“2022牛客暑期多校训练营3 ACFHJ
Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
I can only sell the company after the capital has been "cut off" for two years
Leetcode daily practice - 160. Cross linked list
Fx3 development board and schematic diagram
Pop up layer prompt in the background
一大早支付宝来短信说你中“奖”了?处理服务器挖矿病毒 - kthreaddi
Rosen's QT journey 101 models and views in QT quick