<commit_before>/**
 * Created by AlexanderC on 6/16/15.
 */

'use strict';

import {AbstractDriver} from './AbstractDriver';
import Redis from 'ioredis';
import {RedisClusterException} from './Exception/RedisClusterException';

/**
 * Redis/Elasticache driver implementation
 */
export class RedisDriver extends AbstractDriver {
  /**
   * @param {String} dsn
   */
  constructor(dsn) {
    super();

    this._client = dsn ? new Redis(dsn) : new Redis();
  }

  /**
   * @returns {Redis}
   */
  get client() {
    return this._client;
  }

  /**
   * @param {String} key
   * @param {Function} callback
   */
  _has(key, callback = () => '') {
    this._client.exists(key, function(error, results) {
      if (error && error !== null) {
        throw new RedisClusterException(error);
      }

      callback(results);
    }.bind(this));
  }

  /**
   * @param {String} key
   * @param {Function} callback
   */
  _get(key, callback = () => '') {
    this._client.get(key, function(error, results) {
      if (error && error !== null) {
        throw new RedisClusterException(error);
      }

      callback(results);
    }.bind(this));
  }

  /**
   * @param {String} key
   * @param {*} value
   * @param {Number} ttl
   * @param {Function} callback
   * @returns {Boolean}
   */
  _set(key, value, ttl = 0,callback = () => '') {
    this._client.set(key, value, ttl, function(error) {
      if (error && error !== null) {
        throw new RedisClusterException(error);
      }

      callback(true);
    }.bind(this));
  }

  /**
   * @param {String} key
   * @param {Number} timeout
   * @param {Function} callback
   */
  _invalidate(key, timeout = 0, callback = () => '') {
    this._client.del(key, timeout, function(error) {
      if (error && error !== null) {
        throw new RedisClusterException(error);
      }

      callback(true);
    }.bind(this));
  }

  /**
   * @param {Function} callback
   * @returns {AbstractDriver}
   */
  _flush(callback = () => '') {
    this._client.flushall(function(error) {
      if (error && error !== null) {
        throw new RedisClusterException(error);
      }

      callback(true);
    }.bind(this));
  }
}
<commit_msg>Improve redis cache driver<commit_after>/**
 * Created by AlexanderC on 6/16/15.
 */

'use strict';

import {AbstractDriver} from './AbstractDriver';
import {RedisClusterException} from './Exception/RedisClusterException';

/**
 * Redis/Elasticache driver implementation
 */
export class RedisDriver extends AbstractDriver {
  /**
   * @param {String} dsn
   */
  constructor(dsn) {
    super();

    let nativeDriver = RedisDriver.NATIVE_DRIVER;

    this._client = dsn ? new nativeDriver(dsn) : new nativeDriver();
  }

  /**
   * @returns {Redis}
   */
  get client() {
    return this._client;
  }

  /**
   * @param {String} key
   * @param {Function} callback
   */
  _has(key, callback = () => '') {
    this._client.exists(key, function(error, results) {
      if (error && error !== null) {
        throw new RedisClusterException(error);
      }

      callback(results);
    }.bind(this));
  }

  /**
   * @param {String} key
   * @param {Function} callback
   */
  _get(key, callback = () => '') {
    this._client.get(key, function(error, results) {
      if (error && error !== null) {
        throw new RedisClusterException(error);
      }

      callback(results);
    }.bind(this));
  }

  /**
   * @param {String} key
   * @param {*} value
   * @param {Number} ttl
   * @param {Function} callback
   * @returns {Boolean}
   */
  _set(key, value, ttl = 0,callback = () => '') {
    this._client.set(key, value, ttl, function(error) {
      if (error && error !== null) {
        throw new RedisClusterException(error);
      }

      callback(true);
    }.bind(this));
  }

  /**
   * @param {String} key
   * @param {Number} timeout
   * @param {Function} callback
   */
  _invalidate(key, timeout = 0, callback = () => '') {
    this._client.del(key, timeout, function(error) {
      if (error && error !== null) {
        throw new RedisClusterException(error);
      }

      callback(true);
    }.bind(this));
  }

  /**
   * @param {Function} callback
   * @returns {AbstractDriver}
   */
  _flush(callback = () => '') {
    this._client.flushall(function(error) {
      if (error && error !== null) {
        throw new RedisClusterException(error);
      }

      callback(true);
    }.bind(this));
  }

  /**
   * @returns {Function}
   * @constructor
   */
  static get NATIVE_DRIVER() {
    return require('ioredis');
  }
}

{'fraction_non_alphanumeric': 0.1685418045444518, 'fraction_numerical': 0.003088462386940216, 'mean_word_length': 2.5588697017268447, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '7576385', 'n_tokens_mistral': 1500, 'n_tokens_neox': 1453, 'n_words': 322}