@zenweb/cache
Version:
Zenweb Cache module
30 lines (29 loc) • 1.05 kB
JavaScript
import { Cache } from './cache.js';
import { Redis } from 'ioredis';
import { defaultSetupOption } from './options.js';
export * from './global.js';
export * from './types.js';
export * from './utils.js';
export { Locker } from './locker.js';
export { cached } from './middleware.js';
export { CacheHelper } from './helper.js';
export { Cache };
/**
* 安装模块
* @param option 配置
*/
export default function setup(option) {
return async function cache(setup) {
setup.assertModuleExists('result', 'need setup @zenweb/result');
const opt = Object.assign({}, defaultSetupOption, option);
setup.debug('option: %o', opt);
const redis = opt.redis instanceof Redis ? opt.redis : new Redis(opt.redis);
await redis.ping('check');
setup.defineCoreProperty('redis2', { value: redis });
setup.defineCoreProperty('cache2', { value: new Cache(redis, opt) });
setup.destroy(async () => {
setup.debug('quit redis...');
await redis.quit();
});
};
}