<commit_before>/*
 * cache get, put, getput, del, flushall
 */

'use strict';

var redis       = require('redis');
var winston     = require('winston');
var _           = require('lodash');
var moment      = require('moment');

var cls         = require('./cls');

var options     = null;
var redisclient = null;

var retryStrategy = function(params) {
  if (params.error.code === 'ECONNREFUSED') {
    winston.error('Redis connection refused on host ' + options.host + ':' + options.port);
    return params.error;
  }
  winston.error('Redis error ' + params.error);
  // retry in n seconds
  return params.attempt * 1000;
};


// init cache module : create redis client
module.exports.init = function(config) {
  config      = config || {};
  options     = config.redis || {};

  options     = _.defaultsDeep(options, {
    ttl:  3600,  // default ttl is 1 hour
    no_ready_check: true,
    retry_strategy: retryStrategy
  });

  redisclient = redis.createClient(options);
  if (options.password) {
    redisclient.auth(options.password);
  }
  redisclient.select(options.database || 0);
  redisclient.on('error', function (err) {
    // winston.error('' + err);
  });

  if (config.env !== 'production') {
    module.exports.flushall();
  }
};

//
module.exports.redisclient = function() {
  return redisclient;
};

//
module.exports.put = function(namespace, id, value, callback, ttl) {
  var k = namespace + '/' + id;
  var v = JSON.stringify({ v: value });

  redisclient.set(k, v, cls.bind(function(err) {
    if (callback) {
      callback(null, value);
    }
    redisclient.expire(k, ttl || options.ttl);
  }));
};

//
module.exports.get = function(namespace, id, callback) {

  var k = namespace + '/' + id;
  redisclient.get(k, cls.bind(function(err, value) {
    if (!value) {
      return callback('notfound');
    }
    // found obj in redis<commit_msg>igo.Cache : remove default timeout<commit_after>/*
 * cache get, put, getput, del, flushall
 */

'use strict';

var redis       = require('redis');
var winston     = require('winston');
var _           = require('lodash');
var moment      = require('moment');

var cls         = require('./cls');

var options     = null;
var redisclient = null;

var retryStrategy = function(params) {
  if (params.error.code === 'ECONNREFUSED') {
    winston.error('Redis connection refused on host ' + options.host + ':' + options.port);
    return params.error;
  }
  winston.error('Redis error ' + params.error);
  // retry in n seconds
  return params.attempt * 1000;
};


// init cache module : create redis client
module.exports.init = function(config) {
  config      = config || {};
  options     = config.redis || {};

  options     = _.defaultsDeep(options, {
    timeout:            null,  // no timeout by default
    no_ready_check: true,
    retry_strategy: retryStrategy
  });

  redisclient = redis.createClient(options);
  if (options.password) {
    redisclient.auth(options.password);
  }
  redisclient.select(options.database || 0);
  redisclient.on('error', function (err) {
    // winston.error('' + err);
  });

  if (config.env !== 'production') {
    module.exports.flushall();
  }
};

//
module.exports.redisclient = function() {
  return redisclient;
};

//
module.exports.put = function(namespace, id, value, callback, timeout) {
  var k = namespace + '/' + id;
  var v = JSON.stringify({ v: value });

  redisclient.set(k, v, cls.bind(function(err) {
    if (callback) {
      callback(null, value);
    }
    timeout = timeout || options.timeout;
    if (timeout) {
      redisclient.expire(k, timeout);
    }
  }));
};

//
module.exports.get = function(namespace, id, callback) {

  var k = namespace + '/' + id;
  redisclient.get(k, cls.bind(function(err, value) {
    if (!value) {
      return callback('notfound');
    }
    // found obj in redis
{'fraction_non_alphanumeric': 0.15853976531942635, 'fraction_numerical': 0.003911342894393742, 'mean_word_length': 2.954639175257732, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6045240', 'n_tokens_mistral': 1273, 'n_tokens_neox': 1251, 'n_words': 291}