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.ts25
1 files changed, 21 insertions, 4 deletions
diff --git a/server/lib/redis.ts b/server/lib/redis.ts
index 9ef9d7702..4dcbcddb5 100644
--- a/server/lib/redis.ts
+++ b/server/lib/redis.ts
@@ -1,9 +1,11 @@
1import { createClient } from 'redis' 1import { createClient } from 'redis'
2import { exists } from '@server/helpers/custom-validators/misc' 2import { exists } from '@server/helpers/custom-validators/misc'
3import { sha256 } from '@shared/extra-utils'
3import { logger } from '../helpers/logger' 4import { logger } from '../helpers/logger'
4import { generateRandomString } from '../helpers/utils' 5import { generateRandomString } from '../helpers/utils'
5import { CONFIG } from '../initializers/config' 6import { CONFIG } from '../initializers/config'
6import { 7import {
8 AP_CLEANER,
7 CONTACT_FORM_LIFETIME, 9 CONTACT_FORM_LIFETIME,
8 RESUMABLE_UPLOAD_SESSION_LIFETIME, 10 RESUMABLE_UPLOAD_SESSION_LIFETIME,
9 TRACKER_RATE_LIMITS, 11 TRACKER_RATE_LIMITS,
@@ -260,6 +262,17 @@ class Redis {
260 return this.deleteKey('resumable-upload-' + uploadId) 262 return this.deleteKey('resumable-upload-' + uploadId)
261 } 263 }
262 264
265 /* ************ AP ressource unavailability ************ */
266
267 async addAPUnavailability (url: string) {
268 const key = this.generateAPUnavailabilityKey(url)
269
270 const value = await this.increment(key)
271 await this.setExpiration(key, AP_CLEANER.PERIOD * 2)
272
273 return value
274 }
275
263 /* ************ Keys generation ************ */ 276 /* ************ Keys generation ************ */
264 277
265 private generateLocalVideoViewsKeys (videoId?: Number) { 278 private generateLocalVideoViewsKeys (videoId?: Number) {
@@ -298,6 +311,10 @@ class Redis {
298 return 'contact-form-' + ip 311 return 'contact-form-' + ip
299 } 312 }
300 313
314 private generateAPUnavailabilityKey (url: string) {
315 return 'ap-unavailability-' + sha256(url)
316 }
317
301 /* ************ Redis helpers ************ */ 318 /* ************ Redis helpers ************ */
302 319
303 private getValue (key: string) { 320 private getValue (key: string) {
@@ -330,10 +347,6 @@ class Redis {
330 return this.client.del(this.prefix + key) 347 return this.client.del(this.prefix + key)
331 } 348 }
332 349
333 private getObject (key: string) {
334 return this.client.hGetAll(this.prefix + key)
335 }
336
337 private increment (key: string) { 350 private increment (key: string) {
338 return this.client.incr(this.prefix + key) 351 return this.client.incr(this.prefix + key)
339 } 352 }
@@ -342,6 +355,10 @@ class Redis {
342 return this.client.exists(this.prefix + key) 355 return this.client.exists(this.prefix + key)
343 } 356 }
344 357
358 private setExpiration (key: string, ms: number) {
359 return this.client.expire(this.prefix + key, ms / 1000)
360 }
361
345 static get Instance () { 362 static get Instance () {
346 return this.instance || (this.instance = new this()) 363 return this.instance || (this.instance = new this())
347 } 364 }