{"size": 2980, "ext": "js", "max_stars_repo_path": "src/shared/memcached.js", "max_stars_repo_name": "d3estudio/weekly-digest", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/shared/memcached.js", "max_issues_repo_name": "d3estudio/weekly-digest", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/shared/memcached.js", "max_forks_repo_name": "d3estudio/weekly-digest", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 32.0430107527, "max_line_length": 94, "alphanum_fraction": 0.4879194631}
var _Memcached = require('memcached'),
    settings = require('./settings').sharedInstance(),
    Singleton = require('./singleton');

/**
 * Wraps a Memcached instance in order to provide promisified and overloaded
 * methods
 */
class Memcached {
    /**
     * Initialises a new connection to the Memcached server defined by the application
     * settings.
     * @return {Memcached} A new instance of this class
     */
    constructor() {
        this.instance = new _Memcached(`${settings.memcachedHost}:${settings.memcachedPort}`);
    }

    /**
     * Gets the value for a given key.
     * @param  {String}     key     Key name to be retrieved from the Memcached server
     * @return {Promise}            Promise that will be resolved after getting the value
     *                              for the given key. If key is undefined, `undefined` will
     *                              be passed to the promise resolver.
     */
    get(key) {
        return new Promise((resolve, reject) => {
            this.instance.get(key, (err, d) => {
                if(err) {
                    reject(err);
                } else {
                    resolve(d);
                }
            });
        });
    }

    /**
     * Creates or updates a given key on the Memcached server
     * @param {String}  key     Key to identify this value on the memcached server
     * @param {String}  value   Value for the provided key
     * @return {Promise}        Promise that will be resolved after the value is set on the
     *                          server. The value passed to the resolver is the same value.
     */
    set(key, value) {
        return new Promise((resolve, reject) => {
            this.instance.set(key, value, 0, (err) => {
                if(err) {
                    reject(err);
                } else {
                    resolve(value);
                }
            });
        });
    }

    /**
     * Flushes all the data from the memcached server
     * @return {Promise}    A promise that will be resolved after the operation is completed.
     */
    flush() {
        return new Promise((resolve, reject) => {
            this.instance.flush((err) => {
                if(err) {
                    reject(err);
                } else {
                    resolve();
                }
            });
        });
    }

    /**
     * Deletes a given key from the memcached server
     * @param  {String}     key     Key to be deleted from the server.
     * @return {Promise}            A promise that will be resolved after the key is
     *                              removed from the server.
     */
    del(key) {
        return new Promise((resolve, reject) => {
            this.instance.del(key, (err) => {
                if(err) {
                    reject(err);
                } else {
                    resolve();
                }
            });
        });
    }
}

module.exports = new Singleton(Memcached);
