Sunday, 30 April 2017

Overwrite Redis method

I try to add an option to my app to toggle using a cache. For cache I am using Redis and made simple wrapper for that. The problem is coming when I try to overwrite redis.get method, it's simply doens't work or cannot found this.

'use strict';

const redis = require('redis');
const config = require('../config');

const REDIS_EMPTY_VALUE = 'NOT_EXIST';
const MINUTE = 60;

let client = redis.createClient({
    host: config.get('REDIS_HOST'),
    port: config.get('REDIS_PORT')
});

client.on("error", function (err) {
    logging.error('Redis Error: ' + err);
    throw new Error(err);
});

/**
 * Next are custom extensions for Redis module
 */
client.emptyValue = REDIS_EMPTY_VALUE;
client.minute = MINUTE;
client.setAndExprire = function(key, value, expire) {
    this.set(key, value);
    this.expire(key, expire);
};

// Here is the problem
client.get = function(key, cb) {
    if (config.get('disable-cache') === 'true') return cb(null, null);
    return client.get(key, cb);
}

module.exports = client;



via Alfred

No comments:

Post a Comment