UNPKG

wechaty-puppet

Version:

Abstract Puppet for Wechaty

67 lines 2.82 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CacheAgent = void 0; const quick_lru_1 = __importDefault(require("@alloc/quick-lru")); const config_js_1 = require("../config.js"); class CacheAgent { options; contact; friendship; message; post; room; roomInvitation; roomMember; constructor(options) { this.options = options; config_js_1.log.verbose('PuppetCacheAgent', 'constructor(%s)', options ? JSON.stringify(options) : ''); /** * Setup LRU Caches */ const lruOptions = (maxSize = 100) => ({ maxAge: 15 * 60 * 1000 * 1000, maxSize: maxSize, }); this.contact = new quick_lru_1.default(lruOptions(config_js_1.envVars.WECHATY_PUPPET_LRU_CACHE_SIZE_CONTACT(options?.contact))); this.friendship = new quick_lru_1.default(lruOptions(config_js_1.envVars.WECHATY_PUPPET_LRU_CACHE_SIZE_FRIENDSHIP(options?.friendship))); this.message = new quick_lru_1.default(lruOptions(config_js_1.envVars.WECHATY_PUPPET_LRU_CACHE_SIZE_MESSAGE(options?.message))); this.roomInvitation = new quick_lru_1.default(lruOptions(config_js_1.envVars.WECHATY_PUPPET_LRU_CACHE_SIZE_ROOM_INVITATION(options?.roomInvitation))); this.roomMember = new quick_lru_1.default(lruOptions(config_js_1.envVars.WECHATY_PUPPET_LRU_CACHE_SIZE_ROOM_MEMBER(options?.roomMember))); this.room = new quick_lru_1.default(lruOptions(config_js_1.envVars.WECHATY_PUPPET_LRU_CACHE_SIZE_ROOM(options?.room))); this.post = new quick_lru_1.default(lruOptions(config_js_1.envVars.WECHATY_PUPPET_LRU_CACHE_SIZE_POST(options?.post))); } start() { config_js_1.log.verbose('PuppetCacheAgent', 'start()'); } stop() { config_js_1.log.verbose('PuppetCacheAgent', 'stop()'); this.clear(); } /** * FIXME: Huan(202008) clear cache when stop * keep the cache as a temp workaround since wechaty-puppet-service has reconnect issue * with un-cleared cache in wechaty-puppet will make the reconnect recoverable * * Related issue: https://github.com/wechaty/wechaty-puppet-service/issues/31 * * Update: * Huan(2021-08-28): clear the cache when stop */ clear() { config_js_1.log.verbose('PuppetCacheAgent', 'clear()'); this.contact.clear(); this.friendship.clear(); this.message.clear(); this.post.clear(); this.room.clear(); this.roomInvitation.clear(); this.roomMember.clear(); } } exports.CacheAgent = CacheAgent; //# sourceMappingURL=cache-agent.js.map