当前位置:网站首页>[Architect (Part 41)] installation of server development and connection to redis database
[Architect (Part 41)] installation of server development and connection to redis database
2022-06-24 11:29:00 【A tailed Warbler】
Keep creating , Accelerate growth ! This is my participation 「 Nuggets day new plan · 6 Yuegengwen challenge 」 Of the 24 God , Click to see the event details
Local installation Redis
download
Go to the download address , I downloaded this
Local installation
I unzip it here to D Dish up , That's all the files .
Use vscode Open this file
find requirepass foobared, Copy to a new line , Ahead of # Delete , And clear the space , Otherwise, the password will not be set successfully .
And then foobared Change it to the password you need , Then save , If the identity is not enough, open it as an administrator .
stay redis The folder is open cmd, perform redis-server.exe redis.windows.conf, At this time, the interface shows redis Information , Indicates that the service is on normally .
In order to facilitate the next startup , stay redis Create a new folder under the folder startup.bat file , The content is redis-server.exe redis.windows.conf.
Don't close this window , Continue opening one cmd, perform redis-cli.exe -h 127.0.0.1 -p 6379 -a xiaowei123 enter
among xiaowei123 That's the password you set up before .
And then use set key value Set the cache , Use get key Read cache , The following interface shows that the local installation is successful .
Connect Redis
Install... In the project Redis
Here in order to follow the teacher to learn , Specify the installation version , Different syntax in the new version will lead to errors .
npm i [email protected]3.0.2 -S
To configure Redis
// src\config\envs\dev.js
module.exports = {
// redis Connection configuration
redisConf: {
port: '6379',
host: '127.0.0.1',
password: 'xiaowei123'
},
}
Package settings / Access to the cache
// src\cache\index.js
const redisClient = require('../db/redis')
/** * redis set * @param {string} key key * @param {string|Object} val val * @param {number} timeout Expiration time , Company s , Default 1h */
function cacheSet(key, val, timeout = 60 * 60) {
let formatVal
if (typeof val === 'object') {
formatVal = JSON.stringify(val)
} else {
formatVal = val
}
redisClient.set(key, formatVal)
redisClient.expire(key, timeout)
}
/** * redis get * @param {string} key key */
function cacheGet(key) {
const promise = new Promise((resolve, reject) => {
redisClient.get(key, (err, val) => {
if (err) {
reject(err)
return
}
if (val == null) {
resolve(null)
return
}
try {
resolve(JSON.parse(val))
} catch (ex) {
resolve(val)
}
})
})
return promise
}
module.exports = {
cacheSet,
cacheGet,
}
Test connection Redis database
// src\db\redis.js
const redis = require('redis')
const { redisConf } = require('../config/index')
// Create client
const { port, host, password } = redisConf
const opt = {}
if (password) {
opt.password = password // prd The environment requires a password
}
const redisClient = redis.createClient(port, host, opt)
redisClient.on('error', err => {
console.error('redis connect error', err)
})
// function node src/db/redis.js Make a test connection
redisClient.on('connect', () => {
console.log('redis connect success')
redisClient.set('foo', 'bar', redis.print) // => "Reply: OK"
redisClient.get('foo', redis.print) // => "Reply: bar"
redisClient.quit()
})
module.exports = redisClient
Console execution node src/db/redis.js, appear success It means the connection is successful .
Further test the database connection
// src\routes\index.js
const router = require('koa-router')()
const packageInfo = require('../../package.json')
const testMysqlConn = require('../db/mysql2')
const ENV = require('../utils/env')
const { WorkModel } = require('../models/WorksModel')
const { cacheGet, cacheSet } = require('../cache/index')
// Test database connection
router.get('/api/db-check', async (ctx) => {
// test mysql Database connection
const mysqlRes = await testMysqlConn()
// test mongodb Database connection
let mongodbConn
try {
mongodbConn = true
await WorkModel.findOne()
} catch (error) {
mongodbConn = false
}
// test Redis Connect
cacheSet('name', 'biz editor sever OK - by redis')
const redisTestVal = await cacheGet('name')
ctx.body = {
errno: 0,
data: {
name: 'biz editor server',
version: packageInfo.version,
ENV,
mysqlConn: mysqlRes.length > 0,
mongodbConn,
redisConn: redisTestVal != null,
}
}
})
module.exports = router
visit http://localhost:3000/api/db-check,redisConn As the result of the true, It also further proves that Redis Database connection succeeded .
边栏推荐
- A fault record of misoperation dhclient script
- Axi low power interface
- 【本周六活动】.NET Day in China
- Reliable remote code execution (1)
- @RequestBody注解
- [the lottery in May has ended, and the list of winners has been announced] special session of techo youth university open course database
- 计组_cpu的结构和工作流程
- 工具及方法 - 在Source Insight中使用代码格式化工具
- Coinbase将推出首个针对零售交易员的加密衍生产品
- System design: load balancing
猜你喜欢

math_等比数列求和推导&等幂和差推导/两个n次方数之差/

【毕业季·进击的技术er】绕树三匝,何枝可依?

@RequestBody注解

《opencv学习笔记》-- 感兴趣区域(ROI)、图像混合

Visual presentation of pictures effectively enhances the attraction of large screen

历史上的今天:图灵诞生日;互联网奠基人出生;Reddit 上线

TP-LINK 1208 router tutorial (2)

Qt: judge whether the string is in numeric format

把騰訊搬到雲上,治愈了他們的技術焦慮

Today in history: Turing's birth day; The birth of the founder of the Internet; Reddit goes online
随机推荐
Why does the virtual machine Ping the host but not the virtual machine
It's so difficult for me. Have you met these interview questions?
Why should we make the best use of the external chain in SEO?
Use the process monitor tool to monitor process operations on registries and files
"Write once, run at all ends", Qualcomm released AI software stack!
Base64 decoding method three ways for non professionals
PF_ Ring ZC | high speed traffic processing dpdk alternative
[latest - lightweight cloud servers - hot sales] new lightweight application server optimization scheme, 1-core 2g5m time limit as low as 99 yuan / year
Axi low power interface
Turn 2D photos into 3D models to see NVIDIA's new AI "magic"!
How to open a video number?
《opencv学习笔记》-- 感兴趣区域(ROI)、图像混合
Anonymous Messenger: hidden communication of Trojan horse
《opencv学习笔记》-- 离散傅里叶变换
Jenkins performance test
Jenkins remote publishing products
Realization of alarm clock with AHK
AXI低功耗接口
The record of 1300+ times of listing and the pursuit of ultimate happiness
程序员大部分时间不是写代码,而是。。。