lib/CacheStore/index.js
function CacheStore(options) {
    if (!options.id) {
        throw new IdNotDefinedError();
    }
    var storeType, _cache;

    function parseKey(key) {
        return key.replace(/[^a-z0-9]/gi, '_').replace(/^-+/, '').replace(/-+$/, '').toLowerCase();
    }

    /**
     * Returns Cache Store object
     * @returns {Object}
     */
    this.getCacheStore = function () {
        return _cache;
    };

    /**
     * Method initiates cache store depends on type
     * @param type {String} cache store type
     * @returns {CacheStore}
     */
    this._setStoreType = function (type) {
        storeType = type;
        var ttl = options.expires || DEFAULT_EXPIRATION_TIME;
        options.max = options.max || 0; //makes lru cache to store infinite no of items
        switch (type) {
            case STORE_TYPE_MEMORY:
                _cache = CacheManager.caching({store: STORE_TYPE_MEMORY, max: options.max, ttl: ttl});
                break;

            case STORE_TYPE_REDIS:
                _cache = CacheManager.caching({store: RedisStore, db: options.db, ttl: ttl, host: options.host,
                    port: options.port, id: options.id
                });
                break;

            default :
                throw new InvalidCacheStoreTypeError(type);

        }
        //exposing delete method alias of _del
        _cache.delete = _cache.del;
        return this;
    };

    //setting public functions
    /**
     * Gets cache item
     * @param key {String} Key to retrieve cache item
     * @param next {Function} callback. parameters are err, value
     */
    this.get = function (key, next) {
        _cache.get(parseKey(key), next);
    };

    /**
     * Sets cache item in cache.
     * @param key {String} Key to retrieve cache item
     * @param value {String} Value to save cache item
     * @param next {Function} callback. parameters are err
     */
    this.set = function (key, value, next) {
        _cache.set(parseKey(key), value, next);
    };

    /**
     * Delete from cache
     * @param key {String} Key to retrieve cache item
     * @param next {Function} callback. parameters are err
     */
    this.delete = function (key, next) {
        _cache.del(parseKey(key), next);
    };

    /**
     * Wraps a function in cache, the first time the function is run,
     * its results are stored in cache so subsequent calls retrieve from cache
     * instead of calling the function.
     *
     * @example
     *
     *      var key = 'user_' + user_id;
     *      cache.wrap(key, function(cb) {
     *          User.get(user_id, cb);
     *      }, function(err, user) {
     *         console.log(user);
     *      });
     *
     * @param key {String} Key to retrieve cache item
     * @param wrapFn
     * @param next {Function} callback. parameters are err, value
     */
    this.wrap = function (key, wrapFn, next) {
        _cache.wrap(parseKey(key), wrapFn, next);
    };

    /**
     * Flushes the current cache store
     * @param next {Function} callback. parameters are err, success
     */
    this.reset = function (next) {
        _cache.reset(next);
    };

    /**
     * Gets all keys associated with this cache store
     * @param next {Function} callback. parameters are err, keys
     */
    this.keys = function (next) {
        _cache.keys(next);
    };
}
function CacheStore(options) {
    if (!options.id) {
        throw new IdNotDefinedError();
    }
    var storeType, _cache;

    function parseKey(key) {
        return key.replace(/[^a-z0-9]/gi, '_').replace(/^-+/, '').replace(/-+$/, '').toLowerCase();
    }

    /**
     * Returns Cache Store object
     * @returns {Object}
     */
    this.getCacheStore = function () {
        return _cache;
    };

    /**
     * Method initiates cache store depends on type
     * @param type {String} cache store type
     * @returns {CacheStore}
     */
    this._setStoreType = function (type) {
        storeType = type;
        var ttl = options.expires || DEFAULT_EXPIRATION_TIME;
        options.max = options.max || 0; //makes lru cache to store infinite no of items
        switch (type) {
            case STORE_TYPE_MEMORY:
                _cache = CacheManager.caching({store: STORE_TYPE_MEMORY, max: options.max, ttl: ttl});
                break;

            case STORE_TYPE_REDIS:
                _cache = CacheManager.caching({store: RedisStore, db: options.db, ttl: ttl, host: options.host,
                    port: options.port, id: options.id
                });
                break;

            default :
                throw new InvalidCacheStoreTypeError(type);

        }
        //exposing delete method alias of _del
        _cache.delete = _cache.del;
        return this;
    };

    //setting public functions
    /**
     * Gets cache item
     * @param key {String} Key to retrieve cache item
     * @param next {Function} callback. parameters are err, value
     */
    this.get = function (key, next) {
        _cache.get(parseKey(key), next);
    };

    /**
     * Sets cache item in cache.
     * @param key {String} Key to retrieve cache item
     * @param value {String} Value to save cache item
     * @param next {Function} callback. parameters are err
     */
    this.set = function (key, value, next) {
        _cache.set(parseKey(key), value, next);
    };

    /**
     * Delete from cache
     * @param key {String} Key to retrieve cache item
     * @param next {Function} callback. parameters are err
     */
    this.delete = function (key, next) {
        _cache.del(parseKey(key), next);
    };

    /**
     * Wraps a function in cache, the first time the function is run,
     * its results are stored in cache so subsequent calls retrieve from cache
     * instead of calling the function.
     *
     * @example
     *
     *      var key = 'user_' + user_id;
     *      cache.wrap(key, function(cb) {
     *          User.get(user_id, cb);
     *      }, function(err, user) {
     *         console.log(user);
     *      });
     *
     * @param key {String} Key to retrieve cache item
     * @param wrapFn
     * @param next {Function} callback. parameters are err, value
     */
    this.wrap = function (key, wrapFn, next) {
        _cache.wrap(parseKey(key), wrapFn, next);
    };

    /**
     * Flushes the current cache store
     * @param next {Function} callback. parameters are err, success
     */
    this.reset = function (next) {
        _cache.reset(next);
    };

    /**
     * Gets all keys associated with this cache store
     * @param next {Function} callback. parameters are err, keys
     */
    this.keys = function (next) {
        _cache.keys(next);
    };
}
function CacheStore ( options ) { if ( ! options . id ) { throw new IdNotDefinedError ( ) ; } var storeType , _cache ; function parseKey ( key ) { return key . replace ( / [^a-z0-9] / gi , '_' ) . replace ( / ^-+ / , '' ) . replace ( / -+$ / , '' ) . toLowerCase ( ) ; } /**
     * Returns Cache Store object
     * @returns {Object}
     */ this . getCacheStore = function ( ) { return _cache ; } ; /**
     * Method initiates cache store depends on type
     * @param type {String} cache store type
     * @returns {CacheStore}
     */ this . _setStoreType = function ( type ) { storeType = type ; var ttl = options . expires || DEFAULT_EXPIRATION_TIME ; options . max = options . max || 0 ; //makes lru cache to store infinite no of items switch ( type ) { case STORE_TYPE_MEMORY : _cache = CacheManager . caching ( { store : STORE_TYPE_MEMORY , max : options . max , ttl : ttl } ) ; break ; case STORE_TYPE_REDIS : _cache = CacheManager . caching ( { store : RedisStore , db : options . db , ttl : ttl , host : options . host , port : options . port , id : options . id } ) ; break ; default : throw new InvalidCacheStoreTypeError ( type ) ; } //exposing delete method alias of _del _cache . delete = _cache . del ; return this ; } ; //setting public functions /**
     * Gets cache item
     * @param key {String} Key to retrieve cache item
     * @param next {Function} callback. parameters are err, value
     */ this . get = function ( key , next ) { _cache . get ( parseKey ( key ) , next ) ; } ; /**
     * Sets cache item in cache.
     * @param key {String} Key to retrieve cache item
     * @param value {String} Value to save cache item
     * @param next {Function} callback. parameters are err
     */ this . set = function ( key , value , next ) { _cache . set ( parseKey ( key ) , value , next ) ; } ; /**
     * Delete from cache
     * @param key {String} Key to retrieve cache item
     * @param next {Function} callback. parameters are err
     */ this . delete = function ( key , next ) { _cache . del ( parseKey ( key ) , next ) ; } ; /**
     * Wraps a function in cache, the first time the function is run,
     * its results are stored in cache so subsequent calls retrieve from cache
     * instead of calling the function.
     *
     * @example
     *
     *      var key = 'user_' + user_id;
     *      cache.wrap(key, function(cb) {
     *          User.get(user_id, cb);
     *      }, function(err, user) {
     *         console.log(user);
     *      });
     *
     * @param key {String} Key to retrieve cache item
     * @param wrapFn
     * @param next {Function} callback. parameters are err, value
     */ this . wrap = function ( key , wrapFn , next ) { _cache . wrap ( parseKey ( key ) , wrapFn , next ) ; } ; /**
     * Flushes the current cache store
     * @param next {Function} callback. parameters are err, success
     */ this . reset = function ( next ) { _cache . reset ( next ) ; } ; /**
     * Gets all keys associated with this cache store
     * @param next {Function} callback. parameters are err, keys
     */ this . keys = function ( next ) { _cache . keys ( next ) ; } ; }
Abstract function to create cache store
@param [options]
@constructor
Abstract function to create cache store