查询资料才发现:Node Redis版本V4之后,连接语法变了。
Starting from v4 of node-redis library, you need to call client.connect() after initializing a client. See this migration guide.
新语法:
const redis = require('redis')
const client = redis.createClient({ socket: { port: 6379 } })
client.connect()
client.on('connect', () =>{
console.log('connected')
})
You might also want to consider running the client connect method with await in an asynchronous function. So you don't have to worry about event listeners.
const redis = require('redis')
(async () =>{
try {
const client = redis.createClient({ socket: { port: 6379 } })
await client.connect()
console.log('connected')
} catch (err) {
console.error(err)
}
})()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Example]:
const redis = require("redis")
(async () =>{
try {
const client = redis.createClient({
socket: { port: 6379 },
legacyMode: true,
})
await client.connect()
console.log("connected")
await client.v4.set("key4", "value2", {
NX: true,
})
client.set("key3", "value3", "NX", (err, reply) =>{})
await client.get("key4", function (err, v) {
console.log("redis get hello err,v", err, v)
})
client.set("student1", "Laylaa1", function (err, reply) {
if (err) {
console.log(err)
callback(err, null)
return
}
console.log(reply)
})
} catch (err) {
console.error(err)
}
})()
-直连-连接池
1.占位符的使用
举个栗子:
没有连接池的时候,船家只有一条船,这一条船不断地送人过河。
有连接池之后,船家(连接池)有10(最大连接数)条船,10条船可以同时送人过河。
1、封装连接池方法
使用
2、封装直接连的方法
使用
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)