aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/redis.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/redis.ts')
-rw-r--r--server/lib/redis.ts88
1 files changed, 86 insertions, 2 deletions
diff --git a/server/lib/redis.ts b/server/lib/redis.ts
index 941f7d557..0b4b41e4e 100644
--- a/server/lib/redis.ts
+++ b/server/lib/redis.ts
@@ -60,11 +60,11 @@ class Redis {
60 return this.getValue(this.generateResetPasswordKey(userId)) 60 return this.getValue(this.generateResetPasswordKey(userId))
61 } 61 }
62 62
63 setView (ip: string, videoUUID: string) { 63 setIPVideoView (ip: string, videoUUID: string) {
64 return this.setValue(this.buildViewKey(ip, videoUUID), '1', VIDEO_VIEW_LIFETIME) 64 return this.setValue(this.buildViewKey(ip, videoUUID), '1', VIDEO_VIEW_LIFETIME)
65 } 65 }
66 66
67 async isViewExists (ip: string, videoUUID: string) { 67 async isVideoIPViewExists (ip: string, videoUUID: string) {
68 return this.exists(this.buildViewKey(ip, videoUUID)) 68 return this.exists(this.buildViewKey(ip, videoUUID))
69 } 69 }
70 70
@@ -85,6 +85,52 @@ class Redis {
85 return this.setObject(this.buildCachedRouteKey(req), cached, lifetime) 85 return this.setObject(this.buildCachedRouteKey(req), cached, lifetime)
86 } 86 }
87 87
88 addVideoView (videoId: number) {
89 const keyIncr = this.generateVideoViewKey(videoId)
90 const keySet = this.generateVideosViewKey()
91
92 return Promise.all([
93 this.addToSet(keySet, videoId.toString()),
94 this.increment(keyIncr)
95 ])
96 }
97
98 async getVideoViews (videoId: number, hour: number) {
99 const key = this.generateVideoViewKey(videoId, hour)
100
101 const valueString = await this.getValue(key)
102 return parseInt(valueString, 10)
103 }
104
105 async getVideosIdViewed (hour: number) {
106 const key = this.generateVideosViewKey(hour)
107
108 const stringIds = await this.getSet(key)
109 return stringIds.map(s => parseInt(s, 10))
110 }
111
112 deleteVideoViews (videoId: number, hour: number) {
113 const keySet = this.generateVideosViewKey(hour)
114 const keyIncr = this.generateVideoViewKey(videoId, hour)
115
116 return Promise.all([
117 this.deleteFromSet(keySet, videoId.toString()),
118 this.deleteKey(keyIncr)
119 ])
120 }
121
122 generateVideosViewKey (hour?: number) {
123 if (!hour) hour = new Date().getHours()
124
125 return `videos-view-h${hour}`
126 }
127
128 generateVideoViewKey (videoId: number, hour?: number) {
129 if (!hour) hour = new Date().getHours()
130
131 return `video-view-${videoId}-h${hour}`
132 }
133
88 generateResetPasswordKey (userId: number) { 134 generateResetPasswordKey (userId: number) {
89 return 'reset-password-' + userId 135 return 'reset-password-' + userId
90 } 136 }
@@ -107,6 +153,34 @@ class Redis {
107 }) 153 })
108 } 154 }
109 155
156 private getSet (key: string) {
157 return new Promise<string[]>((res, rej) => {
158 this.client.smembers(this.prefix + key, (err, value) => {
159 if (err) return rej(err)
160
161 return res(value)
162 })
163 })
164 }
165
166 private addToSet (key: string, value: string) {
167 return new Promise<string[]>((res, rej) => {
168 this.client.sadd(this.prefix + key, value, err => err ? rej(err) : res())
169 })
170 }
171
172 private deleteFromSet (key: string, value: string) {
173 return new Promise<void>((res, rej) => {
174 this.client.srem(this.prefix + key, value, err => err ? rej(err) : res())
175 })
176 }
177
178 private deleteKey (key: string) {
179 return new Promise<void>((res, rej) => {
180 this.client.del(this.prefix + key, err => err ? rej(err) : res())
181 })
182 }
183
110 private setValue (key: string, value: string, expirationMilliseconds: number) { 184 private setValue (key: string, value: string, expirationMilliseconds: number) {
111 return new Promise<void>((res, rej) => { 185 return new Promise<void>((res, rej) => {
112 this.client.set(this.prefix + key, value, 'PX', expirationMilliseconds, (err, ok) => { 186 this.client.set(this.prefix + key, value, 'PX', expirationMilliseconds, (err, ok) => {
@@ -145,6 +219,16 @@ class Redis {
145 }) 219 })
146 } 220 }
147 221
222 private increment (key: string) {
223 return new Promise<number>((res, rej) => {
224 this.client.incr(this.prefix + key, (err, value) => {
225 if (err) return rej(err)
226
227 return res(value)
228 })
229 })
230 }
231
148 private exists (key: string) { 232 private exists (key: string) {
149 return new Promise<boolean>((res, rej) => { 233 return new Promise<boolean>((res, rej) => {
150 this.client.exists(this.prefix + key, (err, existsNumber) => { 234 this.client.exists(this.prefix + key, (err, existsNumber) => {