| 1 | import { Injectable } from '@nestjs/common' |
| 2 | import { RedisService } from '@yikart/redis' |
| 3 | import { ServerRedisKeys } from './server-redis-keys' |
| 4 | |
| 5 | @Injectable() |
| 6 | export class ServerRedisService { |
| 7 | constructor(private readonly redis: RedisService) {} |
| 8 | |
| 9 | async incrementRateLimit(key: string, windowSeconds: number): Promise<number> { |
| 10 | const luaScript = ` |
| 11 | local count = redis.call("INCR", KEYS[1]) |
| 12 | if tonumber(count) == 1 then |
| 13 | redis.call("EXPIRE", KEYS[1], ARGV[1]) |
| 14 | end |
| 15 | return count |
| 16 | ` |
| 17 | |
| 18 | return Number(await this.redis.eval(luaScript, 1, ServerRedisKeys.rateLimit(key), windowSeconds.toString())) |
| 19 | } |
| 20 | |
| 21 | async saveShortLink(code: string, originalUrl: string): Promise<boolean> { |
| 22 | return this.redis.set(ServerRedisKeys.shortLink(code), originalUrl, 60 * 60 * 24) |
| 23 | } |
| 24 | |
| 25 | async getShortLink(code: string): Promise<string | null> { |
| 26 | return this.redis.get(ServerRedisKeys.shortLink(code)) |
| 27 | } |
| 28 | |
| 29 | async saveChannelAuthSession<T>(sessionId: string, session: T, ttlSeconds = 60 * 5): Promise<boolean> { |
| 30 | return this.redis.setJson(ServerRedisKeys.channelAuthSession(sessionId), session, ttlSeconds) |
| 31 | } |
| 32 | |
| 33 | async getChannelAuthSession<T>(sessionId: string): Promise<T | null> { |
| 34 | return this.redis.getJson(ServerRedisKeys.channelAuthSession(sessionId)) |
| 35 | } |
| 36 | |
| 37 | async getChannelCredentialCache<T>(accountId: string): Promise<T | null> { |
| 38 | return this.redis.getJson(ServerRedisKeys.channelCredential(accountId)) |
| 39 | } |
| 40 | |
| 41 | async saveChannelCredentialCache<T>(accountId: string, credential: T): Promise<boolean> { |
| 42 | return this.redis.setJson(ServerRedisKeys.channelCredential(accountId), credential, 60 * 5) |
| 43 | } |
| 44 | |
| 45 | async deleteChannelCredentialCache(accountId: string): Promise<boolean> { |
| 46 | return this.redis.del(ServerRedisKeys.channelCredential(accountId)) |
| 47 | } |
| 48 | |
| 49 | async acquireChannelCredentialRefreshLock(accountId: string, token: string): Promise<boolean> { |
| 50 | return this.redis.setNx(ServerRedisKeys.channelCredentialLock(accountId), token, 30) |
| 51 | } |
| 52 | |
| 53 | async releaseChannelCredentialRefreshLock(accountId: string, token: string): Promise<boolean> { |
| 54 | const luaScript = ` |
| 55 | if redis.call("GET", KEYS[1]) == ARGV[1] then |
| 56 | return redis.call("DEL", KEYS[1]) |
| 57 | end |
| 58 | return 0 |
| 59 | ` |
| 60 | |
| 61 | return Number(await this.redis.eval(luaScript, 1, ServerRedisKeys.channelCredentialLock(accountId), token)) > 0 |
| 62 | } |
| 63 | |
| 64 | async saveLegacyChannelAuthTask<T>(platform: string, id: string, value: T): Promise<boolean> { |
| 65 | return this.redis.setJson(ServerRedisKeys.legacyChannelAuthTask(platform, id), value, 60 * 5) |
| 66 | } |
| 67 | |
| 68 | async saveLegacyGoogleBusinessAuthTask<T>(id: string, value: T): Promise<boolean> { |
| 69 | return this.redis.setJson(ServerRedisKeys.legacyChannelAuthTask('google_business', id), value, 60 * 10) |
| 70 | } |
| 71 | |
| 72 | async saveLegacyRelayAuthTask<T>(platform: string, id: string, value: T): Promise<boolean> { |
| 73 | return this.redis.setJson(ServerRedisKeys.legacyChannelAuthTask(platform, id), value, 60 * 10) |
| 74 | } |
| 75 | |
| 76 | async getLegacyChannelAuthTask<T>(platform: string, id: string): Promise<T | null> { |
| 77 | return this.redis.getJson(ServerRedisKeys.legacyChannelAuthTask(platform, id)) |
| 78 | } |
| 79 | |
| 80 | async deleteLegacyChannelAuthTask(platform: string, id: string): Promise<boolean> { |
| 81 | return this.redis.del(ServerRedisKeys.legacyChannelAuthTask(platform, id)) |
| 82 | } |
| 83 | |
| 84 | async extendLegacyChannelAuthTask(platform: string, id: string): Promise<boolean> { |
| 85 | return this.redis.expire(ServerRedisKeys.legacyChannelAuthTask(platform, id), 60 * 3) |
| 86 | } |
| 87 | |
| 88 | async saveLegacyChannelAccessToken<T extends { expires_in?: number, expiresAt?: number }>(platform: string, accountId: string, value: T): Promise<boolean> { |
| 89 | return this.redis.setJson( |
| 90 | ServerRedisKeys.legacyChannelAccessToken(platform, accountId), |
| 91 | value, |
| 92 | value.expires_in ?? (value.expiresAt ? Math.max(value.expiresAt - Math.floor(Date.now() / 1000), 1) : undefined), |
| 93 | ) |
| 94 | } |
| 95 | |
| 96 | async getLegacyChannelAccessToken<T>(platform: string, accountId: string): Promise<T | null> { |
| 97 | return this.redis.getJson(ServerRedisKeys.legacyChannelAccessToken(platform, accountId)) |
| 98 | } |
| 99 | |
| 100 | async deleteLegacyChannelAccessToken(platform: string, accountId: string): Promise<boolean> { |
| 101 | return this.redis.del(ServerRedisKeys.legacyChannelAccessToken(platform, accountId)) |
| 102 | } |
| 103 | |
| 104 | async deleteLegacyChannelCamelAccessToken(platform: string, accountId: string): Promise<boolean> { |
| 105 | return this.redis.del(ServerRedisKeys.legacyChannelCamelAccessToken(platform, accountId)) |
| 106 | } |
| 107 | |
| 108 | async saveLegacyChannelPageAccessToken<T>(platform: string, pageId: string, value: T): Promise<boolean> { |
| 109 | return this.redis.setJson(ServerRedisKeys.legacyChannelPageAccessToken(platform, pageId), value) |
| 110 | } |
| 111 | |
| 112 | async getLegacyChannelPageAccessToken<T>(platform: string, pageId: string): Promise<T | null> { |
| 113 | return this.redis.getJson(ServerRedisKeys.legacyChannelPageAccessToken(platform, pageId)) |
| 114 | } |
| 115 | |
| 116 | async deleteLegacyChannelPageAccessToken(platform: string, pageId: string): Promise<boolean> { |
| 117 | return this.redis.del(ServerRedisKeys.legacyChannelPageAccessToken(platform, pageId)) |
| 118 | } |
| 119 | |
| 120 | async saveLegacyChannelUserPageList<T>(platform: string, accountId: string, value: T): Promise<boolean> { |
| 121 | return this.redis.setJson(ServerRedisKeys.legacyChannelUserPageList(platform, accountId), value) |
| 122 | } |
| 123 | |
| 124 | async getLegacyChannelUserPageList<T>(platform: string, accountId: string): Promise<T | null> { |
| 125 | return this.redis.getJson(ServerRedisKeys.legacyChannelUserPageList(platform, accountId)) |
| 126 | } |
| 127 | |
| 128 | async saveDouyinClientToken<T extends { expires_in?: number }>(value: T): Promise<boolean> { |
| 129 | return this.redis.setJson(ServerRedisKeys.douyinClientToken(), value, value.expires_in) |
| 130 | } |
| 131 | |
| 132 | async getDouyinClientToken<T>(): Promise<T | null> { |
| 133 | return this.redis.getJson(ServerRedisKeys.douyinClientToken()) |
| 134 | } |
| 135 | |
| 136 | async deleteDouyinClientToken(): Promise<boolean> { |
| 137 | return this.redis.del(ServerRedisKeys.douyinClientToken()) |
| 138 | } |
| 139 | |
| 140 | async saveDouyinOpenTicket<T extends { expires_in?: number }>(value: T): Promise<boolean> { |
| 141 | return this.redis.setJson(ServerRedisKeys.douyinOpenTicket(), value, value.expires_in) |
| 142 | } |
| 143 | |
| 144 | async getDouyinOpenTicket<T>(): Promise<T | null> { |
| 145 | return this.redis.getJson(ServerRedisKeys.douyinOpenTicket()) |
| 146 | } |
| 147 | |
| 148 | async deleteDouyinOpenTicket(): Promise<boolean> { |
| 149 | return this.redis.del(ServerRedisKeys.douyinOpenTicket()) |
| 150 | } |
| 151 | } |
| 152 |