当前位置: 首页 > 工具软件 > koa-redis > 使用案例 >

KOA框架编程21 使用redis缓存数据库

姜博
2023-12-01

Node.js连接redis显示Client is Closed

Node连接redis的代码如下:

const redis = require('redis');
const client = redis.createClient(6379, 'localhost');
// const client = redis.createClient();


//获取当前db中所有的key
// function getdbnamelist(){
//     // 相当于命令(keys *), 返回list,包含当前db所有key的名字
//     client.keys('*',function(err,val){
//         console.log(val);
//         //callback(val);
//     });
// }

/**
 * 设置键值
 * @param dbNum 库号
 * @param key 键
 * @param value 值
 * @param expire 过期时间(单位:秒,可为空,为空则不过期)
 */
let set = (key,value,expire,dbNum = 0) => {
    console.log(`[redis]set key=${key}  value=${value}  expire=${expire}  dbNum=${dbNum}`)
    return new Promise((resolve, reject) => {
        client.select(dbNum,function (err) {
            if (err){
                throw new Error('redi
 类似资料: