all files / kayn/dist/lib/Caches/ RedisCache.js

64.71% Statements 22/34
35.71% Branches 5/14
40% Functions 4/10
50% Lines 9/18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49                                                                               
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
 
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; Eif ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (EprotoProps) defineProperties(Constructor.prototype, protoProps); Iif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
 
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
var redis = require('redis');
 
var RedisCache = function () {
  function RedisCache(opts) {
    _classCallCheck(this, RedisCache);
 
    var options = Object.assign({
      host: '127.0.0.1',
      port: 6379,
      keyPrefix: 'kayn-'
    }, opts || {});
 
    this.client = redis.createClient(options.port, options.host);
    this.client.on('error', function (err) {
      console.log('Redis error:', err);
    });
 
    this.prefix = options.keyPrefix;
  }
 
  _createClass(RedisCache, [{
    key: 'get',
    value: function get(args, cb) {
      this.client.get(this.prefix + args.key, function (err, reply) {
        reply ? cb(err, reply) : cb(err);
        return;
      });
    }
  }, {
    key: 'set',
    value: function set(args, value) {
      this.client.setex(this.prefix + args.key, args.ttl, value);
    }
  }]);
 
  return RedisCache;
}();
 
exports.default = RedisCache;