本篇文章给大家谈谈noderedis,以及noderedis v4对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、Nodejs 连接 Redis数据库实例
- 2、nodejs中redis有必要做连接池吗
- 3、nodejs 怎么访问指定的redis据库
- 4、node.js中如何配置redis与连接池?
- 5、在Node.js应用中读写Redis数据库的简单方法
Nodejs 连接 Redis数据库实例
报错:Node连蚂慧接Redis报错 “ClientClosedError: The client is closed”
查询资料才发现: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);
}
})();
nodejs中redis有必要做连接池吗
木有必要。。因为redis也是单线程处理你的请求的。。保持长连接就好了
[img]nodejs 怎么访问指定的redis据库
安装redis模块, 该模森野块会默认安装至当前掘春枝目录下的node_modules里边:
npm install redis
然后连接redis, 并进行get-set操判敏作
var redis = require('redis');
var client = redis.createClient('6379', '127.0.0.1');
// redis 链接错误
client.on("error", function(error) {
console.log(error);
});
node.js中如何配置redis与连接池?
node.js中配置连接池可以考虑使用generic-pool模块
官缺厅网:
var poolModule = bbPromise.promisifyAll(require('generic-pool'));
var redispool = poolModule.Pool({
name : 'redis',
create : function(callback) {
var client = Redis.createClient(configs.dbconfig.dbredis.port,
configs.dbconfig.dbredis.host);
callback(null, client);
},
destroy : function(client) { client.quit(); },
max : 10,
// optional. if you set this, make sure to drain() (see step 3)
min : 2,
// specifies how long a resource can stay idle in pool before being removed
idleTimeoutMillis : 30000
// if true, logs via console.log - can also be a function
//log : true
});
function getRedisClient() {
return redispool.acquireAsync().disposer(function(client, promise) {
console.log("redispool.release(client)"旁扮森运亩)
redispool.release(client);
});
}
dbs.redisclient = getRedisClient ;
在Node.js应用中读写Redis数据库的简单方法
在开始本文之前请确保安装好
Redis
和
Node.js
以及
Node.js
的
Redis
扩展
——
node_redis
首先创建一个新文件夹并新建文本文件
app.js
文件内容如下:
var
redis
=
require("redis")
,
client
=
redis.createClient();
client.on("error",
function
(err)
{
console.log("Error
"
+
err);
});
client.on("connect",
runSample);
function
runSample()
{
//
Set
a
value
client.set("型侍羡string
key",
"Hello
World",
function
(err,
reply)
{
console.log(reply.toString());
});
//
Get
a
value
client.get("string
key",
function
(err,
reply)
{
console.log(reply.toString());
});
}
当连接到
Redis
后会调用
runSample
函数并设置一个值,紧接着便读出该值,运行的结果如下:
OK
Hello
World
我们也可以使用
EXPIRE
命令来设置对象的失效时间,代码如下:
var
redis
=
require('redis')
,
client
=
redis.createClient();
client.on('error',
function
(err)
{
console.log('Error
'
+
err);
});
client.on('connect',
runSample);
function
runSample()
{
//
Set
a
value
with
an
expiration
client.set('string
key',
'Hello
World',
redis.print);
//
Expire
in
3
seconds
client.expire('string
key',
3);
//
This
timer
is
only
to
demo
the
TTL
//
Runs
every
second
until
the
timeout
//
occurs
on
the
value
var
myTimer
=
setInterval(function()
{
client.get('string
key',
function
(err,
reply)
{
if(reply)
{
console.log('I
live:
'
+
reply.toString());
}
else
{
clearTimeout(myTimer);
console.log('I
expired');
client.quit();
}
});
},
1000);
}
注意:
上述使用的定时器只是为了演示
EXPIRE
命令,你必须在
Node.js
项目中谨慎使用定时器。
运行上述程序的输出卜拍结谈皮果是:
Reply:
OK
I
live:
Hello
World
I
live:
Hello
World
I
live:
Hello
World
I
expired
接下来我们检查一个值在失效之前存留了多长时间:
var
redis
=
require('redis')
,
client
=
redis.createClient();
client.on('error',
function
(err)
{
console.log('Error
'
+
err);
});
client.on('connect',
runSample);
function
runSample()
{
//
Set
a
value
client.set('string
key',
'Hello
World',
redis.print);
//
Expire
in
3
seconds
client.expire('string
key',
3);
//
This
timer
is
only
to
demo
the
TTL
//
Runs
every
second
until
the
timeout
//
occurs
on
the
value
var
myTimer
=
setInterval(function()
{
client.get('string
key',
function
(err,
reply)
{
if(reply)
{
console.log('I
live:
'
+
reply.toString());
client.ttl('string
key',
writeTTL);
}
else
{
clearTimeout(myTimer);
console.log('I
expired');
client.quit();
}
});
},
1000);
}
function
writeTTL(err,
data)
{
console.log('I
live
for
this
long
yet:
'
+
data);
}
运行结果:
Reply:
OK
I
live:
Hello
World
I
live
for
this
long
yet:
2
I
live:
Hello
World
I
live
for
this
long
yet:
1
I
live:
Hello
World
I
live
for
this
long
yet:
I
expired
关于noderedis和noderedis v4的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。