-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathRedisService.ts
84 lines (72 loc) · 2.38 KB
/
RedisService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { ENV } from '@env'
import Redis from 'ioredis'
import { injectable, singleton } from 'tsyringe'
interface Service {
connection(): Promise<string>
get generateKey(): GenerateRedisKey
get queueName(): Record<QueueName, string>
createFeedQueue(data: CreateFeedArgs): Promise<number>
}
@injectable()
@singleton()
export class RedisService extends Redis implements Service {
constructor() {
super({ port: ENV.redisPort, host: ENV.redisHost })
}
public async connection(): Promise<string> {
return new Promise((resolve) => {
this.connect(() => {
resolve(`Redis connection established to ${ENV.redisHost}`)
})
})
}
public get generateKey(): GenerateRedisKey {
return {
recommendedPost: (postId: string) => `${postId}:recommend`,
postCache: (username: string, postUrlSlug: string) => `ssr:/@${username}/${postUrlSlug}`,
userCache: (username: string) => `ssr:/@${username}`,
postSeries: (username: string, seriesUrlSlug: string) =>
`ssr:/@${username}/series/${seriesUrlSlug}`,
changeEmail: (code: string) => `changeEmailCode:${code}`,
trendingWriters: () => `trending:writers`,
existsUser: (userId: string) => `exists:user:${userId}`,
}
}
public get queueName(): Record<QueueName, string> {
return {
createFeed: 'queue:feed',
checkPostSpam: 'queue:checkPostSpam',
}
}
public async createFeedQueue(data: CreateFeedArgs): Promise<number> {
const queueName = this.queueName.createFeed
return await this.lpush(queueName, JSON.stringify(data))
}
public async addToSpamCheckQueue(data: CheckPostSpamArgs): Promise<number> {
const queueName = this.queueName.checkPostSpam
return await this.lpush(queueName, JSON.stringify(data))
}
}
type GenerateRedisKey = {
recommendedPost: (postId: string) => string
postCache: (username: string, postUrlSlug: string) => string
userCache: (username: string) => string
postSeries: (username: string, seriesUrlSlug: string) => string
changeEmail: (code: string) => string
trendingWriters: () => string
existsUser: (userId: string) => string
}
type QueueName = 'createFeed' | 'checkPostSpam'
export type ChangeEmailArgs = {
email: string
userId: string
}
export type CreateFeedArgs = {
fk_following_id: string
fk_post_id: string
}
export type CheckPostSpamArgs = {
post_id: string
user_id: string
ip: string
}