node.js - 在 Node.js 机器人服务中实现 Redis

标签 node.js azure redis bots botframework

机器人信息

  • 应用 ID:776ba3b4-38e5-4582-809d-7c8d773cfe9b
  • SDK 平台:Node.js
  • SDK 版本:
  • 活跃 channel :直线
  • 部署环境:Auzure Bot 服务

问题描述

我需要帮助实现 Redis 来保存机器人状态。我正在从事一个项目,该项目实际上要求我们尽可能减少延迟。正确知道我们正在使用 DocumentDB,但由于 Redis 使用内存,这可能会更快。

我已按照使用 mongo DB 的教程进行操作,Microsoft Bot framework MongoDB as middle layer to store conversational states, data and context 我正在编辑文件 /lib/IStorageClient.js 以连接、保存和从 Redis 检索。

代码示例

这是我对 /lib/IStorageClient.js 的实现,我没有使用 MongoDB 连接,而是使用了 Redis 连接

"use strict";
var Consts = require('./Consts');
var redis = require('redis');

var IStorageClient = (function () {
function IStorageClient(options) {
    this.options = options;
}

IStorageClient.prototype.initialize = function (callback) {
    var _this = this;
    var host = "MyRedis.redis.cache.windows.net";
    var auth = "KEY";  
    var client = redis.createClient(6380,host , {auth_pass: auth, tls: 
{servername: host}});    
    this.client = client;
    callback(null);
};

IStorageClient.prototype.insertOrReplace = function (partitionKey, rowKey, 
entity, isCompressed, callback) {
    console.log("=========Insert IStorageClient===========")
    var docDbEntity = { id: partitionKey + ',' + rowKey, data: entity, 
isCompressed: isCompressed };
    var host = "MyRedis.redis.cache.windows.net";
    var auth = "KEY";  
    var client = redis.createClient(6380,host , {auth_pass: auth, tls: 
{servername: host}}); 

    client.set(partitionKey + ',' + rowKey,  JSON.stringify(docDbEntity), 
function(err, reply) {
        console.log("=========SET===========");
        console.log("ID: ",partitionKey + ',' + rowKey);
        console.log("Result: ",docDbEntity);
             });
};

 IStorageClient.prototype.retrieve = function (partitionKey, rowKey, 
callback) {
    console.log("=========Retrieve IStorageClient===========")
    var id = partitionKey + ',' + rowKey;
    var host = "MyRedis.redis.cache.windows.net";
    var auth = "KEY";  
    var client = redis.createClient(6380,host , {auth_pass: auth, tls: 
{servername: host}}); 
    //id
    client.get(id, function(error, result){
        console.log("=========Get===========");
        console.log("Search: ",id);
        console.log("Result: ",result);
                            if (error) {
                                console.log("Error:",error)
                                callback(error, null, null);
                            }
                            else if (result == null) {
                                callback(null, null, null);
                            }
                            else if (result.length == 0) {
                                callback(null, null, null);
                            }
                            else {
                                var finaldoc = JSON.parse(result);
                                callback(null, finaldoc, null);
                            }
                        });
    };

IStorageClient.getError = function (error) {
    if (!error)
        return null;
    return new Error('Error Code: ' + error.code + ' Error Body: ' +                 
error.body);
};

   return IStorageClient;
}());
exports.IStorageClient = IStorageClient;

复制步骤

  1. 下载 Microsoft Bot framework MongoDB as middle layer to store conversational states, data and context
  2. /lib/IStorageClient.js 替换为我的实现
  3. 设置 Redis 帐户并输入 /lib/IStorageClient.js
  4. 在机器人模拟器中运行

实际结果

我可以看到 json 保存到 Redis,也可以在控制台中打印检索结果,但问题是机器人模拟器中没有收到答案。

最佳答案

您正在寻找 botbuilder-redis-storage 中间件,可在此处找到:

使用示例:

var redis = require('redis')
var RedisStorage = require('botbuilder-redis-storage')
var builder = require('botbuilder')

// Initialize redis client 
var redisClient = redis.createClient(process.env.REDIS_URL, { prefix: 'bot-storage:' });

// Create new storage with redis client 
var storage = new RedisStorage(redisClient)

var connector = new builder.ChatConnector()
var bot = new builder.UniversalBot(connector)

// Configure bot to use the RedisStorage 
bot.set('storage', storage)

关于node.js - 在 Node.js 机器人服务中实现 Redis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47474181/

相关文章:

node.js - 在 Nodejs Express 上无法访问该网站?

javascript - 如何在 Node 中没有回调的情况下查看异步性

azure - ARM 模板循环依赖问题

transactions - Jedis/Redis 的容器管理事务

spring-boot - 配置spring boot和redis的问题

node.js - 检查 package.json 依赖项是否与已安装的依赖项匹配

javascript - Node 为何在此基准测试中速度如此之快?

data-structures - Redis Hyperloglog - PFCOUNT 副作用

sql-server - 为回溯运行 Azure 数据工厂单个事件

azure - 无法验证此域,因为它已在 Office 365 中的其他地方使用