UNPKG

apis-scf-nodejs

Version:

Apis Serverless Compute Function NodeJS Package

366 lines (320 loc) 8.69 kB
// const Apis = require( 'apis-core-nodejs' ); // const LRU = require( 'lru-cache' ); // const NodeCache = require( 'node-cache' ); const Cache = { type : 'file', instance : null, /** * 连接 * @param type * @param config * @returns {Promise<HelperCache>} */ connect : async function( type = '', config = {} ) { //缓存类型 let _type = type; if( Apis.Utils.Base.isEmpty( _type ) ) { _type = CONFIG[ 'Base' ].cache.default; } //缓存配置 let _config = config; if( Apis.Utils.Base.isEmpty( _config ) ) { _config = CONFIG[ 'Base' ].cache[ _type ]; } if( this.instance === null ) { switch( _type ) { case 'redis': this.type = 'redis'; this.instance = new Redis( _config ); break; case 'memcached': this.type = 'memcached'; // if( !_config.hasOwnProperty( 'checkperiod' ) ) { _config.checkperiod = 60; } this.instance = new NodeCache( _config ); break; case 'file': default: this.type = 'file'; this.instance = new LRU( { max : 5000, maxSize : 50000, ttl : 1000 * 60 * 5, sizeCalculation : ( value, key ) => { return 1; } } ); break; } //throw new Error( '缓存类型错误' ); } return this; }, /** * 设置 * @param key * @param data * @param options * @returns {Promise<*>} */ set : async function( key, data = '', options = {} ) { //数据 let _data = JSON.stringify( data ); //结果 let _result = null; //过期时间、10年 let _time = (3600 * 24 * 365 * 10); if( options.hasOwnProperty( 'time' ) && Number( options.time ) > 0 ) { _time = Number( options.time ); } //多少秒 let _ttl = (3600 * 24 * 7); if( options.hasOwnProperty( 'ttl' ) && Number( options.ttl ) > 0 ) { _ttl = Number( options.ttl ); } // if( this.instance === null ) { await this.connect(); } // if( 'redis' === this.type ) { _result = await this.instance.set( key, _data, 'EX', _time ); } // if( 'memcached' === this.type ) { _result = await this.instance.set( key, _data, _ttl ); } // if( 'file' === this.type ) { _result = await this.instance.set( key, _data, _time ); } return _result; }, /** * 获取 * @param key * @returns {Promise<*>} */ get : async function( key ) { // let _result = null; // if( this.instance === null ) { await this.connect(); } // if( 'redis' === this.type ) { await this.instance.get( key, ( err, res ) => { if( err ) { _result = null; } else { _result = JSON.parse( res ); } } ); } // if( 'memcached' === this.type ) { _result = await this.instance.get( key ); //没有找到会返回undefined if( _result === 'undefined' || _result === undefined ) { _result = ''; } else { _result = JSON.parse( _result ); } } // if( 'file' === this.type ) { _result = await this.instance.get( key ); //没有找到会返回undefined if( _result === 'undefined' ) { _result = ''; } else { _result = JSON.parse( _result ); } } return _result; }, /** * 删除 * @param key * @returns {Promise<boolean>} */ delete : async function( key ) { // let _result = false; // if( this.instance === null ) { await this.connect(); } // if( 'redis' === this.type ) { await this.instance.del( key, ( err, res ) => { if( err ) { _result = false; } else { _result = true; } } ); } // if( 'memcached' === this.type ) { _result = await this.instance.del( key ); if( Number( _result ) > 0 ) { _result = true; } else { _result = false; } } // if( 'file' === this.type ) { _result = await this.instance.del( key ); } return _result; }, /** * 检查 * @param key * @returns {Promise<*|boolean>} */ has : async function( key ) { // let _result = false; // if( this.instance === null ) { await this.connect(); } // if( 'redis' === this.type ) { _result = await this.instance.exists( key ); } // if( 'memcached' === this.type ) { _result = await this.instance.has( key ); } // if( 'file' === this.type ) { _result = await this.instance.has( key ); } return _result; }, /** * 清空 * @returns {Promise<*|boolean>} */ flush : async function() { // let _result = false; // if( this.instance === null ) { await this.connect(); } if( 'redis' === this.type ) { _result = await this.instance.flushdb(); } if( 'memcached' === this.type ) { _result = await this.instance.flushAll(); } if( 'file' === this.type ) { _result = await this.instance.reset(); } return _result; }, /** * 存在返回,不存在写入 * @param key * @param dataOrCallback * @param time * @returns {Promise<*>} */ remember : async function( key, dataOrCallback = null, time = 0 ) { // let _result = null; // let _check = await this.has( key ); if( _check ) { _result = await this.get( key ); //console.log('======================'+key+'存在========================'); //console.log(_result); } else { // if( typeof dataOrCallback === 'function' ) { _result = await dataOrCallback(); } else { _result = dataOrCallback; } let _options = {}; if( Number( time ) > 0 ) { _options.time = time; } //console.log('======================'+key+'不在========================'); //console.log(_result); await this.set( key, _result, _options ); } return _result; } }; module.exports = Cache;