Skip to content

Commit d53e748

Browse files
authored
create PayloadCache class (#153)
1 parent d4c7d1f commit d53e748

File tree

5 files changed

+163
-89
lines changed

5 files changed

+163
-89
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"typescript.tsdk": "./node_modules/typescript/lib",
33

4-
"editor.fontFamily": "'Fira Code iScript', Consolas, 'Courier New', monospace",
4+
"editor.fontFamily": "Consolas, 'Courier New', monospace",
55
"editor.fontLigatures": true,
66

77
"editor.tokenColorCustomizations": {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"dependencies": {
5757
"@alloc/quick-lru": "^5.2.0",
5858
"brolog": "^1.12.4",
59-
"clone-class": "^0.9.13",
59+
"clone-class": "^0.9.19",
6060
"file-box": "^0.23.2",
6161
"hot-import": "^0.2.14",
6262
"memory-card": "^0.12.2",

src/payload-cache.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env -S node --no-warnings --loader ts-node/esm
2+
3+
import { test } from 'tstest'
4+
5+
import { PayloadCache } from './payload-cache.js'
6+
7+
test('PayloadCache roomMemberId() restart', async t => {
8+
const payloadCache = new PayloadCache()
9+
const roomMemberId = payloadCache.roomMemberId('roomId', 'userId')
10+
t.equal(roomMemberId, 'roomId-userId', 'should get right id')
11+
})

src/payload-cache.ts

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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

Comments
 (0)