|
| 1 | +import type { |
| 2 | + Options as QuickLruOptions, |
| 3 | +} from '@alloc/quick-lru' |
| 4 | +import QuickLru from '@alloc/quick-lru' |
| 5 | + |
| 6 | +import { |
| 7 | + envVars, |
| 8 | + log, |
| 9 | +} from './config.js' |
| 10 | +import type { |
| 11 | + ContactPayload, |
| 12 | +} from './schemas/contact.js' |
| 13 | +import type { |
| 14 | + FriendshipPayload, |
| 15 | +} from './schemas/friendship.js' |
| 16 | +import type { |
| 17 | + MessagePayload, |
| 18 | +} from './schemas/message.js' |
| 19 | +import type { |
| 20 | + RoomMemberPayload, |
| 21 | + RoomPayload, |
| 22 | +} from './schemas/room.js' |
| 23 | +import type { |
| 24 | + RoomInvitationPayload, |
| 25 | +} from './schemas/room-invitation.js' |
| 26 | + |
| 27 | +interface PayloadCacheOptions { |
| 28 | + contact? : number |
| 29 | + friendship? : number |
| 30 | + message? : number |
| 31 | + room? : number |
| 32 | + roomInvitation? : number |
| 33 | + roomMember? : number |
| 34 | +} |
| 35 | + |
| 36 | +class PayloadCache { |
| 37 | + |
| 38 | + readonly contact : QuickLru<string, ContactPayload> |
| 39 | + readonly friendship : QuickLru<string, FriendshipPayload> |
| 40 | + readonly message : QuickLru<string, MessagePayload> |
| 41 | + readonly room : QuickLru<string, RoomPayload> |
| 42 | + readonly roomMember : QuickLru<string, RoomMemberPayload> |
| 43 | + readonly roomInvitation : QuickLru<string, RoomInvitationPayload> |
| 44 | + |
| 45 | + constructor ( |
| 46 | + protected options: PayloadCacheOptions = {}, |
| 47 | + ) { |
| 48 | + log.verbose('PayloadCache', 'constructor(%s)', JSON.stringify(options)) |
| 49 | + |
| 50 | + /** |
| 51 | + * Setup LRU Caches |
| 52 | + */ |
| 53 | + const lruOptions = (maxSize = 100): QuickLruOptions<any, any> => ({ |
| 54 | + maxAge: 15 * 60 * 1000 * 1000, // 15 minutes |
| 55 | + maxSize: maxSize, |
| 56 | + }) |
| 57 | + |
| 58 | + this.contact = new QuickLru<string, ContactPayload>(lruOptions( |
| 59 | + envVars.WECHATY_PUPPET_LRU_CACHE_SIZE_CONTACT(options.contact)), |
| 60 | + ) |
| 61 | + this.friendship = new QuickLru<string, FriendshipPayload>(lruOptions( |
| 62 | + envVars.WECHATY_PUPPET_LRU_CACHE_SIZE_FRIENDSHIP(options.friendship)), |
| 63 | + ) |
| 64 | + this.message = new QuickLru<string, MessagePayload>(lruOptions( |
| 65 | + envVars.WECHATY_PUPPET_LRU_CACHE_SIZE_MESSAGE(options.message)), |
| 66 | + ) |
| 67 | + this.roomInvitation = new QuickLru<string, RoomInvitationPayload>(lruOptions( |
| 68 | + envVars.WECHATY_PUPPET_LRU_CACHE_SIZE_ROOM_INVITATION(options.roomInvitation)), |
| 69 | + ) |
| 70 | + this.roomMember = new QuickLru<string, RoomMemberPayload>(lruOptions( |
| 71 | + envVars.WECHATY_PUPPET_LRU_CACHE_SIZE_ROOM_MEMBER(options.roomMember)), |
| 72 | + ) |
| 73 | + this.room = new QuickLru<string, RoomPayload>(lruOptions( |
| 74 | + envVars.WECHATY_PUPPET_LRU_CACHE_SIZE_ROOM(options.room)), |
| 75 | + ) |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + start (): void { |
| 80 | + log.verbose('PayloadCache', 'start()') |
| 81 | + this.clear() |
| 82 | + } |
| 83 | + |
| 84 | + stop (): void { |
| 85 | + log.verbose('PayloadCache', 'stop()') |
| 86 | + this.clear() |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * FIXME: Huan(202008) clear cache when stop |
| 91 | + * keep the cache as a temp workaround since wechaty-puppet-service has reconnect issue |
| 92 | + * with un-cleared cache in wechaty-puppet will make the reconnect recoverable |
| 93 | + * |
| 94 | + * Related issue: https://github.com/wechaty/wechaty-puppet-service/issues/31 |
| 95 | + * |
| 96 | + * Update: |
| 97 | + * Huan(2021-08-28): clear the cache when stop |
| 98 | + */ |
| 99 | + clear (): void { |
| 100 | + log.verbose('PayloadCache', 'clear()') |
| 101 | + |
| 102 | + this.contact.clear() |
| 103 | + this.friendship.clear() |
| 104 | + this.message.clear() |
| 105 | + this.room.clear() |
| 106 | + this.roomInvitation.clear() |
| 107 | + this.roomMember.clear() |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Concat roomId & contactId to one string |
| 112 | + */ |
| 113 | + roomMemberId ( |
| 114 | + roomId : string, |
| 115 | + memberId : string, |
| 116 | + ): string { |
| 117 | + return roomId + '-' + memberId |
| 118 | + } |
| 119 | + |
| 120 | +} |
| 121 | + |
| 122 | +export { PayloadCache } |
0 commit comments