How to connect to ElastiCache cluster using node.js

后端 未结 1 1851
挽巷
挽巷 2021-02-12 12:08

We know that ElastiCache is not recommended to be accessed outside Amazon instances, so we\'re trying below stuff inside Amazon EC2 instances only.

1条回答
  •  暖寄归人
    2021-02-12 12:16

    Sharing the code for future readers:

    var RedisClustr = require('redis-clustr');
    var RedisClient = require('redis');
    var config = require("./config.json");
    
    var redis = new RedisClustr({
        servers: [
            {
                host: config.redisClusterHost,
                port: config.redisClusterPort
            }
        ],
        createClient: function (port, host) {
            // this is the default behaviour
            return RedisClient.createClient(port, host);
        }
    });
    
    //connect to redis
    redis.on("connect", function () {
      console.log("connected");
    });
    
    //check the functioning
    redis.set("framework", "AngularJS", function (err, reply) {
      console.log("redis.set " , reply);
    });
    
    redis.get("framework", function (err, reply) {
      console.log("redis.get ", reply);
    });
    

    0 讨论(0)
提交回复
热议问题